Skip to content
Tutorials Center
My learning

Topics

Lesson 23 of 36 ยท Control Design

PID Control in Simulink: Blocks, Tuning and Anti-Windup

Build a feedback loop with the Simulink PID Controller block, learn P, I, D and filter terms, tune with the PID Tuner, add anti-windup and discretize the controller.

Most industrial control loops are PID loops: the controller looks at the error between a setpoint and a measurement and computes an actuator command from the error, its integral and its derivative. Simulink has a ready-made PID Controller block and an interactive PID Tuner. This lesson explains the parameters, the loop you build, how tuning works and the practical problems (noise, saturation, discretization).

Simulink and its Control System Toolbox / Simulink Control Design products cannot run on this site. Menus and parameters are described exactly; Python stand-ins (labelled) simulate a plant and a PID loop so you can experiment with the same ideas.

The loop

flowchart LR
    R["Setpoint r"] --> S(("Sum +/-"))
    S -->|"error e"| C["PID Controller"]
    C -->|"u"| P["Plant"]
    P -->|"y"| S
    P --> Y["Scope"]

The classic unity-feedback loop: the plant output y is subtracted from the setpoint r.

Build it in Simulink with these blocks from the Library Browser:

  1. Step (Sources) for the setpoint.
  2. Sum (Math Operations) with signs |+-.
  3. PID Controller (Continuous-Time Blocks > Continuous or Discrete). Double-click to set the parameters.
  4. A plant, for example Transfer Fcn (Continuous) with numerator [1] and denominator [1 2 1].
  5. Scope (Sinks) to watch y. Use a Mux or a Scope with two inputs to show r as well.

Press Run (stop time 10 for this plant).

The PID block parameters

In the block dialog you choose:

  • Controller: P, I, D, PI, PD or PID.
  • Time domain: Continuous-time or Discrete-time (with a Sample time).
  • Form (Compensator formula): Parallel, P + I/s + D*N/(1 + N/s), or Ideal, P*(1 + I/s + D*N/(1 + N/s)). Parallel is the default.
  • Proportional (P), Integral (I), Derivative (D) gains and the Filter coefficient (N).
  • Integrator method and Filter method (Forward Euler, Backward Euler, Trapezoidal in discrete time).
  • Initial conditions for the integrator and filter, and external reset.
  • Saturation limits and an Anti-windup method.

What each term does:

Term Effect Too much causes
P Reacts to the present error: bigger P is faster Overshoot, oscillation, instability
I Removes steady-state error by accumulating error Overshoot, slow oscillation, windup
D Reacts to the trend of the error: damps overshoot Noise amplification
N Sets the bandwidth of the derivative filter N too large: noisy; too small: weak damping

In the parallel form the derivative is really D*N*s/(s + N), a filtered derivative, because a pure derivative would amplify noise without limit and cannot be implemented.

Tuning with the PID Tuner

Open the block and click Tune... (needs Simulink Control Design to linearize the plant in the loop). The tuner:

  1. Linearizes the plant around the model's initial operating point (covered in linearization and frequency response).
  2. Designs a controller for a default response and shows step plots of the reference tracking and disturbance rejection.
  3. Gives you two sliders: Response time (faster/slower) and Transient behavior (aggressive/robust). Moving them changes gains instantly.
  4. Shows performance and robustness numbers (rise time, settling time, overshoot, gain and phase margins).
  5. Update Block writes the gains back into the block.

If you do not have the plant in a linear form, you can also do manual tuning by watching the Scope, with the classical Ziegler-Nichols or relay approach, or with the Closed-Loop PID Autotuner block, which tunes while the loop runs on real hardware (needs its own toolbox).

Manual tuning rules of thumb

  1. Set I = 0 and D = 0; increase P until the response is fast but only slightly oscillatory.
  2. Add I until the steady-state error disappears in an acceptable time (the integral gain is roughly P divided by a time constant).
  3. Add a little D to reduce overshoot, and keep N moderate (10 to 100 relative to the loop speed).
  4. Check disturbance rejection and noise, not only the step response.

A runnable stand-in: PID on a first-order plus lag

The stand-in uses forward Euler on the second-order plant 1/(s^2 + 2s + 1), with the filtered derivative kept as a state, the way the PID block does. It is our own mini simulator, so values differ from a real Simulink run in solver details, but the trends are the same.

Python Stand-in: full PID with filtered derivative

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 filter is written as a state df, which mirrors how the PID block implements the filtered derivative with a filter integrator. Try different values of N and of the gains and compare overshoot and settling time.

Windup and anti-windup

If the actuator saturates (a valve fully open, a motor at its voltage limit), the error stays large and the integrator keeps growing even though the output cannot respond. When the error finally changes sign the integrator is huge and takes a long time to unwind: the response shows a big overshoot. This is integrator windup.

In the PID block, open the Saturation tab:

  1. Tick Limit output and set Upper limit and Lower limit.
  2. Choose Anti-windup method: back-calculation (feeds the difference between the saturated and unsaturated output back into the integrator with gain Kb, the default of I/P works as a start) or clamping (stops integrating while saturated and the error pushes further into the limit).
  3. Add the plant's input limits in the model as well, or the block cannot see them.

Discrete PID and sample time

On a microcontroller the controller runs at a fixed sample time Ts. Choose Time domain: Discrete-time and set the Sample time. The integrator method matters: Forward Euler is simple but can be unstable for fast loops, Backward Euler and Trapezoidal are better for stability. A good starting point is a sample time 10 to 20 times faster than the closed-loop bandwidth. State feedback, observers and LQR continues with state-space methods.

Variants: 2-DOF and gain scheduling

  • PID Controller (2DOF): separate setpoint weights b and c let you tune disturbance rejection and setpoint tracking independently, which reduces overshoot on setpoint steps.
  • Gain scheduling: if the plant changes with an operating condition (speed, load), use a Lookup Table to select gains by an input and connect them to the PID block's external gain ports (set the Source of P, I, D to external).

Common mistakes

  • Tuning on a plant that is not representative (wrong parameters or missing actuator limits).
  • Ignoring noise: a large D with a large N makes the actuator chatter.
  • Forgetting anti-windup when the actuator can saturate.
  • A sample time too slow for the loop speed, which silently adds delay.
  • Reading the Scope only for the step: also test a disturbance and noise.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why does the PID block filter the derivative term?

    A filtered derivative DNs/(s+N) limits high-frequency gain.
  2. 2. What is integrator windup?

    The integrator accumulates error the actuator cannot act on; anti-windup limits it.
  3. 3. Which two anti-windup methods does the PID block offer?

    Back-calculation feeds the saturation difference back; clamping stops integration at the limit.
  4. 4. What does a larger proportional gain usually do?

    Higher P speeds the loop but reduces stability margins.

Summary

  • Build the loop with Step, Sum, PID Controller, plant and Scope; choose Parallel or Ideal form, and a continuous or discrete time domain.
  • P gives speed, I removes steady-state error, D damps; the derivative must be filtered (N).
  • Use Tune... (PID Tuner) for interactive tuning, or tune manually with step and disturbance tests.
  • Add saturation limits and an anti-windup method when the actuator can saturate.
  • Discretize with a suitable sample time and integrator method; use 2-DOF or gain scheduling for harder plants.

Next: Linearization and frequency response