PID Control
PID control is the most common feedback algorithm in practical mechatronics. It combines proportional, integral, and derivative actions to reduce present error, remove steady offset, and add damping. PID appears in motor drives, temperature controllers, pressure loops, flow control, robotics, process equipment, and laboratory instruments.
The algorithm is simple enough to write in a few lines, but good PID behavior depends on the whole system: actuator limits, sensor noise, sample timing, mechanical friction, backlash, compliance, and safe startup/shutdown behavior.
Learning Objectives
By the end of this lesson, you should be able to:
- Explain proportional, integral, and derivative action.
- Write a discrete PID loop with a fixed sample time.
- Identify integral windup, derivative noise, output saturation, and sample-time mistakes.
- Tune a simple loop in a safe, repeatable order.
- Decide when PID is not enough and the mechanical or sensing design must be improved.
PID Equation
Continuous PID is commonly written as:
$$
u(t)=K_p e(t)+K_i\int e(t)dt+K_d\frac{de(t)}{dt}
$$
where:
u(t)is the actuator command.e(t)is setpoint minus measured output.Kpis proportional gain.Kiis integral gain.Kdis derivative gain.
For a firmware loop with fixed sample time Ts:
$$
I_k=I_{k-1}+e_kT_s
$$
$$
D_k=\frac{e_k-e_{k-1}}{T_s}
$$
$$
u_k=K_pe_k+K_iI_k+K_dD_k
$$
Units matter. If position is measured in meters and output is force in newtons, Kp has units of N/m. If sample time changes, the integral and derivative behavior changes unless the implementation accounts for Ts correctly.
What Each Term Does
| Term | Effect | Useful for | Risk |
|---|---|---|---|
| P | reacts to present error | fast correction | oscillation and steady offset |
| I | accumulates past error | removing final offset | windup and slow recovery |
| D | reacts to rate of error change | damping and prediction | noise amplification |
Proportional Action
Proportional action is immediate:
$$
u_P=K_pe
$$
Higher Kp usually gives faster response and smaller steady error, but too much Kp can overshoot, excite resonance, or chatter across backlash.
Integral Action
Integral action keeps increasing while error remains:
$$
u_I=K_i\int e(t)dt
$$
It removes steady-state error caused by friction, gravity, leakage, load torque, or heat loss. It also stores history, so it must be limited when the actuator saturates.
Derivative Action
Derivative action responds to rate of change:
$$
u_D=K_d\frac{de(t)}{dt}
$$
It can reduce overshoot by adding damping, but it is sensitive to noise. Many practical controllers use derivative on measurement rather than derivative on error to avoid a large derivative kick when the setpoint changes.
Discrete PID with Limits
float pid_update(float setpoint, float measurement, float Ts)
{
static float integral = 0.0f;
static float prev_measurement = 0.0f;
const float Kp = 2.0f;
const float Ki = 0.8f;
const float Kd = 0.05f;
const float out_min = -1.0f;
const float out_max = 1.0f;
const float i_min = -0.5f;
const float i_max = 0.5f;
float error = setpoint - measurement;
float derivative = -(measurement - prev_measurement) / Ts;
integral += error * Ts;
if (integral > i_max) integral = i_max;
if (integral < i_min) integral = i_min;
float u = Kp * error + Ki * integral + Kd * derivative;
if (u > out_max) u = out_max;
if (u < out_min) u = out_min;
prev_measurement = measurement;
return u;
}
This example uses derivative on measurement and clamps both the integral state and output. Real firmware should also handle invalid sensor data, disabled state, startup initialization, output slew limits, and fault latching.
Saturation and Windup
Actuators have limits. If the controller asks for 140% duty cycle, 20 A from a 10 A driver, or a valve opening beyond its travel, the real output saturates. If the integral term keeps accumulating while saturated, the loop may overshoot badly when it finally recovers. This is integral windup.
Common anti-windup methods:
- Clamp the integral state.
- Stop integrating when the output is saturated and error would drive it further into saturation.
- Back-calculate the integral from the difference between raw output and saturated output.
- Reset or preload the integral when changing modes.
Tuning Workflow
- Confirm sensor direction, actuator direction, limits, and emergency stop.
- Disable integral and derivative.
- Increase
Kpuntil the response is useful but not unstable. - Add
Kdif overshoot or oscillation needs damping. - Add small
Kito remove final error. - Test steps in both directions and with expected load changes.
- Reduce gains if the loop becomes noisy, hot, unstable, or sensitive to payload.
- Record plots before and after every change.
For temperature loops, start slower and expect long delays. For motor position loops, start with low current limits and small moves. For pressure or hydraulic systems, account for stored energy and relief valves.
Worked Example: Sample-Time Effect
Suppose a controller uses:
$$
I_k=I_{k-1}+e_kT_s
$$
If e = 0.5 and Ts = 0.01 s, the integral grows by:
$$
\Delta I=0.5 \times 0.01=0.005
$$
If the loop accidentally runs at Ts = 0.02 s, the integral grows twice as much per update. Derivative scaling is also wrong if the code assumes the old period. This is why control tasks should use measured or guaranteed sample time, not a casual delay loop.
Common Mistakes
- Starting with all three gains nonzero.
- Using derivative directly on noisy error without filtering.
- Forgetting actuator saturation and windup.
- Changing sample time without retuning or rescaling.
- Tuning unloaded and expecting loaded behavior to match.
- Using integral to overcome a mechanical problem such as severe stiction.
- Resetting the controller while leaving the actuator in an unsafe state.
- Reporting only final error while ignoring overshoot, heat, current, and noise.
Summary
PID works because proportional action reacts, integral action removes offset, and derivative action damps change. Practical PID design is mostly about limits: actuator saturation, sensor noise, sample timing, delay, friction, backlash, mechanical resonance, and safe tuning discipline.
Further Reading
- Karl Astrom and Tore Hagglund, PID Controllers: Theory, Design, and Tuning.
- Brett Beauregard, Improving the Beginner's PID.
- Control Engineering, PID tuning fundamentals.
- Tim Wescott, practical notes on PID without a PhD.