03 Control Systems · #7 of 46

PID Control

Three terms that run almost every machine you've ever touched

Your oven, your car's cruise control, a drone holding altitude, the Mars rover aiming its camera: almost everything that holds a value steady runs the same three-term loop, invented to steer battleships a century ago.

It has a name that sounds like a chemical: PID.

The idea is almost insultingly simple. You have a thing you want at some value, a setpoint. You measure where it actually is. The gap between them is the error. A PID controller turns that error into an action using three terms, each answering a different question.

Drive the loop yourself

Below is the workhorse of every controls course: a springy second-order plant that lags and rings. Tap ▶ play and watch the needle chase the target. Then tune the three gains and see what each one does.

PID step response

Plant G(s) = 1 / (s² + 0.4 s + 1). Controller u = Kp e + Ki ∫e dt + Kd ė. Setpoint steps from 0 → 1 at t = 0.

y 0.000 target 1.000 t 0.00 s u 0.00
noise off
disturbance
rise time
overshoot
settle (±2%)
SSE (live)
what to try
  • Hit ▶ play with defaults. The needle leaves 0, ramps up, overshoots past the target, then settles. The trace below records that whole story; the dot on it tracks the needle.
  • Pure proportional (Ki = Kd = 0): bump Kp. Faster needle, but it parks below the target; the steady-state offset never goes away.
  • Add integral: the needle eventually pulls up to the target, but overshoot grows and settling stretches out.
  • Add derivative: overshoot calms down. D acts on the rate of change, so it brakes before the needle hits the wall.
  • Now turn on noise and replay. The D-term explodes because it amplifies the derivative of the noisy measurement; watch the needle judder. This is why we need observers, later in this chapter.
  • Kick the plant. At t = 8 s the needle suddenly drops; only the I-term reliably brings it back to the target.
  • Ziegler–Nichols ballpark: Kp ≈ 3.0, Ki ≈ 1.5, Kd ≈ 0.7.

Each term reacts to the error differently:

Portrait of control-theory pioneer Nicolas Minorsky.
Nicolas Minorsky · 1885–1970 A Russian-American control engineer who watched a helmsman work the wheel of the battleship USS New Mexico and wrote the intuition down as the three-term law in 1922: the PID equation now estimated to run ~95% of the world's industrial control loops. read more →
r(t)+e(t)PIDu(t)Plantc(t)c(t) feedback
The PID loop: the reference r(t) meets the fed-back output c(t) at the summing junction (+ on the reference, − on the feedback) to form the error e(t). The error drives three terms whose sum is the command u(t); the plant turns that into the output c(t), which loops back to close the error.

The tuning dance

Tuning PID is a balancing act, and the demo lets you feel every trade-off:

  1. Pure P (set I and D to 0): fast, but it never quite reaches the target.
  2. Add I: it pulls all the way up, but overshoots and takes longer to settle.
  3. Add D: the overshoot calms down, because D brakes on approach.
  4. Turn on noise: the D-term explodes, jittering the output. Real sensors are noisy, which is exactly why the next lessons build observers to estimate a clean signal before differentiating it.
  5. Arm the load disturbance: at t = 8 s a constant force shoves the plant. Only the integral term can fully shrug it off.
The math: a second-order plant and the three-term law

The plant is the canonical damped second-order system, with natural frequency ωn=1\omega_n = 1 and damping ratio ζ=0.2\zeta = 0.2:

y¨+0.4y˙+y=u\ddot{y} + 0.4\,\dot{y} + y = u

The controller closes the loop on the error e=rye = r - y (setpoint minus output):

u(t)=Kpe(t)+Ki0te(τ)dτ+Kde˙(t)u(t) = K_p\,e(t) + K_i \int_0^t e(\tau)\,d\tau + K_d\,\dot{e}(t)

Read it term by term: KpeK_p e pushes proportional to the present error; KieK_i \int e integrates the past to erase any lingering offset; Kde˙K_d \dot e looks at the trend and brakes before overshoot. The demo integrates this by forward-Euler at dt=0.01sdt = 0.01\,\text{s}. Switching on noise adds Gaussian σ\sigma to the measurement before the error is formed. Notice that the derivative reacts to the noisy measurement, not the smooth truth. That single fact is the seed of the entire state-estimation half of robotics.

Key takeaways

Run the controller yourself

This is a full PID loop driving a cold plant up to 100 degrees, and the three gains are yours. Bump Kp for a faster, twitchier response; add Ki to erase steady-state error; add Kd to damp the overshoot. Run it and read the overshoot straight off the numbers.

PID controller editable · Python
ready

Three numbers. One loop. It steered a battleship in 1922 and it holds your quadcopter level today.

PID is the plainest possible answer to the deepest question in control: given the gap between where you are and where you want to be, how hard should you push? Everything fancier (pole placement, optimal control, the Kalman-filtered observers a few lessons from now) is just a more careful way of answering that same question.

full glossary →