Line-Following Robot
Build a robot that steers itself by continuously reading sensors and following a taped line.
About 1 hr 30 min
0 of 5 steps done
What are we making?
A line-following robot reads sensors pointed at the ground and steers its wheels based on whether it's drifting off the line.
It's a hands-on introduction to closed-loop control: sense, decide, correct, repeat.
What you need
Microcontroller board × 1
Reads the line sensors and controls both drive motors.
IR line sensors × 2
Detect whether the surface beneath them is light or dark to find the line's edge.
DC motors with wheels × 2
Drive the robot forward and let it steer by spinning at different speeds.
Motor driver board × 1
Lets the microcontroller control motors that need more power than its pins can supply directly.
Chassis and battery pack × 1 each
Provides the frame and mobile power source for the robot.
Let's build it
- 1
Assemble the chassis
Mount the motors and wheels onto the chassis, then attach the battery pack.
Chassis assembly diagram
Not drawn yet. We only publish a diagram once someone has built this with real parts and checked it — so rather than guess at the connections, we are telling you they are missing. The written wiring notes are accurate.
- 2
Mount the line sensors at the front
Position both sensors facing down, close to the surface, near the front of the chassis.
Sensor mounting diagram
Not drawn yet. We only publish a diagram once someone has built this with real parts and checked it — so rather than guess at the connections, we are telling you they are missing. The written wiring notes are accurate.
- 3
Connect the motor driver
Wire the motor driver between your board and the two drive motors, following its datasheet.
Motor driver wiring diagram
Not drawn yet. We only publish a diagram once someone has built this with real parts and checked it — so rather than guess at the connections, we are telling you they are missing. The written wiring notes are accurate.
- 4
Wire the sensors and motors
Connect each sensor's output and the motor driver's control pins to your board.
Sensor and motor wiring diagram
Not drawn yet. We only publish a diagram once someone has built this with real parts and checked it — so rather than guess at the connections, we are telling you they are missing. The written wiring notes are accurate.
- 5
Upload the following code
Connect your board to your computer and upload the code below.
- 6
Tune your sensor threshold on the actual line
Place the robot on your line and adjust lineThreshold until it reliably detects it.
How it all connects
Line-Following Robot wiring diagram
Not drawn yet. We only publish a diagram once someone has built this with real parts and checked it — so rather than guess at the connections, we are telling you they are missing. The written wiring notes are accurate.
The two line sensors' power/ground connect to 5V/GND, with each sensor's output on its own digital or analog pin. The motor driver's inputs connect to digital pins on your board, and its outputs connect to the two drive motors — match all pins in your code below, and always power motors from a source your motor driver is rated for.
The code
1const int leftSensorPin = A0; // update to match your wiring2const int rightSensorPin = A1; // update to match your wiring3const int lineThreshold = 500; // update after testing on your line4 5void setup() {6 // Set up your motor driver's pins here, following its datasheet.7}8 9void loop() {10 int leftReading = analogRead(leftSensorPin);11 int rightReading = analogRead(rightSensorPin);12 13 bool leftOnLine = leftReading > lineThreshold;14 bool rightOnLine = rightReading > lineThreshold;15 16 if (leftOnLine && rightOnLine) {17 driveForward(); // defined by your motor driver's control logic18 } else if (leftOnLine) {19 turnLeft();20 } else if (rightOnLine) {21 turnRight();22 }23}24 - Line 3: lineThreshold is the sensor value that counts as "on the line" — tune it by testing on your actual line and surface.
- Line 6: Motor driver wiring and control code vary by board — follow your specific driver's datasheet here rather than a generic example.
Try it!
- 1Place the robot on a taped line and power it on.
- 2It should stay centered on the line, turning left or right as it drifts.
- 3Test on a straight section first, then try a gentle curve.
Not working yet?
Nothing works the first time for everyone. Here's what to check.
- The robot drives off the line immediately.
- Retest lineThreshold — surface and lighting affect sensor readings, so a value that worked elsewhere may not work here.
- The robot doesn't move at all.
- Confirm your motor driver is wired and powered correctly, separate from the sensor and logic wiring.
- The robot overcorrects and zigzags.
- Try slowing down your turn speed or adjusting how far apart the sensors sit.
Try changing it
Handle intersections
Add logic so the robot can cross a perpendicular line without losing its path.
Add proportional correction
Adjust turn sharpness based on how far off the line the robot has drifted.
Stop at a finish line
Detect a marked finish line and stop the robot when it reaches it.
Do you know how it works?
1. How does the robot steer using two motors?
2. What does "closed-loop control" mean in this project?
3. Why does the robot need two line sensors instead of one?
What should you make next?
You just learned how to:
- Reading and comparing multiple sensors
- Closed-loop sensing and correction
- Differential-drive steering
- Assembling a mobile robot chassis
- Using conditional (if/else) logic
Smart Home
Build a scaled-down smart home model that combines sensors, status indicators, and an automated entry barrier into one coordinated system.