PID Control
Add a term that remembers and a term that predicts, and the stubborn heater becomes obedient. Meet the algorithm running most of industry.
Builds on: 14.1 Closing the Loop13.3 Filters in Software
Three terms, three tenses
The fix for proportional control’s failures is to let the controller consider more than the present moment:
- P — the present. The muscle. Reacts to the error that exists right now.
- I — the past. The grudge-keeper: it accumulates error over time. Any persistent offset makes the integral grow until the offset is gone — this term kills the P-controller’s permanent shortfall. (In code: two lines — an accumulator and a clamp, the clamp being “anti-windup” so a long saturation doesn’t store a mountain of pent-up push.)
- D — the future. The damper: it reacts to how fast the error is changing, braking the approach before overshoot happens — the same job damping resistance did for your ringing LC tank. Its weakness: derivatives amplify noise, which is why real D-terms are filtered (Lesson 13.3, reporting for duty) and why many industrial loops run PI only.
Tuning: engineering as negotiation
Choosing Kp, Ki, Kd is a genuine craft. The practical amateur recipe: raise Kp until the response wobbles, back off a third; add Ki until the offset dies in reasonable time; add a pinch of Kd if overshoot needs taming. Formal methods exist (Ziegler–Nichols, from 1942, starts from that same critical wobble), but every tuning is a negotiation between speed, overshoot and calm — the control-theory version of the trade you’ve met in every filter.
Where PID runs
Your car’s cruise control, the oven that holds 180° through a roast, drone attitude (three nested PIDs per axis, hundreds of updates per second), 3D-printer hotends, chemical plants by the thousand-loop, the buck converter’s feedback (10.2) — and, gloriously, it is about ten lines of MicroPython, which means your Pico can do all of this. The loop skeleton is your night-light’s superloop with better manners.
When any regulated thing misbehaves — a wobbling drone, a thermostat that overshoots, a shower that alternates scald and freeze — diagnose it in PID terms: too much P? starving I? missing D? You now own the vocabulary of every feedback system on Earth.
⚡ Lab — The Obedient Heater
The same stubborn plant, now with all three knobs and a tuning challenge.
- Start P-only (Ki = Kd = 0): the familiar offset. Add Ki and watch the grudge-keeper close it.
- Restart from cold with high Kp and no Kd: overshoot. Add Kd: damped.
- Pass the challenge: under 2 °C overshoot, zero final error, and survive the open window.