Explainer #104 · Electronics & Computers
What is PID control?
A PID controller looks at one number, the error between what you want and what you measure, and acts on it in three ways: P looks at how big it is now, I at how long it has lasted, and D at how fast it is changing. A car's cruise control on a hill shows all three.
The principle
PID control. The controller compares the setpoint with the measured value. The difference is the error e. The output is u = Kp·e + Ki·∫e dt + Kd·de/dt (plus a bias), and it drives the actuator, for example the throttle.
Step 1: Everything starts with the error. A cruise control set to 80 km/h measures the speed with a sensor and subtracts it from the setpoint. Our P is set to 6% throttle per km/h of error. The difference is the error: 80 minus 76.5, or 3.5 km/h. All three terms work on that one error: now, past, trend.
Step 2: P reacts to the error now. The proportional term is the gain times the error. In the animation, Kp is 6% of throttle for each km/h of error. That 6% is not a limit on how close we can get; it is simply how much extra throttle P adds for each km/h of error. On the flat, a base throttle of 25% already holds 80 km/h. The 6% hill needs 21% more just to hold speed. P can only give that extra throttle while there is an error, so the speed falls until P’s extra matches the 21% the hill needs:
| km/h too slow | P adds |
|---|---|
| 1 | 6% |
| 2 | 12% |
| 3 | 18% |
| 3.5 | 21% ← P gives = hill needs (21%) |
That is why the car settles 3.5 km/h too slow (76.5 instead of 80). That leftover gap is the steady-state offset. The 6% is not a cap: with P set to 12% per km/h, the car settles only 1.8 km/h too slow (from the same simulation; not exactly half, because the extra throttle needed is a little higher at the higher speed). A smaller offset, but never zero. If the error were zero, P would add no extra and the car would slow again. Too much gain makes the loop oscillate.
Step 3: I adds up the error over time. The integral term adds up the error over time, like a running total. The I term remembers the 21% extra the hill needed, and takes over that extra throttle. Once I is holding it, P no longer needs an error of 3.5 km/h to supply it, so the error falls to zero and the offset disappears. The price is overshoot: after the hill that stored throttle is still pushing, so the car speeds up to about 83 km/h before it settles. Too much integral gain makes the loop oscillate.
Step 4: D reacts to how fast it changes. The derivative term reacts to how fast the speed is changing. It starts opening the throttle as soon as the speed begins to fall, and it brakes the recovery, so the dip and the overshoot are smaller (in the simulation both the dip and the overshoot drop from about 3.0 to 1.4 km/h). It also amplifies measurement noise, so it is often filtered, taken from the measurement rather than the error, or left out.
Step 5: Tuning the gains. Gains too low give a slow, sloppy loop. Well-tuned gains give a quick, calm response. Gains too high make the loop hunt back and forth and can make it unstable. Common starting points are manual tuning and the Ziegler-Nichols method.
Step 6: One job for each term. Each of the three terms has one job. The present error is handled by P, which pushes back in proportion. A leftover offset is handled by I, which keeps adding until the steady error is gone. Fast changes and overshoot are handled by D, which brakes early.
Step 7: Windup, and where PID is used. Real controllers limit the output and use anti-windup so the integral does not keep growing while the output is at its limit. In the animation, a climb too steep for full throttle winds the integral up so far that the car shoots to about 104 km/h afterwards; with anti-windup it peaks near 85. The same loop holds home heating, drones, and factory temperatures, flows and levels. Most industrial loops are PI; D is used less often.
Common myth
More gain always means better control
Higher gain reacts faster, but past a point the loop overshoots, oscillates and can become unstable. The sensor, the actuator speed and the dead time of the process set how much gain you can use.How this video was made: Animated drawing driven by a real simulation: a 1500 kg car with drag and an engine that responds with a lag of 0.8 s, a 6 percent hill from 10 s to 40 s, a cruise setpoint of 80 km/h, and PID gains Kp 0.06 (6% throttle per km/h of error), Ki 0.02, Kd 0.5 with the derivative taken from the measured speed. A 25% base throttle holds 80 km/h on the flat; on the hill the P-only run settles where the 21% extra throttle P can supply equals Kp × 3.5 km/h. Doubling Kp to 12% per km/h cuts the offset to 1.8 km/h, not exactly half, because drag is a little higher at the higher settled speed (P-only, PI and PID runs share the same Kp; the tuning panel scales the gains down, and raises Ki to 0.25 for the hunting case). The windup example uses a 20 s climb steeper than the engine can hold. The hill is drawn steeper than 6 percent so it is visible, and the playback of the simulation is sped up. Real vehicles have gear changes, nonlinear engine maps and more.
Where the facts come from
- Wikipedia: Proportional-integral-derivative controller (cruise control example, terms, tuning, noise in the derivative term)
- Wikipedia: Integral windup
- Control Tutorials (CTM): PID tutorial (effects of P, I and D on rise time, overshoot and steady-state error)
- ISA InTech: Fundamentals of PID control (derivative from the measurement, noise, PI is most common)