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.
Pins
| Pin | What it is | Type |
|---|---|---|
| VCC | Supply 3.3–5 V | power |
| GND | Ground | ground |
| OUT | Digital out: HIGH over a dark line | io |
In circuit.py:
LineSensor()— Line-tracking sensor — $3.50- Pins: 3
Wiring it in circuit.py
Power it from 3.3 V, so OUT is a 3.3 V logic signal:
from wiring import *
pi = Pi()
line = LineSensor()
pi.V3_3 >> line.vcc
line.gnd >> pi.GND
line.out >> pi.GPIO17For 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
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
OUTin 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
linefeed is the comparator's decision:1means a dark line,0means 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
- GPIO and the 40-pin header — reading a digital input.
- Compare with the obstacle sensor, which uses the same optical pair with the comparator set for distance.
Further reading
- TCRT5000 datasheet — emitter, phototransistor and reflection distance.
- LM393 comparator datasheet — the open-collector comparator inside the module.