Computing Library › Worked Examples
Worked Examples

Tuning a PID Control Loop

Drive a process to a setpoint with proportional, integral, and derivative terms, and understand what each knob does.

The controller

A PID controller computes an actuation u from the error e = setpoint - measurement: u = Kp e + Ki integral(e) + Kd de/dt. The three terms respond to the present error, the accumulated past error, and the predicted future error.

What each term does

Kronos motion — control room
python
import numpy as np
Kp,Ki,Kd=2.0,1.0,0.5; dt=0.05
y=0.0; sp=1.0; integ=0.0; prev=0.0
tau=1.0                               # first-order plant
for n in range(200):
    e=sp-y
    integ+=e*dt; deriv=(e-prev)/dt; prev=e
    u=Kp*e+Ki*integ+Kd*deriv
    y+=dt*(-(y)/tau+u/tau)            # plant response
print('final output:',round(y,4),'(setpoint 1.0)')

Tuning approach

A common recipe: raise Kp until the loop oscillates steadily, note that gain and period, then set the three gains from Ziegler-Nichols rules and refine. Modern practice favors gentler tunings that trade a little speed for robustness and less overshoot. Always test against disturbances, not just setpoint steps.

Practical safeguards

Real loops need integral anti-windup (clamp the accumulator when the actuator saturates), a filter on the derivative term to reject noise, and limits on the output. Without these, a textbook PID can wind up, chatter, or command impossible actions. The three-term core is simple; the safeguards make it dependable in a real plant.