Skip to content

Line Following

Make the robot drive along a black line. Lines on the FLL mat connect mission models, making line following one of the most reliable navigation tools. Three versions, from "works" to "fast and stable".

Principle: follow the EDGE, not the line

The sensor reads ~10 on pure black, ~85 on pure white, and a middle value (~48) riding the edge of the line. Line following = steering to keep the reading at that middle value:

  • Reading too high (too white) → the sensor slid onto the white side, steer back toward the line
  • Reading too low (too black) → the sensor pushed into the line, steer back toward the white

Convention: we follow the left edge of the line - white field on the left of the boundary, black line on the right ("white-left, black-right"). So too white = the robot drifted LEFT and must steer right (positive steering); too black = drifted right, steer left (negative). Every example on this page uses this convention; for the right edge, flip every sign.

First complete threshold calibration to get your black, white and middle values.

Version 1: bang-bang (zigzag)

when program starts
init
forever
  if <color sensor [C] reflected light > [48]> then
    start moving [steering 20]         ← too white (drifted left), steer right back to the line
  else
    start moving [steering -20]        ← too black (drifted right), steer left back to the white

The robot wiggles forward. Works, but slow - raise the speed and it flies off. Good for understanding, not for competition.

Version 2: proportional (P) control - the FLL standard

Core idea: the bigger the error, the harder the correction; small error, gentle correction.

when program starts
init
set [target] to [48]
set [Kp] to [0.8]
forever
  set [error] to ((color sensor [C] reflected light) - (target))
  set [steer] to ((error) * (Kp))
  start moving [steering (steer)]
  • error = current reading − target. Too white → positive error → positive steering, correcting right; too black → negative, correcting left. Same directions as version 1.
  • Kp is the proportional gain - correction strength. If your build corrects the wrong way (sensor mounted differently, following the right edge), negate Kp.

Tuning Kp

  1. Start at 0.5, speed 30%.
  2. Sluggish, loses curves → raise Kp by 0.1-0.2.
  3. Rapid left-right shivering → lower Kp by 0.1-0.2.
  4. Once stable, raise speed gradually; higher speed usually wants slightly lower Kp.

Well tuned: barely wiggles on straights, follows curves smoothly.

Version 3: two-sensor line following

Two sensors straddle the line; their difference IS the error - naturally symmetric and lighting-resistant:

forever
  set [error] to ((color sensor [C] reflected light) - (color sensor [D] reflected light))
  start moving [steering ((error) * (Kp))]

Line centered = zero error. Drift left and the line shifts right under the robot, so the right sensor [D] darkens: D's reflection drops, error C − D goes positive, steering goes positive - correcting right, back onto the line. Symmetric when drifting right. If your build corrects the wrong way (mirrored wiring), negate Kp. A classic hardware-for-software trade: one more sensor, simpler and more robust program.

Stopping: junction detection

Endless line following is useless - stop at the right place. Common exit conditions:

repeat until <color sensor [E] reflected light < [30]>     ← a third sensor spots the crossing line
  ...line follow logic...
stop moving

Or "line follow + encoder": count motor degrees while following, stop after the distance - wrapping it as a follow (cm) My Block is ideal.

Common pitfalls

  • Speed and Kp are a pair: change the speed, retune Kp.
  • Choose the outside edge on curves: follow the edge on the outside of the bend to hold tight corners.
  • Starting pose matters: begin roughly on the edge; starting far off the line causes a violent first correction.
  • Want more (PID with integral and derivative)? See Python Advanced - a PID in Word Blocks takes too many blocks to be worth it.

Next lesson: Gyro Driving & Turns.