Explain proportional, integral and derivative action and tune a PID loop on a simulated first-order plant.
Why it matters
Feedback control keeps motors at speed, holds an inverter's output voltage steady, regulates temperatures, steers drones and balances generation with load on the grid. Most industrial loops are PID controllers. Electrical engineers design them for power converters and drives, and tune them in simulation before going near hardware.
Open loop vs closed loop
An open-loop controller sends a fixed command and hopes. A closed-loop controller measures the output y, compares it with the setpoint r, and acts on the error e = r - y. Feedback corrects for disturbances (a load change, a cold draught) and for a plant that isn't exactly what you modelled.
The plant
The thing being controlled is the plant. Many systems (a heater, a DC motor's speed, an RC circuit) behave roughly like a first-order system:
tau * dy/dt = -y + K * u
K is the steady-state gain (output per unit input) and tau is the time constant. Stepped forward in time with step dt (Euler's method, fine when dt is much smaller than tau):
y += dt / tau * (-y + K * u)
The PID law
u = Kp * e + Ki * integral(e dt) + Kd * de/dt
- Proportional (P): pushes harder the further you are from the setpoint. Alone, it leaves a steady-state error: for the first-order plant the output settles at
K Kp / (1 + K Kp)of the setpoint. - Integral (I): accumulates past error, so any persistent error keeps growing the command until it disappears. It removes steady-state error but can cause overshoot and, if the actuator saturates, integrator windup.
- Derivative (D): reacts to how fast the error is changing, adding damping in many plants. It amplifies measurement noise, so real controllers filter it or take the derivative of the measurement instead of the error.
In discrete time, each step:
integral += e * dt
derivative = (e - prev_e) / dt
u = kp * e + ki * integral + kd * derivative
prev_e = e
Worked example: a first-order plant
Plant: K = 2, tau = 1 s, setpoint 1, dt = 0.01 s, 10 s of simulation.
P only, Kp = 2: settles at 2*2 / (1 + 2*2) = 0.80 (20% error)
PI, Kp = 1, Ki = 5: reaches 1.0 but overshoots to about 1.24
PI, Kp = 4, Ki = 4: within 2% of 1.0 in under 0.5 s, no overshoot
The third tuning works well because the closed-loop poles are real and fast. That is the tuning game: fast enough, little overshoot, no oscillation, and robust when the plant changes a little.
Tuning in practice
- Manual: raise
Kpuntil the response is quick but starts to oscillate, back off; addKito remove the offset; addKdonly if needed for damping. - Ziegler-Nichols and relay auto-tune: classic recipes from the gain at which the loop oscillates. They give a starting point, usually aggressive.
- Model-based: with a plant model, choose gains to place the closed-loop poles; MATLAB, Python's
python-controlpackage or Simulink do this in industry. - Limits: real actuators saturate (a heater can't go below 0 W), so production code clamps
uand uses anti-windup (stop integrating while saturated).
Safety in real systems
Closed-loop control of motors, heaters and power converters can fail in dangerous ways (runaway speed, overheating, overvoltage). Real controllers include independent hardware limits and interlocks, and their tuning on live equipment is done by trained staff following site procedures. Practise in simulation only.
Common mistakes
- Expecting P alone to reach the setpoint on a plant without its own integrator.
- Huge integral gain, giving overshoot and slow oscillation.
- Derivative kick: a setpoint step makes
de/dtspike; take the derivative of the measurement instead. - A time step too large compared with the plant's time constant, so the simulation itself goes unstable.
- No output limits or anti-windup in code that will meet real actuators.
Recap
- Feedback acts on the error
e = r - y. - P reacts to present error (leaves offset), I to accumulated error (removes offset), D to the rate of change (damping).
- Simulate the plant, tune for speed and low overshoot, and add saturation and anti-windup before going near hardware.
Try it
Open the Do tab to write the PID update, simulate the plant and measure steady-state error and overshoot.