TL;DR:
PID turns an error (how far off the line you are) into a correction using three terms: present error, accumulated error and rate of change.
P reacts to the present, I removes a lingering offset, and D damps the swing by reacting to how fast the error changes.
Too much gain makes the robot wobble. Start with P alone, then add D, then a little I.
A line-following robot has one job: stay on a black line. If it steers hard every time it drifts, it zig-zags. If it steers gently, it drifts off at a bend. The trick is to steer by an amount that depends on how far off it is, how long it has been off, and how quickly things are changing. That is what a PID controller does, and a line follower is the friendliest place to learn it.
The error signal
Start with a row of five reflectance sensors under the robot, at positions −2, −1, 0, +1 and +2 (in units of sensor spacing, with 0 in the center and + to the right). Each sensor reads 1 when it sees the dark line and 0 when it sees the floor.
The robot's line position is the average of the positions of the sensors that see the line. The setpoint is 0 (line under the center). The error is how far the line is from where it should be:
error = line position − setpoint
If sensors at +1 and +2 see the line, the position is (1 + 2) / 2 = +1.5. The line is to the right, the error is +1.5, and the robot must turn right. If no sensor sees the line, you need a rule, such as to remember the last error's sign and keep turning that way.
The three terms
The controller adds up three corrections:
P (proportional).
Kp × error. The bigger the error, the bigger the steering correction. It is the main steering action. With P alone, the robot tends to overshoot, swinging past the line and back.I (integral).
Ki × (sum of errors over time). If the error stays on one side for a while, the sum grows and pushes harder. It removes a steady offset, for example a motor that is slightly weaker than the other.D (derivative).
Kd × (error − previous error). It reacts to how quickly the error is changing. If the robot is already swinging back toward the line fast, D reduces the correction so it does not overshoot. It acts like a brake on the swing.
Together: correction = Kp·e + Ki·Σe + Kd·(e − e_prev). This is the discrete form you use in code, run once per loop.
Worked example: correcting drift off a line
Take a loop that runs every 10 ms. The base speed of each wheel is 150 (on a 0 to 255 PWM scale). The gains are Kp = 30, Ki = 2 and Kd = 10, example values that you would have to tune on a real robot.
The last loop's error was e_prev = 1.0. This loop the sensors at +1 and +2 see the line, so e = 1.5. The running sum of past errors (including this one) is Σe = 4.5.
P term.
30 × 1.5 = 45.I term.
2 × 4.5 = 9.D term.
10 × (1.5 − 1.0) = 5.Correction.
45 + 9 + 5 = 59.Wheel commands. The line is to the right, so turn right: the left wheel speeds up and the right wheel slows down.
left = 150 + 59 = 209andright = 150 − 59 = 91. Both are inside 0 to 255.
Check the sign: the left wheel is faster than the right, which turns the robot clockwise, to the right, toward the line. Good.
Now suppose the next loop the error has dropped to 0.5, because the turn worked. P becomes 15, D becomes 10 × (0.5 − 1.5) = −10, so D now pulls against the correction because the robot is returning fast. That negative D is the damping at work.
Where it goes wrong
Gain too high. The robot oscillates, left and right, with growing swings. Lower Kp.
Gain too low. The robot is sluggish and runs off at curves.
Integral windup. If the robot is lost for a while, the integral term keeps growing and then overshoots wildly when the line returns. Clamp the sum to a limit, and reset it when the line is lost.
Noise in D. The derivative magnifies sensor jitter. Smooth the readings, or the derivative.
Saturation. If the correction pushes a wheel above 255 or below 0, clamp it, and remember the robot cannot turn harder than that.
Speed. A gain that works at slow speed may oscillate at a higher speed, since the robot covers more distance per loop.
Safety: Test with the robot on a stand or with the wheels lifted off the table until the sign of the correction is right. A reversed sign makes the robot steer away from the line and accelerate off the table. Keep a power switch within reach and keep fingers out of the wheels.
How real robots use PID
PID is the workhorse of control: wheel speed, motor position, drone attitude, temperature and balancing robots all use it, often with one controller per quantity. The line follower is simple because the plant (the robot) responds fairly directly. The same math applies elsewhere, but the gains differ. The glossary entry on the PID controller has the short definition, and the next article, tuning a PID controller step by step, shows a method for picking the gains.
Try it yourself
Draw a line on white paper and use a robot with the five sensors. Set Ki and Kd to zero and raise Kp until it follows gentle curves but starts to wobble. Back off a bit. Then add a small Kd and see the wobble calm down. Only add Ki if the robot consistently sits to one side of the line.
Frequently asked questions
Do I need all three terms?
Not always. Many line followers work with PD alone. Add I only if you see a steady offset that P and D cannot fix.
Why does the robot wobble when I raise Kp?
Higher gain means a stronger correction for each error, so it overshoots the line. Each overshoot makes an opposite error, and with enough gain the swings grow.
