High School
Line-Following Robot
Build a robot that steers itself by continuously reading sensors and following a taped line.
- Level
- Inventor
- Time
- 1 hr 30 min
- Parts
- 5
- Hardware-checked
- Not yet
0 of 5 build milestones complete
What you're building
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'll learn
- Understand differential steering: driving two wheels at different speeds to turn
- Read and compare multiple sensors at once
- Apply closed-loop logic: sense an error, then correct for it
- Assemble and wire a small mobile robot chassis
Parts you'll 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.
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.
Wiring
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.
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.
Understand
Why this works
Perception
Two sensors read whether the line is under the left or right side.
Decision
Your code compares both readings to figure out which way to correct.
Action
The motors turn at different speeds to steer back onto the line.
Repeat
This sense-decide-correct cycle runs continuously — that's closed-loop control.
Troubleshooting
Common problems and 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.
Take it further
Small change
Handle intersections
Add logic so the robot can cross a perpendicular line without losing its path.
Add something
Add proportional correction
Adjust turn sharpness based on how far off the line the robot has drifted.
Design it yourself
Stop at a finish line
Detect a marked finish line and stop the robot when it reaches it.
You decide how it works. No steps for this one.
Check your understanding
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 to build next
You now know:
- Reading and comparing multiple sensors
- Closed-loop sensing and correction
- Differential-drive steering
- Assembling a mobile robot chassis
- Using conditional (if/else) logic
What this leads to
Smart Home
Line-Following Robot leads straight into this one.