Skip to content
Tutorials Center
My learning

Topics

Lesson 23 of 37 · PID and Compensators

PID Controller Basics

Understand the PID controller: what the proportional, integral and derivative terms do, how each changes a step response, and how to code a PID loop in Python.

More than nine out of ten industrial control loops are PID controllers. The idea is simple: look at the error e(t) = r(t) - y(t) between the setpoint and the measurement, and compute the control signal from three views of that error: how big it is now, how long it has persisted, and how fast it is changing.

TEXT
u(t) = Kp·e(t) + Ki·∫e(t)dt + Kd·de(t)/dt

In the Laplace domain the controller is C(s) = Kp + Ki/s + Kd·s. Some books use the standard (ISA) form C(s) = Kp(1 + 1/(Ti s) + Td s) where Ti is the integral time and Td the derivative time; then Ki = Kp/Ti and Kd = Kp·Td. Both forms describe the same controller; just watch which one a tool expects.

The three terms in plain words

  • Proportional (P): react to the present. A bigger error gives a bigger push. Too little gain is sluggish and leaves a steady-state error; too much gain oscillates.
  • Integral (I): react to the past. The integral keeps growing while any error remains, so the controller keeps pushing until the error is exactly zero. This removes steady-state error, but adds phase lag and can cause overshoot.
  • Derivative (D): react to the future. The slope of the error predicts where it is heading, so the controller can brake early. This damps oscillation, but amplifies measurement noise.
flowchart LR
    R["setpoint r"] --> E(("+/-"))
    E -->|"e"| P["Kp"]
    E --> I["Ki / s"]
    E --> D["Kd s"]
    P --> S(("sum"))
    I --> S
    D --> S
    S -->|"u"| G["plant"]
    G -->|"y"| E

Parallel-form PID: three paths from the error, summed into the plant input.

Effects of each term on the step response

The table lists what happens when you increase one gain alone:

Gain up Rise time Overshoot Settling time Steady-state error
Kp shorter larger little change smaller
Ki shorter larger longer eliminated
Kd little change smaller shorter no change

These are tendencies, not laws, which is why a simulation is the best teacher. We will use a plant with three equal lags, P(s) = 1/(s+1)³, a standard test bench: slow, no overshoot on its own, and it destabilizes when the loop gain is high. The helper step_response computes an exact discrete-time step response using a matrix exponential.

Python P, PI and PID on the same plant

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Read the table the code prints:

  • Kp = 2 alone settles at Kp/(1+Kp) = 0.667: a permanent error of one third. Raising to Kp = 4 reduces the error (final value 0.8) but the response now overshoots by about 23%.
  • Adding integral action (PI 2,1) brings the final value to exactly 1, but with large overshoot (about 44%) and slow settling.
  • Adding derivative action (PID 2,1,1.5) cuts the overshoot to under 8% and settles in about 6.6 s. The D term brakes the response before the integral winds the output too far.
  • PI 2,2 shows the danger of too much integral gain: the loop becomes unstable (the output never settles).

Why steady-state error disappears with integral action

For a constant error e0, the integral term grows linearly: Ki·e0·t. The only way the controller output can stay steady is for e0 = 0. Mathematically, the 1/s in C(s) gives the open loop an integrator, and a type-1 loop has zero steady-state error to a step. (If the plant already has an integrator, like a motor position, a P controller alone suffices for step tracking, but you still need an I term to reject constant load disturbances.)

PID as code

On a microcontroller you write the controller as a loop that runs every dt seconds. The sketch below implements a textbook PID with derivative on measurement (not on error, which would kick the output every time the setpoint jumps) and runs it against the same plant integrated with small Euler steps.

Python A PID class running in a simulation loop

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The final value is exactly 1 again, but the overshoot (about 23%) is larger than the 7.5% of the transfer-function version above. The reason is the derivative on measurement: the setpoint no longer passes through the D term, so the response to a setpoint step loses the helpful zero that the Kd term added. The loop poles are identical; only the way the setpoint enters differs. The trade-off is no derivative kick and the same disturbance rejection.

Derivative kick. If the D term acts on the error, a sudden setpoint jump makes de/dt huge for one sample, producing a spike in u. Using -Kd·dy/dt (measurement only) avoids it and gives the same disturbance rejection. Most real controllers also low-pass filter the derivative with a time constant Td/N (N around 5 to 20); see practical PID.

Variants: P, PI, PD

You do not always need all three terms.

  • P: simple tasks where a small steady-state error is acceptable, or when the plant has its own integrator.
  • PI: the workhorse for flow, level, temperature and speed. No derivative means noise stays quiet.
  • PD: fast systems with an integrator in the plant (positioning, attitude), where overshoot is the main problem and steady-state error is zero anyway.
  • PID: when you need both zero error and good damping, and the measurement is clean enough.

Check your understanding

Quick check · 4 questions

of 4 answered You scored / 4 · perfect!

  1. 1. Which term removes steady-state error for a step setpoint?

    The integral term keeps growing until the error is exactly zero, so the loop settles at the setpoint.
  2. 2. What is the main side effect of a large derivative gain?

    Differentiating amplifies high-frequency content, so noise on the sensor shows up strongly in the control signal.
  3. 3. In standard form, C(s) = Kp(1 + 1/(Ti s) + Td s). What is the equivalent integral gain Ki?

    Kp/(Ti s) = Ki/s, so Ki = Kp/Ti.
  4. 4. Why is derivative applied to the measurement rather than the error in many implementations?

    A setpoint step makes d(error)/dt huge for one sample. Differentiating only the measurement avoids that spike.

Summary

  • PID computes u = Kp·e + Ki·∫e + Kd·de/dt from the error.
  • P gives speed but leaves error; I removes error but adds overshoot and lag; D adds damping but amplifies noise.
  • Too much integral gain destabilizes a loop; derivative can restore damping.
  • In code, put the derivative on the measurement and think about sample time dt.
  • Choosing the three numbers well is the subject of tuning.

Next: PID Tuning and Ziegler-Nichols