Robots and robotics
What is a PID controller? P, I and D explained simply
Cruise control, a drone holding level, a robot arm stopping exactly on its mark: most of them run the same small rule, many times a second. Here's what each of its three parts does, worked through with real numbers.
A PID controller is a feedback rule that holds something at a target, like a car's speed or a robot joint's angle. Many times a second it measures the error (target minus actual) and adds three terms to set its output. P pushes in proportion to the error right now. I adds up past error, which removes the gap P leaves behind. D reacts to how fast the error is changing, which damps overshoot. Tuning means choosing how much each term counts.
What a PID controller does, using cruise control as the example
Think about how you hold a car at a steady speed without cruise control. You glance at the speedometer. If you're too slow, you press the gas a bit harder. If you've been too slow for a while, you press harder still. If the needle is climbing fast toward your target, you ease off before you get there so you don't overshoot. You're running a feedback loop: measure, compare, correct, repeat.
A PID controller is that loop written down as arithmetic. A few terms first. The setpoint is the target, say 20 m/s (about 45 mph). The measurement is what the sensor reads right now. The error is setpoint minus measurement, so if you're doing 18 m/s, the error is 2 m/s. The output is what the controller sends to the machine, here how hard the engine pushes.
PID stands for proportional, integral, derivative. Each term answers one question about the error:
- P (proportional): how far off am I right now? Push harder the bigger the error.
- I (integral): how much have I been off, added up over time? Keep pushing harder while a gap persists.
- D (derivative): how fast is the error changing? Ease off when you're closing in quickly.
The output is just the three added together, each multiplied by its own gain (a tuning number that says how much that term counts):
output = Kp × error + Ki × (sum of error over time) + Kd × (rate of change of error)
That's the standard form in Karl Åström and Richard Murray's control textbook Feedback Systems, which notes that PID controllers were originally called three-term controllers. The same chapter opens with a 2002 survey by L. Desborough and R. Miller of over eleven thousand controllers in refining, chemicals and pulp and paper, in which 97% of regulatory controllers used PID feedback.
The idea is old. In 1922 the engineer Nicolas Minorsky analyzed the three-term controller for automatically steering ships, starting from how a good helmsman behaves. As the control historian Stuart Bennett recounts, Minorsky wrote that an efficient helmsman keeps a ship on course with a properly timed meeting and easing action on the rudder, and his system was tested on the battleship New Mexico in 1923.
In software, one pass of the loop is a handful of lines. It runs on a fixed timer, every dt seconds:
error = setpoint - measured
integral = integral + error * dt
derivative = (error - previous_error) / dt
output = Kp*error + Ki*integral + Kd*derivative
previous_error = errorThe rest of this article is about what each of those three terms does, and what goes wrong when they're set badly.
P: why pushing in proportion to the error always falls a little short
Start with P alone, on a simple made-up car. It weighs 1,000 kg, and to keep things easy, air drag and rolling resistance together push back with 50 N for every m/s of speed. (Real drag grows faster than that at high speed, but the lesson doesn't change.) To hold 20 m/s on a flat road, the engine has to push with 50 × 20 = 1,000 N.
Give the controller a proportional gain Kp = 500 N per m/s of error. That means it asks for 500 N of push for every 1 m/s the car is below the setpoint.
Here's the catch. A P controller only pushes when there's an error. To produce the force the car needs, there has to be an error. So the car slows until the push from the error exactly matches the drag. Set the two equal: 500 × (20 − v) = 50 × v, which gives v = 18.18 m/s. The car settles 1.82 m/s short of the target and stays there.
| Proportional gain Kp | Speed it settles at | Left-over error |
|---|---|---|
| 250 N per m/s | 16.67 m/s | 3.33 m/s |
| 500 N per m/s | 18.18 m/s | 1.82 m/s |
| 1,000 N per m/s | 19.05 m/s | 0.95 m/s |
| 2,000 N per m/s | 19.51 m/s | 0.49 m/s |
That permanent gap is called steady-state error, or offset, the word J. G. Ziegler and N. B. Nichols used in their 1942 paper on controller settings. Doubling the gain roughly halves it, but it never reaches zero, because zero error would mean zero push.
So why not just use a huge gain? Because on any real machine there's lag: the engine takes time to respond, the sensor takes time to report. With lag, a big gain overreacts, overshoots and swings back. Åström and Murray show this on a simple process with lag: proportional gains of 1, 2 and 5 leave steady-state errors of 0.5, 0.33 and 0.17, and the system becomes more oscillatory as the gain rises. Ziegler and Nichols put the limit bluntly: control with infinitely high proportional response is always unstable, oscillating continuously.
In the toy car above there's no lag, so it can't oscillate, but a gain of 2,000 N per m/s would still demand an extra 10,000 N the instant you raised the setpoint by 5 m/s. No engine can do that. P on its own leaves you choosing between a gap and a twitchy machine.
I: how adding up the error over time removes the offset
The integral term keeps a running total of the error, multiplied by time. For cruise control it has a neat physical meaning. If you're 2 m/s slow for 3 seconds, the running total is 6 meters: that's how far you've fallen behind an imaginary car that held exactly 20 m/s the whole time.
Ziegler and Nichols called this effect "automatic reset" and said its only purpose is to eliminate offset. The name comes from older controllers, where a fixed baseline added to the output, called the reset, had to be adjusted by hand until the offset went away. Integral action does that adjusting automatically.
Watching the offset disappear
Take the same 1,000 kg car, with drag of 50 N per m/s, sitting at 18.18 m/s under P control with Kp = 500. Now add an integral gain Ki = 100 N per meter of accumulated error.
- The error is 1.82 m/s, so the running total grows by 1.82 m every second, and the I term adds about 182 N of push per second.
- The extra push speeds the car up, so the error shrinks, so the I term grows more slowly.
- The I term stops growing only when the error is exactly zero. By then it's supplying the whole 1,000 N the car needs at 20 m/s, and the P term is supplying nothing.
That last point is the reason integral action exists. Åström and Murray prove it in a few lines: if a controller with an integral term settles at all, it can only settle with the error equal to zero, because any leftover error would keep the integral growing. The proof doesn't even need to know how the car works.
Driving onto a hill
Now the car, holding 20 m/s, starts up a 4% grade. Gravity adds 1,000 kg × 9.81 m/s² × 0.04 ≈ 392 N of drag.
- P only (Kp = 500): the car loses another 392 / (500 + 50) = 0.71 m/s and stays slower for as long as the hill lasts.
- P plus I (Kp = 500, Ki = 100): the speed dips by about 0.50 m/s, bottoming out roughly 3.3 seconds after the hill starts. Then the integral builds up the missing 392 N, and within about 15 seconds the car is back within a few hundredths of 20 m/s.
Too much I causes its own trouble. With Ki = 1,000 the dip is smaller, about 0.27 m/s, but the speed then swings past the target by about 0.11 m/s before settling, in waves roughly 6.5 seconds apart. The integral has momentum of its own: it keeps pushing until the error changes sign, so it tends to overshoot. The PX4 drone autopilot's tuning guide gives the same symptom: if the I gain is too high, you will see slow oscillations.
D: braking against how fast the error is changing
Cruise control mostly gets by with P and I. That's typical: Åström and Murray note that most PID controllers do not use derivative action, so strictly speaking they're PI controllers. D earns its place when the thing you're controlling coasts, like a position. A car's speed doesn't run past its target on its own momentum. A robot arm swinging toward an angle does.
A cart that won't stop swinging
Picture a 1 kg cart on a frictionless rail, a stand-in for one joint of a robot arm. It starts at 0 and you want it at 1 m. Use P only, with Kp = 4 N per meter of error.
The spring-like push accelerates the cart the whole way to the target. By the time it arrives, the error is zero but the cart is moving at 2 m/s. P has nothing to say about speed, so the cart sails on to 2 m, gets pushed back, and swings between 0 and 2 m every 3.1 seconds, forever. Real friction would slowly calm it, but that's luck, not control.
The D term fixes this by looking at how fast the error is changing. When the cart is rushing toward the target, the error is shrinking fast, and D pushes back in proportion to that speed. It's a brake that's strongest when you're moving fastest. Åström and Murray describe P plus D as acting on a prediction of the error a short time Td = Kd / Kp into the future, found by extending the current trend in a straight line.
| Derivative gain Kd | Overshoot past 1 m | Time to settle within 2 cm |
|---|---|---|
| 0 (P only) | 100% (reaches 2 m) | never |
| 2 N per m/s | 16% (peaks at 1.16 m after 1.8 s) | about 4 s |
| 4 N per m/s | none | about 2.9 s |
| 8 N per m/s | none | about 7.4 s |
Notice the last row of the table. Past a point, more D just makes the cart sluggish: at Kd = 8 it creeps in and takes 7.4 seconds. There's a sweet spot, and here it's around Kd = 4.
The price of D: it amplifies noise
D works from the rate of change of a measurement, and measurements jitter. Say the loop runs 1,000 times a second and the position sensor's reading flickers by 1 mm between two samples. To the D term that looks like 0.001 m in 0.001 s, a speed of 1 m/s that never happened. With Kd = 4, that's a 4 N kick, as big as what P produces for a full meter of error. That's why practical controllers filter the derivative. Åström and Murray describe replacing the pure derivative with a filtered one whose time constant is set by a factor N in the range 2–20. PX4's drone tuning guide describes the symptom of too much D: the motors become twitchy (and maybe hot), because the D term amplifies noise.
How to tune a PID controller, and what each mistuned gain looks like
Tuning means picking the three gains. There's no single right answer, because it's a trade between speed, overshoot and sensitivity to noise. There are two broad ways to do it: read the symptoms and adjust by hand, or run a quick experiment and plug the result into a rule.
Reading the symptoms
The open-source PX4 drone autopilot runs a rate controller built from three independent PID controllers, one per axis of rotation. Its tuning guide lists what each wrong setting looks like:
| Gain | Too low | Too high |
|---|---|---|
| P | reacts slowly to input changes | high-frequency oscillations |
| I | drifts off the target over time | slow oscillations |
| D | overshoots after a sudden change | twitchy, possibly hot motors, because D amplifies noise |
Those symptoms line up with the worked examples above. Too little D is the cart that overshoots. Too much I is the car that swings past 20 m/s in slow waves.
The Ziegler–Nichols rule, with numbers
In 1942 Ziegler and Nichols, both at Taylor Instrument Companies, published the first PID tuning rules. One version works like this: turn off I and D, raise P until the system just keeps oscillating at a steady size, and write down that gain (the critical gain, Kc) and the time for one swing (the critical period, Tc). Their original paper called this point the "ultimate sensitivity". The PID settings, in the form Åström and Murray tabulate, are Kp = 0.6 Kc, an integral time of 0.5 Tc, and a derivative time of 0.125 Tc.
Say the system starts oscillating steadily at Kc = 10, with each swing taking Tc = 2 s. Then:
- Kp = 0.6 × 10 = 6
- integral time = 0.5 × 2 = 1 s, so Ki = Kp / 1 s = 6
- derivative time = 0.125 × 2 = 0.25 s, so Kd = Kp × 0.25 s = 1.5
Treat that as a starting point, not a finish. Åström and Murray say the rules had a huge impact but have two severe drawbacks: too little process information is used, and the closed loop systems that are obtained lack robustness. In their worked example the original rule gives a large overshoot in the control signal, and the improved rules they show typically give lower gains. The same chapter describes the modern version of the experiment: many commercial PID controllers tune themselves using relay feedback, triggered by pushing a button. And deliberately driving a real robot into steady oscillation isn't always safe, which is one reason people tune in simulation first.
Where beginners go wrong with PID controllers
Integrator windup: the hill that makes the car speed
Every real actuator has a limit. A throttle can't open past fully open, and a motor can't spin past its top speed. When the controller asks for more than the actuator can give, the actuator is saturated. The integral doesn't know that. It keeps adding up error, so the controller's request climbs far past anything the machine can do. MathWorks' documentation calls this a condition known as winding up, where the controller is operating in a region where increasing the control signal has no effect on the output.
Here's what that does to the toy car from earlier (1,000 kg, drag of 50 N per m/s, Kp = 500, Ki = 100), now with an engine that tops out at 1,500 N. It hits an 8% hill that lasts 30 seconds. The hill adds about 785 N, so holding 20 m/s would need 1,785 N. The engine can't, and the car slows toward 14.3 m/s, the speed where 1,500 N just balances drag plus hill.
- Over those 30 seconds the car falls about 80 m behind the imaginary 20 m/s car. The I term grows from 1,000 N to roughly 9,000 N, and the total request reaches about 11,000 N, more than seven times what the engine can deliver.
- The hill ends with the car at about 15.6 m/s. The engine stays flat out, the car speeds up, and it passes 20 m/s after about 7 seconds. But the I term is still huge, and it only shrinks while the car is going too fast.
- So the car keeps accelerating. It reaches roughly 26 m/s (about 58 mph) around 25 seconds after the hill ends before the controller finally backs off.
The fix is anti-windup. The simplest kind, sometimes called clamping or conditional integration, stops adding to the integral while the actuator is saturated. With that one change, the I term stays near its 1,000 N during the hill, the car is back near 20 m/s about 6 seconds after the hill ends, and it barely overshoots. MathWorks also describes back-calculation, which uses a feedback loop to unwind the PID Controller block internal integrator. Åström and Murray simulate the same effect on a cruise controller climbing a 6° slope, where windup resulting in a large overshoot and anti-windup eliminates it.
Four more common mistakes
- Cranking up P to kill the offset. The P table above shows the offset shrinking but never vanishing, and on real hardware with lag, the bigger gain brings oscillation. Removing a steady offset is the I term's job.
- Taking a raw derivative of a noisy sensor. A 1 mm flicker at 1,000 samples a second reads as 1 m/s of fake speed. Filter the derivative, or the D term spends its effort fighting noise. If your position comes from sensors that drift or jump, that's a separate problem worth understanding first: how a robot estimates where it is.
- Derivative kick. If D acts on the error and you change the setpoint suddenly, the error jumps in one sample, and its rate of change is enormous for that instant. Åström and Murray note that a step in the setpoint makes the derivative term produce an impulse. The usual fix, which PX4 uses, is to put the derivative term on the feedback path, so D looks at how fast the measurement changes, not the error.
- Forgetting the time step. If your code adds up the error without multiplying by
dt, or divides by adtit never measured, then running the loop twice as fast silently doubles Ki and halves Kd. Tune with the real loop rate, and keep it fixed.
One honest limit on all of this: PID only reacts to error that has already appeared. It can't see a hill coming, and it can't make up for a sensor that's lying. It's popular because it works well on a huge range of machines without anyone needing a detailed model of them, not because it's the best possible controller for any one of them.
Common questions
What's the difference between a PI and a PID controller?
A PI controller leaves out the derivative term. Many real controllers are PI, because D amplifies sensor noise and isn't needed when the thing being controlled doesn't coast past its target, like a car's speed or a room's temperature.
Why does my PID controller oscillate?
Usually the P gain is too high, the I gain is too high, or there's lag in the loop. Fast, small oscillations point to too much P, slow wide ones point to too much I, and overshoot after every change usually means too little D.
Is PID still used, or has AI replaced it?
It's still everywhere. A 2002 survey of over eleven thousand industrial controllers found 97% used PID, and drone autopilots such as PX4 still use PID loops to control how fast the drone rotates.
What is a setpoint in a PID controller?
The setpoint is the target value the controller tries to hold, like 20 m/s for cruise control or 90 degrees for a robot joint. The error is the setpoint minus the current measurement.
Learn it properly
Physical AI & Robotics Engineering has a full lesson on control, from PID to model predictive control, alongside dynamics, force control and state estimation. It's the advanced robotics path, so if you've never worked with a robot, start with Robotics Foundations, which covers how hard a robot should correct and why delays make it wobble.
See Physical AI & Robotics Engineering in the Toorova catalog →
Sources
- Feedback Systems: An Introduction for Scientists and Engineers, ch. 10 PID Control (Åström & Murray) · Princeton University Press (copy hosted on an MIT course site)
- Nicolas Minorsky and the Automatic Steering of Ships (Bennett, IEEE Control Systems Magazine, 1984) · IEEE (copy hosted at NTNU)
- Optimum Settings for Automatic Controllers (Ziegler & Nichols, Transactions of the ASME, 1942) · ASME (reproduced with permission)
- Multicopter PID Tuning Guide (Manual/Advanced) · PX4 Autopilot documentation
- Anti-Windup Control Using PID Controller Block · MathWorks Simulink documentation
Read next
How does a robot know where it is?
Robots count wheel turns, read an IMU and check GPS or landmarks, then a filter weighs each one by how far it can be trusted. Here's how, with worked numbers.
What is a Kalman filter, in plain English?
A Kalman filter blends a prediction with a noisy measurement, weighting each by how uncertain it is. Here's how it works, with a robot example worked in numbers.
What is SLAM in robotics?
SLAM lets a robot build a map of a new place while working out where it is on that map. Here's how it works, why loop closure matters, and lidar vs visual SLAM.