Skip to content

Robotics

How a line follower robot works

A line follower robot uses two downward-facing light sensors, one each side of a dark line. A pale floor reflects lots of light and the line very little, so each sensor's reading drops when it's over the line. If the left sensor sees the line, the robot turns left; if the right one does, it turns right; if neither does, it's centred and drives straight.

Eyes on the floor

Each "eye" is a reflectance sensor: an infrared LED shining down and a phototransistor beside it measuring how much comes back. Read through analogRead, a typical pair gives about 937 over a white floor and about 250 right over a black line, with values in between at the line's edge.

A threshold turns a reading into a yes

The robot needs a yes or no: line, or floor? Put a threshold between the two readings. Halfway is about 594; 512 works too, with room either side for a dusty floor or a faded line. Put it at 950 and even the floor counts as line; put it at 100 and the line never does.

Live experiment

Pick a threshold, follow the loop

Each eye reads about 937 over the floor and 250 over the line. Pick a threshold and drive the loop: 512 or 800 follow it all the way round; 100 or 300 sit too close to the line's reading and run off; 950 is above even the floor, so the rover spins for ever.

Try it yourself: free lesson

“Follow the line” is lesson 2 of 6 in Rover Ridge, Robotics World 1. Free with an account. Lessons unlock in order, so you’ll do the one before it first.

Three rules

With the left eye on A0, the right on A1, and the left and right wheels on pins 9 and 10:

void loop() {
  int left = analogRead(A0);
  int right = analogRead(A1);
  if (left < 512) {           // line under the left eye: swing left
    analogWrite(9, 0);
    analogWrite(10, 255);
  } else if (right < 512) {   // line under the right eye: swing right
    analogWrite(9, 255);
    analogWrite(10, 0);
  } else {                    // centred: straight on
    analogWrite(9, 255);
    analogWrite(10, 255);
  }
}

To swing left, stop the left wheel and run the right: the rover pivots round its stopped wheel and its nose swings toward the line.

Smoother: steer in proportion

The three rules work, but every correction is a lurch, so the robot zigzags. Better: steer by how far off the line it is. The difference between the eyes says which way and how much:

int diff = right - left;   // positive: the line is under the left eye
analogWrite(9, constrain(180 - diff / 8, 0, 255));
analogWrite(10, constrain(180 + diff / 8, 0, 255));

Half over the line, a difference of about 300 slows the left wheel and speeds the right by about 38: a gentle lean, not a pivot. That's proportional control, the P in PID. Divide by too little and the zigzag comes back; by too much and the robot can't turn hard enough for a bend. Racing line followers add more sensors and a D term, and lap tracks at several metres a second.

Free lesson

Now build it yourself

Each lesson comes in three short parts: see the idea, try it on a live board, then build it yourself, with quick checks as you go.

Try it yourself: free lesson

“Follow the line” is lesson 2 of 6 in Rover Ridge, Robotics World 1. Free with an account. Lessons unlock in order, so you’ll do the one before it first.

Or start from the very first lesson, no account needed

Practice questions

Three quick questions from the lessons. Pick an answer to check it.

  1. Question 1 of 3The floor reads about 937 and the line about 250. A good threshold is…
    Show the answer

    Answer: About 512, between them. The threshold has to sit between the two readings so the floor lands above it and the line below. 512 sits comfortably between them, with room either side for a dusty floor or a faded line.

    • Why not “Below 250”? Below 250, even the line would read as floor, so the rover would never see it.
    • Why not “Exactly 937”? 937 is the floor's own reading, so any floor that reads a touch lower would count as line.
    • Why not “Above 937”? Above 937, the floor itself would count as line, and the rover would turn for ever.
  2. Question 2 of 3The left eye sees the line. To get back on it the rover should…
    Show the answer

    Answer: Swing left: stop the left wheel, run the right. The line has drifted to the left of centre, so the rover must turn left to bring it back under the middle. Stopping the left wheel while the right runs does exactly that.

    • Why not “Swing right”? Swinging right moves the rover away from a line that has drifted to the left.
    • Why not “Stop”? Stopping doesn't bring the line back under the middle; the rover has to turn toward it.
    • Why not “Go straight”? Going straight leaves the line drifting further left until it's lost.
  3. Question 3 of 3Both eyes see floor. The rover…
    Show the answer

    Answer: Drives straight. No line under either eye means it's centred: both wheels on.

    • Why not “Swings right”? Swinging right is for when the right eye sees the line, which it doesn't here.
    • Why not “Swings left”? Swinging left is for when the left eye sees the line. With both on floor, the line is centred between them.
    • Why not “Stops”? Floor under both eyes means the rover is centred on the line, so it should keep going.
Keep practising: free lesson

“Follow the line” is lesson 2 of 6 in Rover Ridge, Robotics World 1. Free with an account. Lessons unlock in order, so you’ll do the one before it first.

Questions people ask

What sensors does a line follower robot use?

Usually infrared reflectance sensors, such as the TCRT5000 or QRE1113: an IR LED and a phototransistor side by side, pointing at the floor. A pale floor reflects lots of infrared and a dark line very little, so the reading changes as the sensor crosses the line.

How many sensors does a line follower need?

Two is the minimum for steering both ways, one each side of the line. Racing line followers use five to eight in a row, which tells them how far off the line they are, so they can steer smoothly and take corners at speed.

Why does my line follower zigzag?

All-or-nothing rules make it swing hard one way, overshoot the line and swing hard back. Steer in proportion to how far off it is instead, and turn the gain down until it glides; slowing down helps too.

Every topic