2026-10-13

PID tuning: from hand-twiddling to a relay test that measures Ku for you

Control SystemsPIDMotor ControlTuningZiegler-NicholsRelay FeedbackIMCLambda TuningServoTemperature ControlPythonSimulation
PID tuning: from hand-twiddling to a relay test that measures Ku for you

Most PID explanations end where the difficulty starts. The formula is three lines of arithmetic, and then someone says tune the gains, and the conversation stops being about control theory and becomes about craft.

That is a fair division of labour, but it leaves a practical question unanswered: when you have a plant, no model, and no appetite for pushing production equipment to the edge of stability, where do the first numbers come from? There are five honest answers, and they differ in what they cost you. This piece walks through all of them, then reports what one actually returns when you run it, because the relay test has a measurement bias that almost never gets mentioned alongside the formula.

Everything below was checked against a simulated plant rather than a real one: a first-order process with dead time, process gain 2, time constant 5 seconds and 1 second of pure delay, stepped at 10 millisecond resolution. That model is the standard test case for every tuning rule in the literature, which makes it the fairest way to compare them.

What the three terms actually do

The control law used here is the parallel form, which is what most code looks like:

u = Kp * e + Ki * integral(e) + Kd * derivative(e)

Increasing each gain moves the closed-loop response in a direction that is worth memorising, because it is the map you will use for the rest of your tuning life.

Effect of raising the gain Kp Ki Kd
Rise time faster faster slightly faster
Overshoot more more less
Settling time little change longer shorter
Steady-state error smaller, not always zero eliminated no effect
Stability worse worse usually better, worse on noise

The one-sentence version, which is worth more than the table while you are learning: P sets how fast, I removes the offset, D brakes the overshoot.

There is a trap in the notation that costs people real time. Industrial controllers describe the same controller with a proportional band, an integral time Ti and a derivative time Td, and the conversion into the parallel form is Ki = Kp / Ti and Kd = Kp * Td. A gain quoted as Ki = 2.4 means two very different things depending on which convention the speaker used, and datasheets rarely say. When you inherit a parameter set, always ask which form it is in before you compare it with anything.

Route one: hand tuning, one parameter at a time

This is still the most common method on a factory floor, and it requires nothing except the ability to watch the response.

Set Ki and Kd to zero, then raise Kp in small steps until the response shows a small overshoot or a slight oscillation, then back off. How far to back off is a judgement call rather than a percentage; the point where oscillation begins is the stability boundary, and a working value sitting on that boundary will not survive a change in load. Somewhere between half and two thirds of the value that produced visible oscillation is a normal starting point.

Then raise Ki from zero until the steady-state error disappears. The useful diagnostic here is the frequency of any oscillation that appears. Integral action produces slow oscillation with a long period, while too much proportional gain produces a fast one. A slow surge points at Ki, a high-frequency chatter points at Kp, Kd or measurement noise.

Add Kd last, and only if the loop overshoots while already tracking. Watch the controller output while you do it. If the output starts showing high-frequency spikes, the derivative is amplifying sensor noise, and the fix is either less Kd or a filter on the derivative term. On noisy measurements, PI usually beats a PID that has been forced to work.

Hand tuning is fast to start, slow to finish, and hard to reproduce. The parameters you end up with depend on the person and the load, which is exactly why the next three methods exist.

Route two: Ziegler-Nichols, or the deliberate trip to the edge

The closed-loop version of Ziegler-Nichols dates from 1942 and works by finding the point where the loop is marginally stable.

Disable integral and derivative action, then raise Kp until the loop oscillates with constant amplitude, neither growing nor decaying. The gain at that point is the ultimate gain Ku, and the period of the oscillation is Tu. Then read the gains off the table.

Controller Kp Ti Td
P 0.50 Ku none none
PI 0.45 Ku Tu / 1.2 none
PID 0.60 Ku Tu / 2 Tu / 8

With a measured Ku of 4 and Tu of 2 seconds, the PID row gives Kp 2.4, Ti 1 second and Td 0.25 seconds, which in parallel form is Ki 2.4 and Kd 0.6.

The reason to be careful is in the method rather than the arithmetic. Finding Ku means holding a live process at the boundary of instability, which is tolerable on a simulation and questionable on a heater, a large motor, or anything with a mechanical limit. The second reason is that the 1942 rules were designed for quarter-amplitude decay, a damping ratio around 0.2, which is a much livelier response than most modern processes want. The tuning is not wrong; the target is aggressive.

Route three: relay feedback, the safe version of the same idea

In 1984 Åström and Hägglund published a way to get Ku and Tu without ever raising the gain. Replace the controller with a relay: when the error is positive the output is the bias plus d, when it is negative the output is the bias minus d. The loop settles into a limit cycle, and because the output is clamped, it cannot run away.

The describing-function estimate of the ultimate gain from that cycle is:

Ku = 4 * d / (pi * a)

where d is the relay amplitude and a is the measured amplitude of the process oscillation. Feed Ku and Tu into the Ziegler-Nichols table and you have a starting point, obtained in one bounded experiment that a controller can run unattended. This is what the auto-tune button on a temperature controller is doing.

The relay output switching between plus and minus d while the process settles into a limit cycle, with the amplitude a and period Tu marked

Three details matter in practice. The relay amplitude is usually set to 5 to 10 percent of the output range, small enough to stay in the linear region of the plant and large enough to see above the noise. The test is normally stopped after two or three clean cycles. And on a noisy measurement, the relay needs a hysteresis band, otherwise it switches on noise and the measured period is meaningless.

What the relay test actually returns

Here is the part that the tutorials leave out. Run the standard recipe on our simulated plant and compare the result with the exact answer, which can be computed analytically because the model is known.

Quantity Exact, from the model Relay, asymmetric cycle Relay, cycle centred on set point
Ku 4.25 3.52 3.53
Tu 3.72 s 4.45 s 3.64 s
Error reference Ku 17 percent low, Tu 20 percent long Ku 17 percent low, Tu 2 percent short

Two separate things go wrong, and they have different fixes.

The first is a missing bias. The describing-function derivation assumes the limit cycle is centred on the operating point, which means the relay output has to include a bias equal to the steady-state output needed to hold the set point. Code that switches between plus d and minus d around zero technically runs the experiment at a different operating point, so the cycle is asymmetric and the measured period comes out about 20 percent long. Adding the bias brought Tu to within 2 percent of the true value. It is a one-line fix and it is missing from a lot of example code.

The second is harder to fix and worth understanding. The formula wants the amplitude of the first harmonic of the process oscillation, because the describing function is a first-harmonic approximation. Measuring peak-to-peak and halving it measures the peak, and on a waveform that is not sinusoidal the peak is larger than the fundamental. Larger a means smaller Ku, and in this case about 17 percent smaller.

The practical consequence is reassuring rather than alarming. A Ku that is 17 percent low produces a proportional gain of 2.12 instead of 2.55 on this plant, and the loop overshoots 33 percent instead of 44 percent. The error runs in the conservative direction, which is why nobody complains about it. Just do not treat the relay result as a precision measurement of Ku, and if you need accuracy, measure the fundamental rather than the peak or fit a model from a step instead.

Route four: the open-loop step and the reaction curve

If the plant can be stopped or switched to manual, the step test is gentler than either closed-loop method. Apply a step in open loop, record the response, and read three numbers off it: the process gain K, the time constant tau, and the dead time theta between the input changing and the output starting to move.

The classical way to extract them is to draw a tangent at the steepest point of the curve, and the corresponding Ziegler-Nichols open-loop table is:

Controller Kp Ti Td
P tau / (K * theta) none none
PI 0.9 * tau / (K * theta) 3.3 theta none
PID 1.2 * tau / (K * theta) 2 theta 0.5 theta

The tangent construction is crude, and it was devised for pencil and paper. If you already have the logged data, a least-squares fit of a first-order-plus-dead-time model costs a few lines of Python and gives K, tau and theta without arguing about where the inflection point is. The logging and sampling advice is the same either way: sample fast enough to see the dead time, record well past the point where the output settles, and take the step at the operating point you actually care about, because most plants are not linear across their full range.

Route five, for when you have the model: IMC and lambda tuning

Once K, tau and theta are known, the model-based rules take over. The simplest is internal model control, also called lambda tuning, which for a first-order-plus-dead-time plant gives a PI controller:

Kp = tau / (K * (lambda + theta))
Ti = tau

Everything about the aggressiveness of the loop is in lambda, which is the closed-loop time constant you are asking for. Small lambda means a fast, twitchy loop that trusts the model; large lambda means a slow, forgiving one that survives model error. A common floor is lambda at or above 0.8 theta, with lambda around tau giving a moderate response and several times tau giving a deliberately conservative one.

This is the method most likely to survive a design review, not because the maths is better but because there is one knob with a physical meaning. Nobody has to defend a Kd they cannot explain, and there is no derivative term to amplify noise.

The same plant, three appetites

Here is the comparison that makes the trade-off concrete. Same measured Ku and Tu throughout for the Ziegler-Nichols row, same model for the lambda row. Overshoot, rise time and settling time are measured from a set-point step.

Measured on the step Ziegler-Nichols PID Tyreus-Luyben PID Lambda / IMC PI
Kp 2.12 1.59 1.25
Ti 1.82 s 8.01 s 5.00 s
Td 0.46 s 0.58 s none
Overshoot 33.5 percent 0 percent 3.9 percent
Rise time 0.76 s 3.5 s 1.9 s
Settling to 2 percent 10.3 s 14.8 s 6.0 s

Step responses of the same plant under Ziegler-Nichols, Tyreus-Luyben and lambda tuning, with overshoot labelled on each curve

Three things fall out of that table.

Ziegler-Nichols did what it says on the tin and landed just over one third overshoot. The literature figure often quoted is 25 percent, and the difference comes from the dead time ratio rather than from the formula being wrong: with theta at a fifth of tau, the loop is fast enough that the extra phase lag bites.

Tyreus-Luyben, the relaxed Ziegler-Nichols variant that drops the proportional gain from 0.6 to 0.45 of Ku and stretches the integral time from half a period to more than two periods, is genuinely non-overshooting here. It is also slow: its rise time is nearly five times longer, and it takes about half again as long to settle.

The lambda-tuned PI beat both on settling time with 4 percent overshoot, and it did it without a derivative term at all. On a plant with modest dead time, integral action combined with a sensible bandwidth is doing the work that people expect derivative action to do.

Reading a bad response

Once you have a loop running, tuning becomes diagnosis. The mapping from symptom to the parameter to change is short.

Symptom Likely cause What to change
Slow to arrive, never gets close Kp too small raise Kp
Large overshoot or sustained oscillation Kp or Ki too large lower Kp or Ki, consider a little Kd
Settles but sits slightly off target Ki too small or zero raise Ki
Slow, long-period oscillation Ki too large lower Ki
Controller output shows high-frequency spikes Kd too large, noise amplified lower Kd, filter the derivative, or go PI
Slow and offset at the same time both Kp and Ki too small raise each in turn, not together

The table gives a direction, not a diagnosis. Oscillation in particular has causes that are not gains at all: a sampling period that is too slow for the loop, an actuator hitting its limits, a sensor mounted where it sees mechanical resonance, or stiction in a valve. Plot the set point, the process value and the controller output on the same axis before touching a parameter. The output trace usually names the culprit.

Six things that matter more than the formula

One caveat specific to the hardware this site usually covers. Every rule above assumes a self-regulating plant, meaning one that settles at a new value after a step. A position or velocity loop driven by an encoder is an integrator, and it does not settle at a new value; it ramps. Applying the standard Ziegler-Nichols tables to an integrating process produces gains that are wrong by a wide margin, and integrating-process variants of the rules exist for exactly that reason. If the plant ramps instead of settling during a step test, stop and pick a different table before you tune anything.

The three things worth keeping from all of this are smaller than the formulas. P sets speed, I removes offset, D trades noise for damping. Pick the tuning route that matches whether you can stop the plant. And expect a classic Ziegler-Nichols result to overshoot by a third rather than a tenth, because that is what it was designed to do.

← All news