PID Controller
March 21, 2026 · View on GitHub
Overview & Motivation
The Proportional-Integral-Derivative (PID) controller is the workhorse of industrial feedback control. It continuously computes an error — the difference between a desired setpoint and a measured process variable — and applies a correction based on three terms: one proportional to the current error, one to its accumulated history, and one to its rate of change.
Despite its simplicity, PID handles a remarkably wide range of plants — from temperature regulation to motor speed control — because each term addresses a different aspect of the transient response:
- P reacts immediately to the current error.
- I eliminates steady-state offset by integrating past errors.
- D anticipates future error by acting on the rate of change.
This library implements a discrete recursive form suitable for fixed-sample-rate embedded loops, where the output is computed incrementally rather than from scratch at every step.
Mathematical Theory
Continuous-Time PID
Discrete Recursive Form
Discretizing with sampling period and applying the difference approximation:
where:
This incremental (velocity) form has two key advantages:
- No integral accumulator — the integral action is implicit in the recursion, reducing overflow risk.
- Bumpless mode switching — toggling between auto/manual mode does not cause output jumps because the output is updated incrementally.
Output Saturation
The output is clamped to after each computation, which also provides implicit anti-windup: the integral term cannot drive the output beyond the saturation limits.
Complexity Analysis
| Case | Time | Space | Notes |
|---|---|---|---|
| Per sample | 3 multiplications, 3 additions, 1 clamp |
The algorithm uses a fixed-size recursive buffer storing , , and . No loops, no dynamic allocation, fully deterministic execution time.
Step-by-Step Walkthrough
Parameters: , , , limits , setpoint .
Coefficients:
| Step | Measured | (clamped) | ||||
|---|---|---|---|---|---|---|
| 0 | 0.0 | 5.0 | 0 | 0 | $2.6 \times 5 = 13$ | 10.0 (clamped) |
| 1 | 2.0 | 3.0 | 5.0 | 0 | $2.6(3) - 2.2(5) + 0.1(0) = -3.2$ | 6.8 |
| 2 | 4.0 | 1.0 | 3.0 | 5.0 | $2.6(1) - 2.2(3) + 0.1(5) = -3.5$ | 3.3 |
| 3 | 4.8 | 0.2 | 1.0 | 3.0 | $2.6(0.2) - 2.2(1) + 0.1(3) = -1.38$ | 1.92 |
The output converges toward the steady-state value needed to maintain the setpoint.
Pitfalls & Edge Cases
- Derivative kick. A sudden change in setpoint causes a spike in . Mitigate by differentiating the process variable instead of the error, or by filtering the derivative term.
- Integral windup. Although the recursive form provides implicit anti-windup through output clamping, very aggressive values can still cause sluggish recovery from saturation. Monitor the output rail time.
- Sample rate dependency. The gains and are implicitly scaled by . Changing the control loop rate without retuning will alter the effective integral and derivative contributions.
- Fixed-point saturation. For Q15/Q31 types, the intermediate products etc. can overflow. Ensure gains and error ranges are scaled to stay within the representable range.
- Setpoint jumps. Call
Reset()after large setpoint changes to clear stale error history.
Variants & Generalizations
| Variant | Key Difference |
|---|---|
| Standard (positional) PID | Computes from scratch each step using an explicit integral accumulator |
| PI controller | ; simpler, no derivative noise issues |
| PD controller | ; no steady-state error correction, used when offset is acceptable |
| PID with derivative filter | Low-passes the derivative term to reject high-frequency noise |
| Gain-scheduled PID | Tuning parameters vary as a function of operating point |
| Cascade PID | Inner and outer loops, each with its own PID — common in motor control |
Applications
- Temperature control — Maintaining oven, room, or process temperatures via heater/cooler actuation.
- Motor speed/position control — Servo drives, robotics joints, CNC machines.
- Flow and pressure regulation — Industrial process control (chemical plants, water treatment).
- Voltage/current regulation — Power supply output regulation, battery charging.
- Attitude control — Drone stabilization, satellite pointing.
Connections to Other Algorithms
graph LR
PID["PID Controller"]
LQR["LQR Controller"]
KF["Kalman Filter"]
PID -.->|"alternative"| LQR
KF -->|"state estimate"| LQR
KF -->|"filtered measurement"| PID
| Algorithm | Relationship |
|---|---|
| LQR Controller | Optimal alternative when a state-space model is available; PID is model-free. |
| Kalman Filter | Can provide filtered state estimates as input to either PID or LQR |
References & Further Reading
- Åström, K.J. and Murray, R.M., Feedback Systems: An Introduction for Scientists and Engineers, Princeton University Press, 2008 — Chapter 10.
- Åström, K.J. and Hägglund, T., Advanced PID Control, ISA, 2006.
- Ziegler, J.G. and Nichols, N.B., "Optimum settings for automatic controllers", Transactions of the ASME, 64, 1942.