Skip to content
RoboLearn
Not completed

Line-following robot

What you’ll build

A robot that follows a black line on the floor using two IR sensors, built on a 2WD chassis. Then you’ll make it faster and smoother.

Parts

Part Qty Notes
2WD robot car chassis with 2 TT gear motors and wheels 1 Includes a caster
Arduino UNO (or compatible) 1
L298N or TB6612FNG motor driver 1
6 × AA battery holder with switch 1 About 9 V with alkalines
Jumper wires (male–male and male–female) ~20
IR line sensor modules (TCRT5000 type) 2
Black electrical tape and a light floor or card — For the track

Motor wiring

Driver pin Arduino pin Purpose
ENA 5 (PWM) Left motor speed
IN1 / IN2 7 / 8 Left motor direction
ENB 6 (PWM) Right motor speed
IN3 / IN4 9 / 10 Right motor direction
12V (VM) Battery + (through the switch) Motor power
GND Battery − and Arduino GND Shared ground
5V Arduino VIN is fed from the battery instead Leave the driver’s 5V output unused, or check your board’s jumper

Sensor wiring

Sensor pin Arduino
Left sensor DO A0 (used as a digital input)
Right sensor DO A1
VCC / GND 5V / GND

Mount the sensors under the front, about 3–8 mm above the floor, roughly 2–3 cm apart so the tape (about 2 cm wide) fits between them.

Build steps

  1. Assemble the chassis, motors, wheels and caster (chassis lesson).
  2. Mount the Arduino, driver and battery; wire the motors (table above).
  3. Test motors alone with wheels off the ground (Examples tab, step 1). Fix any motor that spins backwards by swapping its two wires.
  4. Mount and wire the sensors. Test sensors alone (step 2): note what each reads over white and over black.
  5. Upload the line follower (step 3), set it on the line and switch on.
  6. Tune speeds until it completes the track reliably.

If it doesn’t work

Symptom Check
Spins in circles One motor wired backwards
Drives straight off the line Sensor logic inverted: swap HIGH/LOW in onLine()
Wobbles wildly Lower the speeds; turn less sharply
Loses the line on tight curves Lower the base speed, or move the sensors further forward
Arduino resets Batteries low, or motors and logic sharing a weak supply

Finished this lesson?