EmbeddedRelated.com

PID Controller

Category: Architecture | Also known as: proportional-integral-derivative controller

A PID controller is a closed-loop feedback algorithm that computes a corrective output by summing three terms: a proportional term (scaled directly from the current error), an integral term (scaled from the accumulated error over time), and a derivative term (scaled from the rate of change of the error). Together, the three terms let the controller reduce steady-state error, respond to sustained disturbances, and dampen oscillation.

In practice

PID controllers appear in a wide range of embedded applications: motor speed and position control, temperature regulation, DC-DC converter loops, drone attitude control, line-following robots, and more. The controller reads a measured process variable (e.g., shaft speed from an encoder, temperature from a thermistor), computes the error against a setpoint, and drives an actuator output (e.g., PWM duty cycle to a motor driver or heater). The loop typically runs at a fixed sample rate chosen to be fast enough relative to the plant dynamics, though some implementations use event-triggered or variable-rate scheduling -- commonly anywhere from a few Hz for thermal loops to tens of kHz for current-mode power supply loops.

On resource-constrained MCUs (8/16-bit devices, or any target without an FPU), floating-point PID implementations may be too slow or too expensive in code size. Fixed-point implementations using scaled integers are common in these cases. The blog posts "How to Build a Fixed-Point PI Controller That Just Works: Part I" and "Part II" cover the practical details of scaling, overflow avoidance, and rounding in fixed-point control code. "Data Types for Control & DSP" provides broader guidance on choosing numeric representations.

Several practical issues arise in almost every implementation. Integral windup occurs when the integrator accumulates a large value while the actuator is saturated (e.g., output clamped to 100% PWM), causing overshoot once the error shrinks. Anti-windup techniques -- clamping the integrator, back-calculation, or conditional integration -- are common in production code, though systems with sufficient actuator headroom or limited integral action may not require them. Derivative kick is a related problem: a step change in setpoint produces a large spike in the derivative term because the error changes instantaneously; computing the derivative of the measured value rather than the error significantly reduces or eliminates this effect in most formulations. Derivative noise amplification is another concern -- the D term differentiates the measurement signal, so sensor noise is amplified; a low-pass filter on the derivative term is standard practice.

Tuning the three gains (Kp, Ki, Kd) is often the most time-consuming part of a PID deployment. Manual methods (Ziegler-Nichols step response, trial-and-error), autotuners, and model-based tools all see use. In many embedded applications, the I and D terms are dropped entirely -- a pure P or PI controller is simpler to tune, less sensitive to noise, and sufficient for the plant. "PID Without a PhD" is a widely cited practical introduction to getting a working implementation tuned quickly without requiring control theory background.

Discussed on EmbeddedRelated

Frequently asked

What is the difference between positional and velocity (incremental) PID forms?
The positional form computes the absolute output value at each step from the full accumulated integral. The velocity (incremental) form computes the change in output since the last step and adds it to the previous output. Velocity form is often preferred in actuator-with-memory situations (e.g., a stepper with a position register) because it naturally avoids windup on startup and makes bumpless transfer easier when switching controllers.
How do I choose the sample rate for my PID loop?
A common rule of thumb is to sample at least 10 to 20 times faster than the desired closed-loop bandwidth, though tighter loops may need more. For slow thermal processes, a sample rate of 1 to 10 Hz is often adequate. For current-mode motor or power-supply control, sample rates of 10 kHz to 100 kHz are typical. Sampling too slowly degrades phase margin and can destabilize the loop; sampling excessively fast amplifies derivative noise without improving control.
What is integral windup and how do I prevent it?
Windup occurs when the actuator output is saturated (clamped at its minimum or maximum), but the integrator keeps accumulating error. When the error eventually reverses, the integrator must unwind before the output leaves saturation, causing significant overshoot. Common mitigations include clamping the integrator to a fixed range, disabling integration while the output is saturated (conditional integration), or using back-calculation anti-windup, where a correction proportional to the difference between the unsaturated and saturated output is fed back to subtract from the integrator.
Can I implement a PID controller in fixed-point on a small MCU without floating-point hardware?
Yes. A fixed-point PI or PID is standard practice on 8-bit and 16-bit MCUs and on Cortex-M0/M0+ parts that lack an FPU. The key challenges are choosing appropriate Q-format scaling so intermediate products do not overflow, and ensuring the integrator accumulator is wide enough (typically 32 or 64 bits) to prevent truncation errors. The blog series 'How to Build a Fixed-Point PI Controller That Just Works' covers these details concretely. 'Data Types for Control & DSP' provides additional background on numeric representation choices.
When should I use a PI controller instead of a full PID?
The derivative term improves response speed and damping but amplifies measurement noise and complicates tuning. For plants where the sensor signal is noisy (e.g., a current-sense resistor with switching noise), or where the plant itself provides sufficient natural damping, a PI controller is often the better choice. Many temperature and DC motor speed loops run successfully as PI-only. The D term is most valuable for fast, underdamped plants where overshoot must be aggressively suppressed.

Differentiators vs similar concepts

A PID controller is sometimes confused with a bang-bang (on/off) controller, which simply switches the actuator fully on or off based on whether the error is positive or negative. Bang-bang control is simpler but causes limit cycling (continuous oscillation around the setpoint). A PID controller produces a continuously variable output that can settle to a steady state with minimal oscillation, at the cost of tuning complexity. PID is also distinct from more advanced control strategies such as model predictive control (MPC) or state-space controllers, which incorporate an explicit plant model; PID does not require an explicit plant model, though practical implementations often incorporate filters, feedforward terms, setpoint weighting, and anti-windup logic that go beyond operating on the raw error signal alone.