Track: Robots & Electronics · Lesson 6 of 10 · Ages: 7+ · Time: 40 minutes
Imagine a path on the floor, and a little robot that can follow it all by itself, round every bend. It does not have a map, and it does not have eyes like yours. All it has is two light sensors and a few simple rules. Let us find out how it works!
Beep says Line following is one of my favourite tricks. I look down at the floor, and I keep the line right in the middle of my body. If the line wanders off, I steer after it. Beep boop!
What you need
- A big sheet of paper and a thick dark felt-tip pen.
- A small piece of card, about 5 cm by 7 cm, cut by a grown-up.
- A pencil, and about 40 minutes.
Grown-ups No electronics in this lesson. Real line-following robots use small sensors that shine a little light at the floor and measure how much comes back. Dark surfaces reflect less light than pale ones, which is how the robot tells a black line from a white floor. The paper robot below lets your child act out the rules before any hardware is involved.
How a robot sees a line
A line-following robot has two light sensors pointing down at the floor, one on the left and one on the right. They are spaced just wider apart than the line. When everything is going well, the line runs between the sensors, and both sensors see the pale floor.
Each sensor gives a number for how bright the floor is. A dark line gives a low number. A pale floor gives a high number.

The two red squares are the sensors. The line is in the middle, so both of them see white floor.
The four rules
What if the robot drifts, and the line moves under one of the sensors? Then the robot must steer back. Here are the rules.
| Left sensor sees | Right sensor sees | What it means | What the robot does |
|---|---|---|---|
| Floor | Floor | The line is between the sensors | Go forward |
| Line | Floor | The line has drifted to the left | Turn left |
| Floor | Line | The line has drifted to the right | Turn right |
| Line | Line | Both sensors are on dark, maybe a finish line | Stop |
If the left sensor sees the line, the line has moved to the left, so the robot turns left to put it back in the middle. It is a chase, and the robot keeps catching up with the line, many times every second.
Play the paper robot
- With the felt-tip pen, draw a thick, curving line on the big sheet of paper, as wide as your thumb.
- On your card, draw two dots near the front edge, a little wider apart than the line. The dots are the sensors.
- Put the card at the start of the line, with the line between the dots.
- Slide the card forward a little. After each move, look at the dots and ask: which dots are on the line?
- Follow the four rules. If the left dot is on the line, turn the card a bit left. If the right dot is, turn it a bit right.
Can you get all the way to the end without leaving the line? It gets easier with practice, and with tiny moves.
The rules in Python
Here is a list of pretend readings from the two sensors, as pairs of numbers: the left one, then the right one. Anything below 30 counts as dark.
readings = [(80, 80), (75, 20), (85, 85), (15, 80), (10, 10)]
for left, right in readings:
left_on_line = left < 30
right_on_line = right < 30
if left_on_line and right_on_line:
action = "stop"
elif left_on_line:
action = "turn left"
elif right_on_line:
action = "turn right"
else:
action = "go forward"
print(left, right, action)
80 80 go forward 75 20 turn right 85 85 go forward 15 80 turn left 10 10 stop
The lines left_on_line = left < 30 store True or False. A boolean is a value that is either true or false, and it is perfect for decisions. Follow the output: the second pair, 75 and 20, means the right sensor is on the line, so the robot turns right.
Pixel says The order of the rules matters! The “both on the line” check comes first, because otherwise the robot would see “left on the line” and turn left, without noticing it should stop. Meow!
Did you know? Line-following robots are real workers. Some factories and warehouses use robots that follow lines or tapes on the floor to carry boxes around.
Challenges
Try it Change readings to [(90, 10), (10, 90), (50, 50)]. Before you run it, write down what the robot will do for each pair.
Show the answer
In the first pair, only the right sensor (10) is below 30, so the robot turns right. In the second, only the left sensor (10) is below 30, so it turns left. In the third, 50 is not below 30 for either sensor, so it goes forward.
readings = [(90, 10), (10, 90), (50, 50)]
for left, right in readings:
left_on_line = left < 30
right_on_line = right < 30
if left_on_line and right_on_line:
action = "stop"
elif left_on_line:
action = "turn left"
elif right_on_line:
action = "turn right"
else:
action = "go forward"
print(left, right, action)
90 10 turn right 10 90 turn left 50 50 go forward
Try it What should the robot do if the line suddenly disappears and both sensors see only floor for a long time? Make up a new rule to help it find the line again.
What you learned
- A line-following robot has two light sensors that look down at the floor, with the line in between.
- If one sensor sees the line, the robot steers towards that side to bring the line back to the middle.
- A boolean is a value that is
TrueorFalse, and programs use booleans to decide. - The order of the rules in a program can change what the robot does.
