How to make a line-following robot in one afternoon

Asked 5 days ago Modified 3 days ago Viewed 2 times
How to make a line-following robot in one afternoon

Project overview: a two-wheel-drive robot that follows a black line on a white surface using two IR reflectance sensors and proportional control on the motors.

Parts list

  • Arduino UNO
  • L298N dual H-bridge motor driver
  • 2WD chassis kit (2 yellow gear motors + 2 wheels + 1 caster)
  • 2 × TCRT5000 IR line sensors (digital out is fine)
  • 4 × AA battery holder + batteries (or a 7.4V LiPo)
  • A roll of black electrical tape for the track

Wiring summary

  • Left sensor OUT → D2, Right sensor OUT → D3
  • L298N IN1/IN2 → D5/D6 (left motor), IN3/IN4 → D9/D10 (right motor)
  • L298N ENA/ENB → 5V (or PWM pins if you want speed control)
  • Battery + → L298N 12V terminal, battery − → L298N GND and Arduino GND (common ground!)

Control logic

  • Both sensors see white → both motors forward (going straight on the line).
  • Left sensor sees black → robot is drifting right, so slow the left motor / speed up the right.
  • Right sensor sees black → mirror of the above.
  • Both sensors see black → you've hit a junction; you can stop, beep, or go straight.

Tuning the speed (the hard part)

  • Start with PWM = 120. Too fast and the robot overshoots corners; too slow and it stalls on the caster.
  • Work down to about 90 for tight turns. Raise the inner-wheel PWM gradually for sweeping turns.
  • Tape height matters — lift the sensors to about 5-8mm above the surface for clean readings.

Track tips: lay smooth curves with no acute angles, keep the line at least 15mm wide, and avoid glossy floors that reflect light unevenly. Once you've got it working, the next upgrade is swapping the digital sensors for an analog 5-channel array and switching to a real PID loop.