Line Follower Robot: How PID Works

Why a simple line follower wobbles and how PID fixes it: the proportional, integral and derivative parts in plain words, the sensors, the track and how to tune it.

Published 4 min readBy the RoboHub team

An assembled 2WD robot car chassis: a clear plate, two yellow geared motors with wheels and a battery holder
The base of a line-following car: a 2WD chassis.
In this article

A line follower robot follows a black line drawn on the floor. It's one of robotics' classic exercises and a common school competition: who gets round the track fastest without leaving the line. A first version is easy to build, but the secret of a fast, steady robot is one word: PID.

The simple method and its problem

The simplest robot has two sensors, one on each side of the line. If the left sensor sees the line, the robot turns left; if the right one does, it turns right; if neither, it goes straight. It works (our line-following car is built exactly like that), but the robot zigzags all the way: it either turns fully or not at all. Speed it up and the zigzag grows until the robot loses the line in a bend.

What PID is

PID is a control method that works out, hundreds of times a second, how much the robot should turn, not just which way. First it needs the error: how far the line is from the robot's centre line. A bar of five sensors sees the line's position at nine levels: far left, a little left, in the middle and so on.

PID's steering is the sum of three parts:

P: proportional

P = Kp × error. The further away the line is, the harder the robot turns. With a small Kp the robot can't keep up with a bend; with a large one it starts to wobble. P alone often improves a lot.

D: derivative

D = Kd × (error now − error last time). D watches how fast the error is changing. As the robot swings back towards the line, D eases the turn early so it doesn't overshoot. D is what stops the wobble and lets you add speed.

I: integral

I = Ki × the sum of errors. If the robot keeps running slightly off to one side (one motor weaker than the other, say), I slowly grows and corrects it. On a line follower I is often zero or very small: a large Ki makes the robot unstable.

In the end: turn = P + I + D. The left motor gets the base speed plus the turn, the right motor the base speed minus the turn.

Sensors

  • Digital TCRT5000 modules: each one only says "black" or "white". Five in a row are enough for PID, because neighbouring sensors seeing the line together give the in-between positions.
  • An analogue sensor bar: each sensor measures brightness as a number, so the position is more precise. Fast competition robots often use 8 analogue sensors.

The sensors should sit a few millimetres above the floor and ahead of the wheels: the further ahead, the earlier the robot sees a bend.

The track

For practice the handiest option is a ready-made practice track (you'll find it among the line follower robot parts): unroll it on the floor and the line is always the same. For your own track, stick black electrical tape onto a light surface, keep the bends gentle and smooth out the creases. Competition tracks differ in line width, bends, crossings and gaps, so read the rules in advance.

How to tune PID

  1. Set Ki and Kd to zero and the speed low.
  2. Raise Kp a little at a time until the robot gets round the bends, even if it wobbles.
  3. Raise Kd until the wobble dies down.
  4. Raise the speed step by step and tune Kp and Kd again each time.
  5. Add Ki only if the robot keeps drifting to one side on the straights.

Tune with a fully charged battery and write down which values work: as the battery runs down, the robot behaves differently.

Speed or stability

At a competition the winner isn't the fastest robot but the one that gets round the track fastest without leaving the line. When you add speed, two things happen: the robot's momentum carries it wide in the bends, and the sensors gather less information in less time. So raise the speed step by step and retune PID each time. A light robot, a low centre of gravity, grippy tyres and sensors mounted well ahead of the wheels add more speed than simply stronger motors.

Common problems

  • It leaves the line in bends: slow it down or raise Kp.
  • It zigzags on straights: lower Kp or raise Kd.
  • It steers away from the line, not towards it: the sensor order or a motor's direction is mixed up.
  • It gets confused in sunlight: outside light disturbs infrared sensors. Shield the sensors from the sides.

Build one yourself

The full guide, with five sensors, a TB6612FNG driver, a pin table and ready PID code, is our PID line follower project. You'll find the parts under line follower robot.

Prices and stock change often, so articles leave them out: the product page always has them.

Questions and answers

How many sensors does a line follower need?

The simplest robot manages with two, but for PID five or more are better: the more sensors, the more precisely the robot knows where the line is.

What are Kp, Ki and Kd?

They are PID's three numbers: Kp sets how hard to turn for a given error, Kd calms the wobble, and Ki corrects a long-lasting drift. You find them by experiment.

How wide should the line be?

For practice, 15–20 mm black electrical tape works well. At a competition the line width is in the organiser's rules.

More articles

All articles