Skip to content
FlintMindsMiddle School
Middle School

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 are we 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.

The mechanism

How it works

  1. Perception

    Two sensors read whether the line is under the left or right side.

  2. Decision

    Your code compares both readings to figure out which way to correct.

  3. Action

    The motors turn at different speeds to steer back onto the line.

  4. Repeat

    This sense-decide-correct cycle runs continuously — that's closed-loop control.

What you'll pick up

  • 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

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.

Build it

  1. 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. 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. 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. 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. 5

    Upload the following code

    Connect your board to your computer and upload the code below.

  6. 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

arduino
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.

Does it work?

  1. 1Place the robot on a taped line and power it on.
  2. 2It should stay centered on the line, turning left or right as it drifts.
  3. 3Test on a straight section first, then try a gentle curve.

Make it yours

What can you change?

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.

If something's off

The usual causes, in the order they're worth checking.

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.

Do you know why 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 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.