ToolSphere
Back to all articles
engineering

How to Tune a PID Controller, Step by Step

2026-10-158 min read

Try it: free PID Tuning Helper

Get suggested PID gains and preview a model's step response

Open →

A PID controller adds up three reactions to the error between where you are and where you want to be: P pushes in proportion to the error now, I pushes harder the longer an error lasts, and D brakes when the error is closing fast. Tuning means choosing how strongly each one acts. You can calculate starting gains from a step test, but most loops are finished by hand, on the real system, with the method below.

Before you start

  • Know your output limits. A heater can't go below 0% or above 100%, and a motor driver saturates at full PWM. The controller must know these limits (see anti-windup below).
  • Fix the sample time. Run the loop at a steady rate, typically 10–100 times faster than the system responds. Gains tuned at one rate change meaning at another unless your code multiplies by dt.
  • Make it safe to oscillate. Tuning pushes the loop to the edge of stability on purpose; keep a hand on the off switch and limit travel or temperature.

The four steps

1. P only. Set Ki and Kd to 0. Start with a small Kp and give the loop a step in setpoint (for example 20 → 50 °C). Raise Kp until the response is quick and overshoots a little. If you keep raising it, you will reach a gain where the output oscillates steadily: that is the ultimate gain Ku, and the oscillation period is Tu. Write both down.

2. Back off. Use about half the gain that caused steady oscillation (Kp ≈ 0.5 Ku). A P-only loop now settles, but usually short of the setpoint: that leftover gap is the steady-state error.

3. Add I until the error disappears. Increase Ki until the response reaches the setpoint in a reasonable time. Too little and it creeps there slowly; too much and it overshoots and oscillates slowly. A good starting point is Ki ≈ Kp ÷ (Tu ÷ 2).

4. Add D only if needed. If there is still too much overshoot, add a small Kd (start around Kp × Tu ÷ 8) to damp it. Stop if the output becomes jittery: D amplifies sensor noise, and many loops (temperature, flow) work well as PI with no D at all.

With Ku and Tu from step 1, the classic Ziegler-Nichols PID settings are Kp = 0.6 Ku, Ti = Tu ÷ 2, Td = Tu ÷ 8. Expect them to be aggressive; the PID tuning helper calculates them, along with the step-test rules, and plots the response so you can try changes before touching hardware.

Read the symptoms

What you see Likely cause Change
Fast oscillation around the setpoint Kp too high Lower Kp
Settles below the setpoint No or too little I Raise Ki
Slow, wide overshoot that rings Ki too high Lower Ki
Big overshoot after a large setpoint change, then a long recovery Integral windup Add anti-windup (below)
Output twitches, motor buzzes Kd too high or noisy sensor Lower Kd, filter the measurement
Sluggish, never overshoots, takes ages Gains too low Raise Kp, then Ki

Loop code you can use

This is the core of a PID loop in C++ (it ports directly to Arduino, C or Python). It includes the two fixes almost every textbook version leaves out:

struct Pid {
  float kp, ki, kd;
  float outMin, outMax;
  float integral = 0, lastMeasurement = 0;
  bool first = true;

  float update(float setpoint, float measurement, float dt) {
    float error = setpoint - measurement;
    // Derivative on measurement: no kick when the setpoint jumps.
    float derivative = first ? 0 : -(measurement - lastMeasurement) / dt;
    first = false;
    lastMeasurement = measurement;

    float candidate = integral + ki * error * dt;
    float out = kp * error + candidate + kd * derivative;
    // Anti-windup: only keep the new integral if the output is not saturated.
    if (out > outMax) out = outMax;
    else if (out < outMin) out = outMin;
    else integral = candidate;
    return out;
  }
};
  • Derivative on measurement uses the change in the measurement instead of the change in error. They are the same while the setpoint is steady, but when the setpoint jumps, the error-based version produces a huge one-sample "kick".
  • Anti-windup stops the integral growing while the output is pinned at its limit. Without it, the integral keeps accumulating during saturation and the loop overshoots badly once it gets there.

Call it at a fixed rate with the real elapsed time: float out = pid.update(setpoint, reading, 0.01f); every 10 ms.

We ran this code against a simulated heater (process gain 1.5, time constant 2 s, dead time 0.5 s) with Ziegler-Nichols gains and the output limited to 40%. With anti-windup it overshot by 0.9% and settled within 2% in 3.9 s. With the anti-windup line removed it overshot by 19.8% and took 14.6 s to settle.

On Arduino

The widely used Arduino PID library (by Brett Beauregard) already includes both fixes and handles the sample time for you:

#include <PID_v1.h>

double input, output, setpoint = 50;
PID pid(&input, &output, &setpoint, 1.92, 1.92, 0.48, DIRECT);

void setup() {
  pid.SetOutputLimits(0, 255);   // PWM range
  pid.SetSampleTime(10);         // ms
  pid.SetMode(AUTOMATIC);
}

void loop() {
  input = readTemperature();     // your sensor
  pid.Compute();
  analogWrite(9, (int)output);
}

Its Ki and Kd are per second, like the gains from the calculator, because it scales them by the sample time internally. Use REVERSE instead of DIRECT when increasing the output lowers the measurement, as with a cooling fan.

Common mistakes

  • Tuning with the wrong sign. If the loop runs away instead of correcting, the output direction is reversed.
  • Changing the loop rate after tuning with code that does not multiply by dt.
  • Using D on a noisy sensor. Filter the reading first, or leave D at zero.
  • Tuning at one operating point only. A loop tuned at 50 °C may behave differently at 200 °C, or with a heavier load. Test across the range you will use.

Use the Bode Plot Generator to inspect a related frequency-response example. For a related walkthrough, read Bode Plot: Gain and Phase Margin Example.

Looking for other tools?

Explore ToolSphere Homepage →