Preview build — page text is a draft awaiting human review. Code badges come from real compiles. The flasher is real but not yet hardware-tested; the Playground is still a preview.

TCRT5000 line sensor with Raspberry Pi Pico — MicroPython line follower basics

Detect a black line with a TCRT5000 IR reflective sensor on a Raspberry Pi Pico: DO on GP15, AO on ADC0 (GP26), powered from 3V3, GP25 LED as indicator.

MicroPythonVerified — micropython (mpy-cross v1.29.0), 3 Nov 2026Compile-only

Community draft — report a mistake

The TCRT5000 shines infrared at the surface below it and measures how much comes back: white reflects, black absorbs. That is all a line-following robot needs. On the Raspberry Pi Pico, DO goes to GP15 and AO to GP26 (ADC0).

Power the module from 3V3. The script prints whether it sees the line, with a bar graph of the analog level, and lights the Pico’s GP25 LED over the line.

Wiring

Raspberry Pi PicoOnly the pins used are shown · 3.3 V logicTCRT5000 IR reflective sensor moduleGNDGP153V3GP26VCCGNDDOAO
Every connection
Raspberry Pi PicoTCRT5000 IR reflective sensor moduleWireNotes
3V3VCCred3V3 is pin 36. DO and AO then stay within the Pico’s 3.3 V range.
GNDGNDblack
GP15DOyellowPhysical pin 20.
GP26AOgreenGP26 = ADC0, physical pin 31.

3.3–5 V part, 3.3 V board. Outputs swing to VCC. Power it from 3.3 V on ESP32/Pico, 5 V on Uno.

Board quirks

  • Hold the sensor 2–10 mm above the surface. Black electrical tape on white chart paper is the classic test track.
  • Turn the blue pot until DO flips exactly at the line edge. AO shows the raw reflectance, useful for setting the threshold or for smoother “how far off the line” steering.
  • A line-follower robot needs two to five of these. The Pico has three ADC pins (GP26–GP28), so use DO for most sensors and AO only where you want proportional steering.
  • Sunlight contains plenty of IR and can blind the sensor — test indoors.

Code

main.pyMicroPython
# TCRT5000 reflective IR sensor (line follower) on a Raspberry Pi Pico (MicroPython).
# Wiring: VCC -> 3V3 (pin 36), GND -> GND, DO -> GP15 (pin 20), AO -> GP26 / ADC0 (pin 31).
# DO is HIGH over a black line (little IR reflected) on most modules - check yours.
from machine import Pin, ADC
import time

do = Pin(15, Pin.IN)
ao = ADC(26)
led = Pin(25, Pin.OUT)  # plain Pico LED

print("TCRT5000: move it over black tape on white paper, 2-10 mm above")
while True:
    on_line = do.value() == 1
    level = ao.read_u16()
    led.value(on_line)
    bar = "#" * (level * 30 // 65535)
    print("{:5s}  AO {:5d} {}".format("LINE" if on_line else "white", level, bar))
    time.sleep_ms(100)

Expected serial output

REPL / Thonny shellOutput
TCRT5000: move it over black tape on white paper, 2-10 mm above
white  AO  8214 ###
LINE   AO 51876 #######################

Common errors

DO never changes

Too high above the surface, or the pot is at one end. Bring it to about 5 mm and turn the pot slowly.

LINE and white are reversed

Some modules invert DO. Change do.value() == 1 to == 0.

↑↓ to move · Enter to open