PID Control: Derivation, Tuning, and When It Fails
PID is the most deployed controller in history and the most mistuned — derive it from the Laplace transform and you will never tune it blindly again.
🎯 By the end you'll understand
Where PID comes from mathematically — the closed-loop transfer function and how P, I, and D each move the poles. The right order to add gains: stability first (P-only), steady-state error next (add I), transient performance last (add D). How Ziegler-Nichols works and exactly why it often produces an over-tuned response. How to implement PID discretely without derivative kick or reset windup. Why PID fails with pure plant delay — and what to do instead. How D-term amplifies sensor noise if unfiltered.
KEY INSIGHTPID is pole placement disguised as arithmetic
Every time you turn a PID knob you are moving the closed-loop poles of the system — whether you know it or not. Know it.
PID control is often taught as three intuitive knobs: P for proportion, I to kill steady-state error, D to damp oscillation. That framing is useful for a first mental model but nearly useless for systematic tuning. The reason: it hides the underlying structure. In the Laplace domain, PID is a specific rational transfer function that adds zeros to the open-loop plant and pushes the closed-loop poles to desired locations. When you raise Kp, you are not "making the controller stronger" — you are scaling the open-loop gain and changing where those poles land. The wrong poles mean instability or noise amplification, regardless of how large your gains are.
Start with Ki = 0, Kd = 0. Raise Kp until the system responds — without oscillating. You now have a stable P-only controller with steady-state error. Then raise Ki slowly until the steady-state error disappears; watch for oscillation onset. Finally, if your sensor is clean enough, add Kd to reduce overshoot. This order is not aesthetic — each gain solves a different structural problem, and adding I or D before achieving P-stability is guaranteed to fail.
CORE CONCEPTPID from the Laplace transform
Start with the error signal: e(t) = r(t) − y(t), where r is the reference (setpoint) and y is the measured output. The PID control law in continuous time is:
The Laplace form C(s) = Kp + Ki/s + Kd·s is the controller transfer function. It multiplies the plant transfer function G(s) in the open-loop. For a first-order plant with unity feedback, G(s) = 1/(τs + 1), the open-loop transfer function is:
L(s) = (Kds² + Kps + Ki) / [s(τs + 1)]
The closed-loop transfer function from reference R to output Y is T(s) = L(s) / [1 + L(s)]. The denominator 1 + L(s) = 0 defines the characteristic equation — its roots are the closed-loop poles. PID tuning moves these poles. Adding Ki places an open-loop pole at s = 0 (origin), which forces the closed-loop to have zero steady-state error to a step input. Adding Kd places a zero that can cancel a slow plant pole or add damping. This is pole-zero placement, not magic.
For the closed-loop to be stable, all poles must have negative real parts (left half of the s-plane). Raising Kp moves poles left initially (faster response) but eventually drives them into the right half-plane (instability). The gain at which the poles cross the imaginary axis is the ultimate gain Ku — the foundation of Ziegler-Nichols.
The gain margin and phase margin are the canonical measures of how far the closed-loop is from instability. A gain margin of 6 dB means the open-loop gain can double before instability; a phase margin of 45° means the loop can tolerate 45° of additional phase lag (from delay or unmodeled dynamics). Both appear directly on the Bode plot.
HOW IT WORKSStability first, then steady-state error, then transient performance
The three gains solve three separate problems. Understand each problem before adding its gain.
P-only: achieve stability with fast response
Set Ki = 0, Kd = 0. Raise Kp from zero. At low Kp, the response is sluggish — the closed-loop pole is real, large, and negative, producing slow exponential decay. As Kp increases, the pole pair moves leftward and eventually becomes complex (underdamped oscillation). Keep raising and the poles move toward the imaginary axis — oscillation grows. Keep going past the ultimate gain Ku and the poles cross into the right half-plane: the system diverges. The useful range for Kp is well below Ku. P-only steady-state error: for a first-order plant, ess = 1/(1 + Kp·Kdc) where Kdc is the plant DC gain — never zero with P-only unless Kp → ∞.
Add I: eliminate steady-state error
Adding Ki places an open-loop pole at s = 0. The resulting system is Type 1 — it will track a step reference with zero steady-state error. The price: the integrator adds 90° of phase lag at all frequencies, which reduces phase margin. A Kp that gave 60° phase margin in P-only may give only 20° with Ki added — dangerously close to oscillation. Keep Ki small relative to Kp; the integrator should be slow enough to remove steady-state error over 2–5 settling-time periods, not aggressively fast.
Add D: reduce overshoot and settling time
The derivative term adds a zero at s = −Kd/Kp to the open-loop transfer function. This zero contributes positive phase (phase lead) near its frequency, partially compensating the phase lag from the integrator. The result: the closed-loop poles move further left (faster settling) or become less underdamped (less overshoot). The cost: D amplifies high-frequency noise because differentiation multiplies noise amplitude by frequency. A first-order low-pass filter on the error — or equivalently a filtered derivative N/(s + N) — is not optional in real hardware.
Kp: "I act in proportion to how wrong things are right now." Fast but can overshoot. Ki: "I remember every past error and accumulate debt." Eliminates steady-state error but slows response and can wind up. Kd: "I react to how fast the error is changing." Brakes before the overshoot happens — but hears every noise spike in the sensor as an 'error changing fast'.
IN PRACTICEDiscrete-time implementation: two pitfalls to avoid
Continuous PID is derived in continuous time. Your microcontroller runs at a fixed sample period Ts. The translation introduces two classic implementation bugs.
Pitfall 1: Derivative kick. When the setpoint r changes step-wise, the error e = r − y jumps discontinuously. The derivative de/dt spikes to +∞ in theory (large in practice), commanding a huge control output for one sample. Fix: compute the derivative of the output y, not the error. Since d(r−y)/dt = −dy/dt when r is constant, you get the same damping effect without the setpoint spike:
class PIDController: def __init__(self, kp, ki, kd, dt, N=20, windup_limit=10.0): self.kp, self.ki, self.kd = kp, ki, kd self.dt, self.N = dt, N self.windup_limit = windup_limit self.integral = 0.0 self.y_prev = None # track output, not error → no derivative kick self.d_filtered = 0.0 # first-order filter on derivative def update(self, setpoint, measurement): e = setpoint - measurement # --- Integral with anti-windup clamping --- self.integral += e * self.dt self.integral = max(-self.windup_limit, min(self.windup_limit, self.integral)) # --- Derivative on measurement (not error) → no kick --- if self.y_prev is None: dy = 0.0 else: dy = (measurement - self.y_prev) / self.dt # first-order low-pass filter: alpha = N·dt / (1 + N·dt) alpha = self.N * self.dt / (1.0 + self.N * self.dt) self.d_filtered = alpha * dy + (1 - alpha) * self.d_filtered self.y_prev = measurement return (self.kp * e + self.ki * self.integral - self.kd * self.d_filtered) # minus: oppose increasing y
Pitfall 2: Reset windup. When the actuator saturates, the control output u(t) is clamped but the integrator keeps accumulating error. When the error finally reverses, the integrator has built up a large "debt" that continues to drive the output in the original direction — causing overshoot or oscillation. The fix above clamps the integral term to ±windup_limit. A more principled fix is conditional integration: stop integrating when the output is saturated and the error is in the same sign as the saturation.
The discrete derivative (Δy/Ts) amplifies noise by 1/Ts. At Ts = 0.01 s (100 Hz), a sensor noise floor of 0.001 rad/s becomes a derivative spike of 0.1 rad/s². At Ts = 0.001 s (1000 Hz), the same noise spike becomes 1.0 rad/s². Running PID at unnecessarily high rates magnifies the D-term noise problem. The derivative filter coefficient N (above) should be set so the filter cutoff frequency N is well below the sensor Nyquist rate but above the desired bandwidth.
EXAMPLEZiegler-Nichols: a field method with known limitations
Ziegler-Nichols (1942) is a systematic procedure for tuning PID gains without a plant model. The closed-loop method: increase Kp with Ki = Kd = 0 until the system oscillates with constant amplitude. Record the ultimate gain Ku and ultimate period Tu of that oscillation. Apply the tabulated formulas:
| Controller type | Kp | Ti (= Kp/Ki) | Td (= Kd/Kp) |
|---|---|---|---|
| P-only | 0.50 · Ku | — | — |
| PI | 0.45 · Ku | 0.83 · Tu | — |
| PID | 0.60 · Ku | 0.50 · Tu | 0.125 · Tu |
Z-N is brutally practical: it requires no model, works on a running system, and gets you into the right ballpark in minutes. It is also widely criticized for a specific reason: the original tuning was designed to minimize integral of time-weighted absolute error (ITAE) for a load disturbance, not for a reference step. The resulting gains typically produce 25–50% overshoot on a setpoint change — unacceptable for many robotics applications. The fix is to scale the Z-N gains down after the initial estimate: start at 60% of Z-N gains, observe the step response, and adjust.
Z-N was derived for plants that behave like first-order systems with dead time — a reasonable model for process control (chemical tanks, temperature loops). A FollowBot wheel velocity loop with a brushless DC motor is closer to a second-order system with a back-EMF time constant and no significant dead time. Z-N will over-tune Ki and under-specify Kd for this plant class. Use Z-N as a starting point, not a finish line. Root locus or frequency-domain analysis (Phase margin > 45°, Gain margin > 6 dB) should be the termination criterion.
SEE ITPID tuner — live step response, gain plane, and noise injection
The plant below is a first-order system with configurable time constant τ and pure time delay θ: G(s) = e−θs/(τs + 1). Set the PID gains with the sliders. Watch the step response update live. Toggle noise on and see how the D-term amplifies it. The red stability region on the gain plane shows where the system goes unstable — learn the shape before you tune blindly.
Key observations from the tuner: (1) With zero delay θ=0, you can push Kp very high before instability. Add delay and the stability boundary shrinks dramatically — doubling the delay roughly halves the maximum stable Kp, because the limit scales with τ/θ. (2) The D-term without filtering on the "High noise" setting produces a control signal that is more noise than signal — the controller is fighting the sensor, not the plant. (3) On the gain plane, the red unstable region expands as you increase Kd (for fixed Kp and Ki) — the D-term can destabilize an otherwise stable system if the derivative filter coefficient N is too large.
FOLLOWBOTPID velocity controller for FollowBot's wheel drive
In Lesson 3.3 we derived the full dynamics model for FollowBot's differential-drive base, including the brushless DC motor's electrical and mechanical time constants. Now we close the velocity loop.
The wheel velocity loop plant is approximately a first-order system. From the motor dynamics in Lesson 3.3, the motor's mechanical time constant τm ≈ 0.12 s and the combined electrical + computation delay θ ≈ 8 ms. The plant DC gain Kdc = 1/(b) where b is the viscous damping coefficient.
Using Ziegler-Nichols closed-loop method on the real FollowBot hardware:
# Step 1: Set Ki=0, Kd=0. Ramp Kp up in a safe environment. # Observed: Ku ≈ 14.2, Tu ≈ 0.085 s (measured from oscillation period) Ku = 14.2 Tu = 0.085 # seconds # Step 2: Z-N PID formulas (starting point only) Kp_zn = 0.60 * Ku # = 8.52 Ki_zn = Kp_zn / (0.50 * Tu) # = 200.5 — very aggressive (units: N·m·s/rad for velocity control) Kd_zn = Kp_zn * (0.125 * Tu) # = 0.090 # Step 3: Scale down to 60% — Z-N overshoots by design Kp = 0.60 * Kp_zn # = 5.11 Ki = 0.60 * Ki_zn # = 120.3 — still aggressive, watch windup Kd = 0.60 * Kd_zn # = 0.054 # Step 4: Verified settling time ≤ 0.35 s with <15% overshoot (spec). # Phase margin measured from Bode plot: 52°. Gain margin: 8.1 dB. Acceptable.
The quantitative spec for FollowBot's velocity controller is: settling time ≤ 0.35 s and overshoot ≤ 15% for a step in commanded wheel velocity. With the Z-N starting point refined via root locus, the final gains are Kp = 5.11, Ki = 120.3, Kd = 0.054 with filter N = 20. Phase margin 52°, gain margin 8.1 dB.
The feedforward term from Lesson 3.3's inverse dynamics model reduces the load on the PID by pre-computing the expected torque required for a given velocity trajectory. With feedforward active, the PID only needs to correct residual model error — Ki can be reduced by roughly 40% without sacrificing steady-state tracking, which reduces the integrator windup risk during arm loading events.
With the velocity controller tuned and feedforward active, FollowBot tracks a figure-eight trajectory (radius 1.0 m, period 12 s) with measured cross-track error (lateral deviation from the reference path) below 0.08 m RMS. The spec is 0.10 m RMS. This milestone closes the Stage 3 control thread: full dynamics model derived (Lesson 3.2), sim-to-real gap characterized (Lesson 3.3), PID velocity controller tuned to spec (this lesson). The figure-eight test is the acceptance criterion before moving to Stage 4's state estimation stack.
WEAKNESSWhere PID breaks: the failure-mode angle
Pure time delay θ in the plant contributes phase −ωθ radians at frequency ω. As ω → ∞, this phase lag becomes unbounded — it cannot be compensated by any finite-order controller. In the s-domain, e−θs is non-rational: it has infinitely many poles in the right half-plane when expanded as a Taylor series. For small θ, PID survives because the phase lag at the bandwidth frequency is small. But as θ grows relative to the plant time constant (specifically when θ/τ > 0.3), phase margin collapses and no PID tuning can restore it. The fix is a Smith Predictor (which moves the delay inside the feedback loop) or a model-predictive controller (which plans over the delay horizon). For FollowBot's wheel loop (θ/τ ≈ 8ms/120ms ≈ 0.07), PID is fine. For a hydraulic system with θ/τ > 0.5, it is not.
The derivative gain Kd amplifies signal components proportional to their frequency. A sensor with noise floor at −40 dB (1% of full scale) spread across 0–500 Hz will produce derivative noise of Kd × noise × frequency. At Kd = 0.1 and 500 Hz, a 1% noise floor becomes a derivative contribution of 31× the noise amplitude — dominating the control output. Without the first-order filter (N ≈ 10–30), increasing Kd past a small value just adds high-frequency chatter to the motors, accelerating wear and producing heat without damping. The filtered derivative in the code above is not an optional nicety: on real hardware with real encoders, it is the difference between a working D-term and a buzzing motor.
A differential-drive robot has two independently controlled wheels. Running separate PID loops on each wheel velocity works for straight-line tracking but produces cross-coupling during turns: the left wheel's velocity error creates a torque that perturbs the right wheel's dynamics, which the right PID then corrects, which perturbs the left, and so on. For slow robots this cross-coupling is negligible. For fast maneuvers or heavy arms, decoupling becomes necessary — either via feedforward (the inverse dynamics model cancels the coupling terms explicitly) or by moving to a multivariable controller (LQR, covered in Lesson 3.6). PID works well on SISO subsystems; force-fitting it onto coupled MIMO systems requires increasing amounts of engineering workaround.
KEY INSIGHTHow this connects
- 3.3 · Actuator Models and the Sim-to-Real Dynamics Gap — the plant model we just tuned PID for. The motor time constants τm and delay θ used throughout this lesson come directly from the brushless DC motor dynamics derived there. Without the sim-to-real characterization, the Z-N procedure would use an incorrect Ku.
- 1.7 · Actuators and the Real-Time Constraint — the timing budget for the velocity control loop (100 Hz, 10 ms deadline) was set in Stage 1. This lesson fills in why that rate matters: too slow and the discrete derivative amplifies noise; too fast adds no information but amplifies it further.
- 3.5 · Stability: Lyapunov Theory and Phase Portraits → — this lesson characterized stability via Bode phase margin (a frequency-domain tool). The next lesson introduces Lyapunov's direct method: a time-domain stability proof that works for nonlinear systems where PID's linear analysis breaks down.
PID sits at the foundation of the Act node's control stack. Every lesson in Stage 3 that follows — LQR, feedforward, trajectory tracking — is either an extension of PID's structure or a response to one of its failure modes described above. Understanding where PID succeeds and where it fails is prerequisite to understanding why those more complex tools exist.
GLOSSARYKey terms introduced
- Closed-loop transfer function
- T(s) = C(s)G(s) / [1 + C(s)G(s)]: the ratio of output to reference in a unity-feedback loop. Its poles — roots of 1 + C(s)G(s) = 0 — determine stability and transient behavior.
- Phase margin
- The additional phase lag (in degrees) that would cause the closed-loop to become marginally stable. Measured at the gain crossover frequency where |L(jω)| = 1. A healthy robotics controller targets 45–60°.
- Gain margin
- The factor by which the open-loop gain can increase before instability. Measured at the phase crossover frequency where ∠L(jω) = −180°. Expressed in dB; a typical target is 6–12 dB.
- Reset windup (integral windup)
- Accumulation of integrator state during actuator saturation. When saturation ends, the wound-up integral drives a large overshoot. Mitigated by clamping the integral or using conditional integration.
- Derivative kick
- The impulse-like spike in PID output when the setpoint changes discontinuously, caused by differentiating a step change in error. Avoided by computing the derivative of the measured output instead of the error.
- Ultimate gain Ku
- The P-only gain at which the closed-loop system produces sustained oscillation (poles on the imaginary axis). The Ziegler-Nichols starting point for gain selection.
- Smith Predictor
- A control structure that compensates for pure plant delay by incorporating an internal plant model to predict the future output, effectively removing the delay from the closed-loop characteristic equation.
- Derivative filter coefficient N
- The cutoff frequency (rad/s) of the first-order low-pass filter applied to the derivative term: D(s) = Kd·N·s/(s + N). Limits noise amplification; typical values N = 10–40 for robotics applications.
ACTIVE RECALLCheck your understanding
Answer in your own words first, then reveal.
1Why does adding Ki to a P-only controller always reduce phase margin, and what is the practical consequence for tuning?›
Adding Ki places an open-loop pole at s = 0 (the integrator). In the frequency domain, a pole at the origin contributes −90° of phase lag at every frequency. This reduces the phase margin of the open-loop transfer function at the gain crossover frequency. If the P-only design had 60° phase margin, adding Ki might reduce it to 20–30°, making the system far more sensitive to unmodeled dynamics or delay. The practical consequence: when you add Ki, you must either simultaneously reduce Kp (to push the gain crossover lower, where the integrator's phase lag is less damaging) or accept a reduced stability margin. This is why adding I before achieving adequate P-stability — specifically, phase margin well above 45° — almost always produces oscillation.
2FollowBot's wheel velocity PID is well-tuned on the bench but oscillates on a carpet surface that adds 35 ms of extra stick-slip delay. Why does this happen, and what is the principled fix?›
The plant delay θ increased from ~8 ms to ~43 ms. The ratio θ/τ went from 0.07 to 0.36 — approaching the regime where PID loses phase margin rapidly. Pure delay contributes phase −ωθ radians; at the gain crossover frequency ωc, the additional 35 ms of delay adds −ωc × 0.035 radians of phase lag. If the original phase margin was 52° and the bandwidth was around 10 rad/s, the additional delay contributes about 20° of phase loss — bringing the margin down to ~32°. Reducing Kp lowers the bandwidth and partially restores margin, but also slows the response. The principled fix for large delays is a Smith Predictor, which incorporates an internal delay model to predict the output θ seconds ahead and removes the delay from the characteristic equation. For smaller delay growth, simply reducing Kp and Ki until stability is restored is practical.
3What specifically goes wrong in discrete-time PID when the derivative is computed on the error e(t) = r(t) − y(t) and the reference r changes as a step?›
The derivative of the error at the step instant is (e[k] − e[k−1])/Ts. At the moment r steps from 0 to 1, e jumps by 1 in one sample. The discrete derivative is therefore 1/Ts, which at 100 Hz is 100 — multiplied by Kd, this produces a control impulse Kd/Ts times the step size. For Kd = 0.1 and Ts = 0.01 s, that is a momentary control output of 10× the step magnitude — often saturating the actuator and causing an initial overshoot spike unrelated to the actual transient response. This "derivative kick" disappears if you compute the derivative on y instead of e: d(−y)/dt = −dy/dt, which does not spike on reference changes. The damping effect on the error dynamics is identical during the transient (when r is constant, de/dt = −dy/dt), but the kick is eliminated.
4Ziegler-Nichols gives you Kp = 8.5 for a system. After implementation you observe 45% overshoot. What does this tell you about Z-N's design objective, and what should you do?›
45% overshoot is consistent with what Ziegler-Nichols was designed to produce. The original Z-N tuning rules optimize for minimum integral of time-weighted absolute error (ITAE) for a load disturbance, not for minimum overshoot on a setpoint step. The 0.6 Ku gain rule places the closed-loop poles in a somewhat underdamped configuration (damping ratio ζ ≈ 0.15–0.2 for typical plants) that handles disturbances quickly but overshoots setpoints substantially. The correct response is to reduce Kp to about 60–70% of the Z-N value (roughly 5.1–6.0 in this case) and simultaneously reduce Ki proportionally, then re-evaluate. Alternatively, use the Z-N gains as an initial estimate for root locus: place them in the characteristic equation and find where the poles are, then move the gains to push the poles to a desired damping ratio (typically ζ = 0.7 for 5% overshoot).