Lessons and the learning game

Build a line-following racer

A line-following racer as real control engineering: a five-sensor array, line position in millimeters, P then PD steering, grip and lap timing.

What it teaches

Seven lessons in three chapters, for a learner who knows the Uno and the Serial Monitor:

  • See the line: how a TCRT5000 and a comparator turn black tape into a high pin, why 19 mm tape suits 15 mm spacing, the five channels as one number, and the line's position as a weighted average in millimeters, with its nine steps and its 37 mm reach.
  • Steer: differential drive through a TB6612FNG (forward, spin, short brake, coast), then a P controller, saturation at the PWM clamp, and a D term that kicks on a sudden error and brakes the turn on the way back.
  • Race: base speed against the grip formula v = sqrt(mu x g x r), slowing in the bends, a tired pack, laps timed to the hundredth with the distance from the wheel encoder, and the project: a three lap race with your own gains and a failsafe.

Open it at /learn/path/build-line-follower-racer.

The circuits

  • Line-following cart (gallery) running lfrsense: the tracker read on its own with the motors off. It prints the pattern (S1-S5: 00110) and the position (position 7.5 mm) each time the tape moves.
  • TB6612 robot drive (gallery) running tbdrive: two gearbox motors on four AA cells, through forward, half speed, a spin, reverse, a brake and standby.
  • Line-following racer: the cart on a 2WD chassis, running lfrrace. The tracker's S1 to S5 are on A4 to A0, the TB6612 on pins 2 and 4 to 9, the left wheel's encoder on pin 3, and a 2S 18650 pack feeds the motors. Type kp 3, kd 6, base 120, slow 0, stop or go to tune it live.

You are the track

The chassis does not drive over a drawn track, so the tape moves instead. Click the tracker and slide it: 100 is the tape under the middle sensor, 115 is 15 mm to the right, 0 is no line, and the swatch lays a bar across for the start and finish line. Every number the racer prints follows from where you put the tape.

Troubleshooting

  • The wheels do not answer the tape: the racer prints only when the wheel commands change, so slide the tape to a new place.
  • A lap is not counted: bars closer than one second apart count as one. Put the tape back to the middle between bars.
  • The left wheel is clamped at 180: that is MAX_SPEED, the most a 6 V motor should see from an 8.4 V pack.
  • The racer spins in place: it lost the line and is searching toward where it last saw it. Slide the tape back under the tracker, or set LOST_STOP_MS so it stops instead.