⚡ Spark Academy53 lessons

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.

lesson 2 of 2 in this unit

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:

drive = Kp·e + Ki·∫e·dt + Kd·de/dtpresent · past · future — the PID controller, workhorse of industry since the 1920s
  • 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.

The master's habit

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.
60 °C
3.0
0.50
1.5
Tuning challenge
from cold: reach 60 °C with under 2 °C overshoot and zero final error. Then survive the window.
What each term is
P reacts to the present, I remembers the past (kills offset), D predicts the future (damps the ring)

Check your understanding

Q1. Which PID term eliminates steady-state offset?

Q2. The derivative term's job is to…

Q3. 'Integral windup' is the problem of…

Q4. Why do many industrial loops run PI without D?