PyRasp

Line-tracking sensor

The line-tracking module costs $3.50 and is the simplest way to give a robot a sense of the floor. A TCRT5000 package contains an infrared LED and a phototransistor. The LED shines down at the surface; black tape reflects little, white paper reflects a lot. An LM393 comparator turns that difference into a clean digital signal and the blue trimmer sets the trip point. OUT is high over a dark line and low over a light surface.

Line-tracking sensor

Pins

PinWhat it isType
VCCSupply 3.3–5 Vpower
GNDGroundground
OUTDigital out: HIGH over a dark lineio

In circuit.py:

Wiring it in circuit.py

Power it from 3.3 V, so OUT is a 3.3 V logic signal:

circuit.py
from wiring import *

pi = Pi()

line = LineSensor()

pi.V3_3 >> line.vcc
line.gnd >> pi.GND
line.out >> pi.GPIO17

For a Pico the same three wires go to pico.P36 (3V3), pico.GND and a GP pin such as pico.GP16. On the KS3017 shield the header is labelled G / V / S; V goes to the 3.3 V selection.

Driving it in main.py

main.py
from gpiozero import DigitalInputDevice
from time import sleep

line = DigitalInputDevice(17)

while True:
    print("line" if line.value else "floor")
    sleep(0.05)

pull_up=None is not needed because the comparator actively drives OUT, but it is harmless. Keep the sensor 2–15 mm above the surface; too high and even black tape looks white.

Electrical notes

  • Supply 3.3–5 V. The 3.3 V connection keeps OUT in GPIO range; from 5 V the output is 5 V and needs a divider on a Pi or Pico.
  • The IR LED is invisible; the module's indicator LED shows the comparator state, not the IR.
  • The trimmer is the threshold. Turn it until the indicator changes exactly at the line edge.
  • Ambient sunlight contains infrared. Strong sun or a very reflective floor can confuse the comparator; a shaded sensor and a short range are more reliable.
  • The output is digital only. If you need the analogue reflection, use the phototransistor and an ADC instead of this module.

Simulator vs real hardware

  • The line feed is the comparator's decision: 1 means a dark line, 0 means a light surface. Move the slider or use a timed stimulus to test both.
  • The simulator does not model ambient IR or the trimmer's analogue curve; the feed is already clean.
  • The same model powers the line-sensor problem in the Robotics unit.

Going deeper

Further reading