Tutorial · Beginner · 18 min read
How to Tune a PID Controller: A Practical Guide
Tune a PID controller step by step: set proportional gain first, add derivative to stop overshoot, add integral last, and avoid integral windup.
Published
A PID controller turns the error between where a system is and where you want it to be into a correction. It has three terms—proportional, integral, and derivative—and tuning is the process of choosing how strongly each one acts. The reliable way to do it is one term at a time, in a fixed order, watching the response after every change. You can follow along in the PID Controller Simulator.
Start with proportional gain
Set the integral and derivative gains to zero. Raise the proportional gain (Kp) until the system responds quickly to a change in setpoint. Too little and it reacts sluggishly; too much and it overshoots and oscillates. Stop when the response is fast but only lightly oscillatory—this is your working point for adding the other terms.
A useful rule: P gives you speed. It reacts to the error right now, and nothing else.
Add derivative to tame overshoot
Now raise the derivative gain (Kd). Derivative action responds to how fast the error is changing, so it applies the brakes as the system approaches the setpoint. This is what removes the overshoot and ringing that pure proportional control leaves behind.
Add just enough derivative to settle the oscillation. Too much makes the controller twitchy, because the derivative term amplifies any noise in the measurement. If your signal is noisy, filter it before differentiating, or lean on proportional and integral instead.
Add integral last
Proportional and derivative together can still leave a small, steady offset—the system parks just short of the setpoint. The integral term (Ki) fixes that: it accumulates error over time and pushes until the offset is gone.
Add integral gain slowly and only if a repeatable steady-state error remains. Integral is the slowest term and the easiest to overdo, so a little goes a long way.
Watch for integral windup
Integral windup is the classic PID failure. When the actuator saturates—a motor already at full speed, a valve fully open—the system cannot respond, but the integral term keeps accumulating error. When the error finally reverses, all that stored-up action unwinds at once and the system overshoots badly.
Prevent it by clamping the integral (stop adding to it while the output is saturated) or by back-calculation (bleed the integral down using the gap between the requested and delivered output). A simple clamp:
float candidateIntegral = integral + error * dt;
float raw = kp * error + ki * candidateIntegral + kd * derivative;
float output = constrain(raw, outputMin, outputMax);
if (raw == output) integral = candidateIntegral; // only accumulate when not saturated
Keep the loop timing honest
All three terms depend on a consistent time step. On a microcontroller, a long delay() in the loop adds dead time that makes the derivative term useless no matter how high you set it, and it distorts the integral. Run the loop fast and at a steady interval, and pass the real elapsed time into the calculation.
Try it yourself
Open the PID Controller Simulator, zero the I and D gains, and walk through this order. Watch how overshoot, settling time, and steady-state error each respond to a single term. Once the shape makes sense here, the same intuition transfers to a real motor, heater, or the steering loop in the Line Follower Simulator.
Further reading