Skip to content

Robotics

How to tune a line follower's proportional steering

To make a line follower steer smoothly, steer by how far it's off the line, not just which side. Subtract the two sensor readings, diff = right - left, and move each wheel away from a base speed in proportion: the left wheel slower and the right faster when the line drifts left, and more the further it drifts. A small drift gives a gentle lean, a big one a hard swing, and the zigzag disappears.

Why on-off rules zigzag

The classic follower has three rules: line under the left eye, stop the left wheel; under the right eye, stop the right; otherwise straight. Every correction is all or nothing, so the robot swings hard, crosses the line, swings hard back. It works, slowly and wobbly, and on a fast curve it falls off.

How far, not which side

Reflectance sensors read about 937 over the floor, 633 half over the line, and 251 right over it. The difference between the eyes measures how far off centre the robot is:

  • Centred, both on floor: diff = 0
  • Left eye half over the line: 937 − 633 = 304
  • Left eye fully over: 937 − 251 = 686

A positive diff means the line is under the left eye; a negative one, under the right.

Steer in proportion

left = 180 − diff ÷ 8 right = 180 + diff ÷ 8
int diff = analogRead(A1) - analogRead(A0);   // right minus left
analogWrite(9,  constrain(180 - diff / 8, 0, 255));
analogWrite(10, constrain(180 + diff / 8, 0, 255));

Half over the line, 304 ÷ 8 is 38: left 142, right 218, a lean. Fully over, 686 ÷ 8 is 85: left 95, right 265, which constrain holds at 255, a swing. Centred, both 180.

Live experiment

Rules against proportion

The rover runs the same line four ways. The three on-off rules slam a wheel between 0 and 255 and lurch dozens of times. diff ÷ 8 glides the whole line without one. ÷ 2 steers four times as hard and still copes. ÷ 32 is too gentle to lean into the first bend and loses the line.

Try it yourself: free lesson

“Steer in proportion” is lesson 1 of 6 in Encoder Edge, Robotics World 2. Free with an account. Lessons unlock in order, so you’ll do the 7 before it first.

Choosing the gain

The 8 sets how hard it steers. Divide by 2 and half a line becomes 152: left 28, nearly stopped, and the lurching comes back. Divide by 32 and it's only 9, too gentle to get round a bend. Tune it on the floor: start gentle, and make the divisor smaller until it holds the line on the tightest bend without weaving on the straights.

This is proportional control, the P in PID, with the gain written as a divisor. Race-level followers add more sensors for a finer position, and a D term that steers into a bend before the error grows.

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

“Steer in proportion” is lesson 1 of 6 in Encoder Edge, Robotics World 2. Free with an account. Lessons unlock in order, so you’ll do the 7 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 3With diff 304 and gain 8, the wheels are…
    Show the answer

    Answer: Left 142, right 218. 304 ÷ 8 is 38. 180 − 38 is 142 for the left wheel, 180 + 38 is 218 for the right: a gentle lean, not a lurch.

    • Why not “Left 0, right 255”? That's the all-or-nothing rule. Proportional steering moves each wheel by only diff ÷ 8, which is 38 here.
    • Why not “Both 180”? Both at 180 is for a diff of 0. Here diff is 304, so the wheels move 38 either side of 180.
    • Why not “Left 218, right 142”? That's backwards. A positive diff means the line is under the left eye, so the left wheel slows to lean left.
  2. Question 2 of 3Both eyes read 937: diff is 0. The wheels are…
    Show the answer

    Answer: Both 180: straight ahead. Zero difference means centred, so neither wheel moves away from the base speed.

    • Why not “Both 255”? The base speed is 180. A diff of 0 adds nothing to it.
    • Why not “Left 0, right 255”? A hard swing is the old rules. With diff 0 there's nothing to correct.
    • Why not “Both 0”? Zero difference means centred, not stopped. The wheels stay at the base speed.
  3. Question 3 of 3Change the gain from 8 to 2. Half over the line, diff 304, the left wheel is…
    Show the answer

    Answer: 28: nearly stopped, a lurch again. 304 ÷ 2 is 152, and 180 − 152 is 28. A small drift becomes a hard swing: too much gain brings the zigzag back.

    • Why not “255”? The left wheel slows when the line is under the left eye; 255 would speed it up.
    • Why not “180”? 180 is the base speed, for no drift. A diff of 304 changes it.
    • Why not “142”? 142 is the left wheel with gain 8. With 2 the correction is 152, not 38.
Keep practising: free lesson

“Steer in proportion” is lesson 1 of 6 in Encoder Edge, Robotics World 2. Free with an account. Lessons unlock in order, so you’ll do the 7 before it first.

Questions people ask

What does the 8 in diff ÷ 8 mean?

It sets the gain: how much each wheel speeds up or slows down per count of difference between the eyes. Dividing by 8 is a gain of one eighth. A smaller divisor steers harder; a bigger one steers more gently.

Why does my line follower lose the line on bends?

The steering is too gentle for the speed. On a bend the line moves sideways under the eyes faster than a weak correction can follow. Steer harder by dividing by a smaller number, or lower the base speed.

How many sensors does proportional steering need?

Two is enough: their difference says which way and roughly how far. Five to eight in a row give a much better estimate of where the line is, so racing line followers use arrays and add a D term to steer early into bends.

Every topic