Try Embedded

Pico line follower in MicroPython

The two-wheel robot on a Raspberry Pi Pico in MicroPython: three IR sensors on its ADC, the outer two through their comparators, PD steering on two PWM…

Control · Intermediate

In a real product: Line-following robot (Robotics): A classroom robot kit runs MicroPython: students change the steering and run it again in seconds.

The PD line follower in MicroPython on a Raspberry Pi Pico. The Pico has only three ADC inputs, so the middle three IR sensors are read as analog and the outer two through the array's comparators, which go high over the tape. Open the World tool's Arena view to watch it.

What you'll learn

  • Mixing analog and digital sensors into one estimate of the line's position
  • machine.ADC and read_u16, and machine.PWM at 20 kHz into a TB6612FNG
  • A polled control loop with time.sleep_ms, no interrupts
  • PD steering, in a few lines of Python

How it works

IR2, IR3 and IR4 are on GP26–GP28; read_u16 gives 0–65535 at 3.3 V, about 3900 over the mat and 58 980 over the tape. The outer sensors' comparator outputs D1 and D5 are on GP14 and GP15: 1 over the tape. The loop scales the three readings to 0–1, counts each comparator as fully over the tape, and takes the weighted mean of their places (+24, +12, 0, −12, −24 mm) as the line's position.

Each motor has its direction pins set once (both forwards) and a PWM output at 20 kHz on its driver's PWM input. The steering KP·pos + KD·(pos − last) comes off the left duty and goes on the right, every 5 ms. The loop polls everything: it needs no pin interrupts.

Try this

  • Run it with the Arena view open; watch how the outer sensors only switch, while the middle three read smoothly.
  • Set KD = 0: the robot snakes. How big can KP get before it oscillates?
  • Raise BASE toward 0.8. Where does it start losing the line on the bends?
  • Print the lap time: the robot's start gate is under it at the start, so count your own laps from the line position.

main.py

# Line follower on the arena robot: a Raspberry Pi Pico in MicroPython, through a TB6612FNG.
# The Pico has three ADC inputs: the middle sensors IR2..IR4 on GP26..GP28, the outer two through the array's
# comparators (D1 on GP14, D5 on GP15: high over the tape). The loop polls them every 5 ms; no pin interrupts.
from machine import ADC, PWM, Pin
import time

WHITE, BLACK = 3900, 58980        # read_u16 over the mat and over the tape, at the robot's 3.3 V
BASE = 0.62                        # duty, 0..1
KP, KD = 0.045, 0.55               # per mm, and per mm of change in 5 ms

mid = [ADC(26), ADC(27), ADC(28)]  # IR2, IR3, IR4
outer_left, outer_right = Pin(14, Pin.IN), Pin(15, Pin.IN)
Pin(6, Pin.OUT).on(); Pin(7, Pin.OUT).off()     # AIN1, AIN2: left forwards
Pin(10, Pin.OUT).on(); Pin(11, Pin.OUT).off()   # BIN1, BIN2: right forwards
Pin(9, Pin.OUT).on()                            # STBY
left, right = PWM(Pin(8)), PWM(Pin(12))
for pwm in (left, right):
    pwm.freq(20000)

def duty(pwm, x):
    pwm.duty_u16(int(max(0.0, min(1.0, x)) * 65535))

last = 0.0
count = 0
while True:
    cover = [max(0.0, min(1.0, (a.read_u16() - WHITE) / (BLACK - WHITE))) for a in mid]
    # Positions in mm, + to the left: IR2 +12, IR3 0, IR4 -12; the comparators count as fully over the tape.
    total = sum(cover) + outer_left.value() + outer_right.value()
    weighted = 12 * cover[0] - 12 * cover[2] + 24 * outer_left.value() - 24 * outer_right.value()
    pos = weighted / total if total > 0.2 else (30.0 if last > 0 else -30.0)
    u = KP * pos + KD * (pos - last)
    last = pos
    duty(left, BASE - u)
    duty(right, BASE + u)
    count += 1
    if count % 50 == 0:
        print("pos", round(pos, 1))
    time.sleep_ms(5)

Lessons behind it