Line Following — Starter Code
This folder holds the starter program for Day 5: Line Following + Showcase.
line_follow.py— the starter you’ll edit, calibrate, and tune todaysolution/line_follow.py— instructor reference (a complete, runnable example)
How to run it
- On a Windows laptop or Chromebook, open Chrome, Edge, or Chromium and go to code.pybricks.com. (Web Bluetooth is required — iPads, iPhones, and Firefox will not connect.)
- Turn on your hub. Click the Bluetooth / connect button and pick your hub’s name from the list (your hub has a name and a matching label).
- Make a new program and paste in
line_follow.py(or build the same thing with blocks — see the Block Coding Guide). - Click Run. Watch the robot follow the edge of the line.
You need contrast
This only works if the line stands out from the floor. Use black electrical tape on white poster board, or a light-colored floor. No contrast → the sensor can’t tell the line from the floor.
Before you trust the numbers — calibrate!
The line_follow.py starter contains starting guesses, not correct answers:
BLACK = 10 # measured on the tape
WHITE = 80 # measured on the floor
threshold = (BLACK + WHITE) / 2
GAIN = 1.2 # TUNE: too low = sloppy, too high = wobbly
Every tape, floor, and robot is a little different. You’ll measure and tune today:
- Hold the sensor over the black tape and read
line_sensor.reflection()→ that’sBLACK - Hold it over the white floor and read
line_sensor.reflection()→ that’sWHITE - The
thresholdis the midpoint between them - Change only
GAINand watch: too low = sloppy, too high = wobbly. Find just right.
Record your readings and GAIN tries on the worksheet.
This follows the EDGE of the line
The robot rides the edge — half on the black, half on the white — not the center. That’s why a small, steady wobble is normal. The sensor must be mounted facing down at the front, near the floor.
Quick troubleshooting
- Robot ignores the line / drifts straight off?
GAINis probably too low, or yourthresholdis wrong — re-measureBLACKandWHITEand recompute it. - Robot wobbles wildly?
GAINis too high — lower it. SlowingDRIVE_SPEEDhelps too. - It steers the wrong way? It’s following the other edge — start the robot on the other side of the line (or, in the stretch version, swap the two sensors).
- Program won’t stop?
while True:runs forever — use the editor’s Stop button. - Hub not in the Bluetooth list? Make sure it’s on and not already connected to another laptop (only one connection at a time), then refresh the page.