PID Controller Simulator
A PID controller simulator: tune Kp, Ki and Kd for a cart or an oven and read rise time, overshoot, settling time and steady-state error.
Simulator
Use the arrow keys to move the target. Space plays and pauses.
A 1 kg cart on a track starts at 0 m and is driven to a target at 1 m against a 2 N load. It peaks at 1.163 m after 0.9055 s and settles within 2 percent of 1 m by 2.015 s. Bars above the track show the proportional, integral and derivative terms and the effort they add up to at the time on the clock.
- Overshoot (peak − final) / (final − start) × 100: the cart peaks at 1.163 m against a final 1 m. A second-order loop overshoots by 100 e^(−ζπ/√(1 − ζ²)) percent, 16.3 at ζ = 0.5.
- 16.31 %
- Rise time, 10 to 90 percent The time to go from 10 to 90 percent of the way from the start to the final value: from 0.1219 to 0.5306 s.
- 0.4087 s
- Settling time, 2 percent The last time the output is more than 2 percent of the step, 0.02 m, from its final value; it stays inside the band after that. Nise’s estimate for a second-order loop is 4/(ζωn).
- 2.015 s
- Steady-state error Target less final position. The integral term keeps adding force while any error is left, so the cart settles on the target whenever the motor can hold the load.
- 0 m
- Peak time When the output reaches its peak, 1.163 m. A second-order loop peaks at π/(ωn√(1 − ζ²)).
- 0.9055 s
- Largest force The largest force in the run. With the derivative on the measurement the start is Kp × step = 18 × 1 = 18 N, with no kick.
- 18 N
- Force at rest At rest the proportional and derivative terms are zero, so the integral term alone balances the 2 N load.
- 2 N
- Position now Where the cart is at the time on the clock, exact between samples.
- 0 m
- Force now The controller’s last output, held until the next sample: the zero-order hold.
- 18 N
- Target
- Position
- 2 percent band
- Force on the cart
- Proportional term
- Integral term
- Derivative term
Citing this tool
Last updated . Add the date you accessed it as well, which a citation of a page that can change asks for. If a specific result matters, cite the permalink from the tool’s share row instead of this page: it reproduces the exact parameters.
The equation
Åström and Murray, Feedback Systems (2008)
What is a PID controller?
A PID controller is a feedback rule that sets an actuator’s effort from the error, the gap
e = r − y between the setpoint r and the measured output y, as the sum of three terms:
u = Kp e + Ki ∫e dt + Kd de/dt. The proportional term pushes in proportion to the
error now, the integral term in proportion to the error added up so far, and the derivative
term in proportion to how fast the error is changing. Kp, Ki and Kd are the gains you tune.
Some form of this rule, often with the derivative term switched off, runs most of the feedback
loops in industry, from a car’s cruise control to the hot end of a 3D printer.
How well a tuning works is judged from the response to a step in the setpoint, using four standard figures: the rise time, the percentage overshoot, the settling time and the steady-state error. This simulator measures all four on the exact response of a sampled controller driving one of two plants, and the sections below show where each figure comes from.
Using the simulator
Choose a plant: a cart that the controller drives along a track to a target flag, or an oven it heats to a target temperature. The three gains follow the plant in the parameters. Below them are the target, the load and the cart’s mass or the heater’s rating, and the options: a limit on the cart’s force, anti-windup, differentiating the error instead of the measurement, a filter on the derivative, and the sample period. Press Play to watch the step response, drag the scrubber to move through it by hand, or select the scene and use the arrow keys to move the target.
The bars at the top of the scene show the three terms and the effort u they add up to at the time on the clock, with the sum outlined behind u while the actuator is at its limit, so you can watch the proportional term hand over to the integral term as the error closes. The first plot draws the output against the target with the 2 percent band that the settling time is measured against. The second draws the effort, with the three terms it is made of. Every setting goes into the page’s link, so a tuning can be shared exactly.
Worked example: a 1 kg cart against a 2 N load
The simulator opens on a 1 kg cart with b = 1 N s/m of drag, a 2 N load pulling it back and a target 1 m away. The gains are Kp = 18 N/m, Ki = 8 N/(m s) and Kd = 3.5 N s/m, and the controller samples at 1 kHz with the derivative taken on the measured position.
-
The cart obeys m x″ + b x′ = u − F. With the PID rule for u, the closed loop’s characteristic
polynomial is m s³ + (b + Kd)s² + Kp s + Ki, which here is
s³ + 4.5s² + 18s + 8 = (s + 0.5)(s² + 4s + 16). -
The complex pair has
ωn = √16 = 4 rad/sandζ = 4/(2 × 4) = 0.5, and the third pole is at −0.5. -
The target and the load enter through
(Kp r − F)s + Ki r = 16s + 8 = 16(s + 0.5), which cancels the slow pole, so the position follows the standard second-order step response. -
Overshoot:
100 e^(−ζπ/√(1 − ζ²)) = 100 e^(−π/√3) = 16.30%. Peak time:π/(ωn√(1 − ζ²)) = π/(2√3) = 0.9069 s. Settling time, roughly:4/(ζωn) = 4/2 = 2 s. -
At the start the force is
Kp × 1 m = 18 N, with nothing in the integral yet and no derivative kick, so the cart sets off at(18 − 2)/1 = 16 m/s². - At rest the proportional and derivative terms are both zero, so the integral term alone supplies the 2 N that balances the load, and the steady-state error is 0.
The readouts give 16.31% overshoot, a rise time of 0.4087 s, a peak at 0.9055 s and a settling time of 2.015 s. The exact continuous response rises in 0.4094 s and settles at 2.019 s, so sampling at 1 kHz moves each figure by less than 0.2 percent. The 2 s settling time is Nise’s estimate from the decaying envelope; the true time to stay inside the 2 percent band is a little longer.
What each of the three terms does
The proportional term on its own makes the loop a spring: Kp is a stiffness, so the cart rings
at ωn = √(Kp/m) with only the track’s drag to damp it, ζ = b/(2√(m Kp)). Set Ki and Kd to 0 and
the opening cart has ζ = 1/(2√18) = 0.118, overshooting where it comes to rest by
about 69 percent. It also stops short. A proportional term pushes only while there is an
error, so to hold the load it needs an error of F/Kp = 2/18 = 0.1111 m, and the
cart settles at 0.8889 m.
The derivative term opposes the cart’s speed, so it adds damping. With Kd back at 3.5 N s/m,
ζ = (b + Kd)/(2√(m Kp)) = 4.5/(2√18) = 0.530 and the overshoot falls to 14.0 percent,
though the cart still stops 0.1111 m short. A PD-controlled cart obeys the same equation as a
damped mass on a spring of stiffness Kp, the system the
mass spring simulator animates, with the derivative
gain as extra damping.
The integral term removes the offset. It keeps growing while any error is left, so the cart can only come to rest where the error is zero, with the integral term holding the load. The price is a slow extra mode and more overshoot. Set the load to 0 and the cancellation in the worked example no longer happens: the same gains overshoot by 27.6 percent and take 3.84 s to settle.
Rise time, overshoot and settling time
These are the step response figures of Nise’s Control Systems Engineering. The rise time runs from 10 to 90 percent of the way from the start to the final value. The percentage overshoot is how far the peak passes the final value, as a share of the step. The settling time is the last moment the output is more than 2 percent of the step from its final value, so a response that enters the band and swings back out has not settled until it stays in. The steady-state error is the target less the final value. The simulator measures each on the exact output, between samples as well as at them, rather than reading them off the samples.
For a second-order loop the overshoot depends only on the damping ratio,
%OS = 100 e^(−ζπ/√(1 − ζ²)): 16.3 percent at ζ = 0.5, 4.6 percent at ζ = 0.7 and none
from ζ = 1. The same formula gives the overshoot of a series RLC circuit, which the
RLC circuit simulator measures, because the
capacitor’s voltage obeys the same equation as the cart’s position. The position plot is itself
a motion graph, and its gradient is the cart’s velocity, as the
motion graphs simulator explains.
Actuator saturation and integral windup
Every real actuator has limits. The oven’s heater gives between nothing and its rating, 2500 W
to begin with, and the oven is a 10 kJ/K heat capacity losing 5 W/K to a 20 °C room, so its time
constant is C/G = 10,000/5 = 2000 s. Holding 180 °C takes
5 × (180 − 20) = 800 W, and flat out the oven heads for
20 + 2500/5 = 520 °C, passing 180 °C after
−2000 ln(1 − 160/500) = 771.3 s, about 13 minutes.
With Kp = 100 W/K the controller asks for 16,000 W at the start, so the heater sits at its limit while the oven warms, and the loop is open while it does. An integral term that keeps adding up the error meanwhile winds up. With anti-windup off it reaches 14,460 W, the heater stays full on long after 180 °C until the error above the target has paid the integral back, and the oven overshoots by 47.9 percent, to 256.6 °C, taking 36.5 minutes to settle. Anti-windup here is conditional integration: the integral is held while the heater is saturated and the error would push it further. With it on, the integral never passes 810 W, the overshoot is 0.04 percent and the oven settles in 15.0 minutes. The heater is full on for exactly 630 s, until the proportional term alone drops below 2500 W at 155 °C.
The cart shows the same thing. Limit its force to 5 N and the opening loop overshoots by 2.2 percent with anti-windup and by 25.3 percent without it.
Derivative kick and the derivative filter
A step in the target is a step in the error, and a step has no finite derivative. A digital
controller differencing the error over one sample sees
Kd Δe/h = 3.5 × 1/0.001 = 3500 N. Switch on Differentiate the error and the largest
force jumps from 18 N to 3518 N for one millisecond, throwing the cart forward at about
3.5 m/s. That spike is derivative kick. Differentiating the measured position instead gives
exactly the same feedback, since the target does not move after the step, but no kick, which
is why it is the default here.
A filter on the derivative, Kd s/(Tf s + 1), limits how fast the derivative can respond, so
that sensor noise is not amplified without limit. It does not remove the kick. With
Tf = 0.02 s the first sample gives Kd/(Tf + h) = 3.5/0.021 = 166.7 N, but the kick
then decays over about Tf, and its impulse, Kd Δe = 3.5 N s, is the same as before.
Why the sample period matters
A digital controller reads the output, computes u and holds it until the next sample, which is called a zero-order hold. Holding acts much like delaying the signal by half a sample, and the derivative’s difference over the last sample adds about another half. Lag eats into the loop’s phase margin, which is read off a Bode plot of its frequency response, and the Bode plot visualiser shows how gain and phase change with frequency. On the opening cart, sampling every 100 ms raises the overshoot from 16.31 to 20.22 percent, every 200 ms raises it to 47.12 percent, and beyond 240 ms the loop is unstable, with each swing larger than the last.
Nothing here is stepped numerically. Between samples the cart and the oven follow the exact solutions of their equations with the effort held constant, the zero-order-hold discretisation of Franklin, Powell and Workman’s Digital Control of Dynamic Systems. The sample period is a setting of the controller being modelled, not an accuracy setting, and every figure is exact for the controller it describes.
Tuning the gains
When the plant is known, place the poles. Matching the cart’s polynomial,
m s³ + (b + Kd)s² + Kp s + Ki, to m(s + p)(s² + 2ζωn s + ωn²) gives Kd = m(p + 2ζωn) − b,
Kp = m(ωn² + 2ζωn p) and Ki = m p ωn². The opening gains come from p = 0.5 s⁻¹, ζ = 0.5 and
ωn = 4 rad/s: Kd = 4.5 − 1 = 3.5, Kp = 16 + 2 = 18 and
Ki = 0.5 × 16 = 8.
On a plant with no model, a common order is to raise Kp until the response is fast enough, add Kd until the overshoot is acceptable, then add the smallest Ki that removes the steady-state error in a reasonable time, and finally check the response with the actuator at its limit. The closed-loop rules of Ziegler and Nichols start from the gain at which a proportional loop oscillates steadily, which the cart never reaches in continuous time, since m s² + b s + Kp is stable for every positive Kp.
What this model leaves out
- Noise. The measurement is perfect, so the derivative filter’s real job, keeping sensor noise out of the derivative, is described rather than shown, and every run is exactly reproducible.
- Delay and sensor lag. The controller reads the output the instant it samples and the new effort starts at once. A real controller takes part of a sample to compute, and a real thermometer lags the oven it sits in.
- Friction that sticks. The cart has only viscous drag. A real drive has static friction too, and an integral term pushing against it until it breaks free makes the cart lurch back and forth around the target, which is called hunting.
- Quantisation. The controller computes with exact numbers, where a real one reads its sensor through a converter with a finite number of levels.
- A real oven. The oven is one lumped heat capacity with a loss proportional to its rise above the room. A real one has walls, air and food heating at different rates, loses more by radiation as it gets hotter, and has an element that takes time to warm up.
Common mistakes
- Using the integral term to cure overshoot. It adds overshoot. Damping comes from the derivative term, or from less proportional gain.
- Expecting a derivative filter to remove the kick. It spreads the kick over about Tf. Only differentiating the measurement removes it.
- Reading the settling time as the first entry into the band. It is the last exit, after which the output stays inside.
- Tuning with the actuator unlimited. A loop that looks well damped can overshoot badly once the actuator saturates and the integral winds up.
- Mixing up gain conventions. These are parallel gains. The standard form writes Kp(1 + 1/(Ti s) + Td s), so Ki = Kp/Ti and Kd = Kp Td, and an integral time from a data sheet is not an integral gain.
- Measuring overshoot from the target. It is measured from the final value, which is the target only when the steady-state error is zero.
Model and assumptions
- Method
- Exact expression, no time stepping
- Repeatability
- Deterministic. The same link gives the same numbers on any machine.
What it assumes
- The controller is digital by design: every sample period it reads the output, works out the proportional, integral and derivative terms, and holds their sum until the next sample, a zero-order hold.
- Between samples each plant follows the exact solution of its linear equation with the effort held constant, so the run is exact at every sample and in between, and the sample period is a setting of the controller rather than a numerical step.
- The cart is a mass on a track with 1 N s/m of viscous drag and a constant load pulling it back, as gravity does on a track tilted uphill, with no static friction.
- The oven is one lumped heat capacity of 10 kJ/K losing 5 W/K to a 20 °C room, so its time constant is 2000 s and its whole interior is at one temperature.
- The integral is summed by forward Euler and the derivative is a backward difference passed through a first-order filter of time constant Tf, taken on the measurement unless the error is chosen, as in Åström and Murray’s Feedback Systems.
- Anti-windup is conditional integration: the integral is held whenever the actuator is at a limit and the error would drive it further past that limit.
- The measurement has no noise, no delay and no quantisation, and each new effort is applied at the instant the output is read.
Where it stops holding. Drives with static friction, where the integral term builds up until the friction gives way and the cart lurches past the target and back again, a stick-slip oscillation called hunting, and plants with dead time, such as a heater whose sensor sits far from its element. The Inclined Plane Simulator is the right tool there.
Numerical accuracy
No numerical integration, so nothing accumulates: between samples the cart and the oven follow the exact solutions of their equations with the controller’s output held, the zero-order-hold discretisation, so the run is exact to rounding at every sample and in between. The sample period is part of the controller being modelled, not an accuracy setting. The peak is found where the cart’s velocity changes sign, from its exact expression; the rise and settling times are found by halving an interval on the exact output until it is settled to the limit of double precision; and whether the loop is stable comes from the Schur-Cohn test on the sampled loop’s characteristic polynomial, which decides it without finding the poles, with the slowest pole’s size found by halving the radius until the test changes its answer.
Common questions
How do you tune a PID controller?
If the plant is known, place the closed-loop poles. For this simulator’s 1 kg cart with 1 N s/m of drag, matching s³ + (1 + Kd)s² + Kp s + Ki to (s + 0.5)(s² + 4s + 16) gives Kp = 18 N/m, Ki = 8 N/(m s) and Kd = 3.5 N s/m: a damping ratio of 0.5 and 16.3 percent overshoot. Without a model, raise Kp until the response is fast enough, add Kd until the overshoot is acceptable, then add the smallest Ki that removes the steady-state error, and check the response with the actuator at its limit.
What is integral windup?
Windup is the integral term growing while the actuator is saturated and cannot respond to it. Here a 2500 W oven heater driven towards 180 °C with Kp = 100 W/K and Ki = 0.25 W/(K s) is full on for most of the warm-up, and without anti-windup the integral term climbs past 14,000 W, so the heater stays on long after the target and the oven overshoots to 256.6 °C. Holding the integral while the heater is saturated, called conditional integration or clamping, brings the overshoot down to 0.04 percent.
Why does a proportional controller leave a steady-state error?
A proportional term pushes only while there is an error, so holding a steady load F takes an error of F/Kp: a 2 N load on a cart with Kp = 18 N/m leaves it 0.1111 m short. With no load a cart reaches its target on proportional control alone, because nothing has to be held. An oven always needs power to replace its heat loss, so with Kp = 100 W/K and no integral term it settles 7.619 °C below 180 °C. An integral term removes the error by building up the holding effort itself.
What is derivative kick?
It is a spike in the controller’s output when the setpoint steps, because a step change in the error has no finite derivative. A digital controller sees Kd Δe/h for one sample: 3.5 N s/m × 1 m / 0.001 s = 3500 N on this simulator’s cart, against 18 N without the kick. Differentiating the measured output instead of the error gives the same feedback with no kick. A derivative filter only spreads the kick out, since its impulse is still Kd times the step.
How is percentage overshoot related to the damping ratio?
For a second-order system with no zeros, %OS = 100 exp(−ζπ/√(1 − ζ²)), so the overshoot depends only on the damping ratio ζ: 16.3 percent at ζ = 0.5, 4.6 percent at ζ = 0.7 and none from ζ = 1. The peak comes at π/(ωn√(1 − ζ²)) and the 2 percent settling time is about 4/(ζωn). A PID loop has an extra pole and zero, so for it these are estimates, and the simulator measures the real figures.
How fast should a digital PID controller sample?
Fast enough that holding the output between samples, which acts like a delay of about half a sample, costs little phase at the loop’s crossover. For this simulator’s opening cart, with ωn = 4 rad/s, sampling at 1 kHz moves the rise, peak and settling times by under 0.2 percent from the continuous design. At 10 Hz the overshoot grows from 16.31 to 20.22 percent, and with more than 240 ms between samples the loop is unstable.