Skip to content
Tutorials Center
My learning

Topics

Lesson 18 of 37 ยท Function Blocks and Structured Text

PID Control Basics

Learn PID control for PLCs: setpoint, process value and output, the P, I and D terms, anti-windup, tuning basics and a runnable C simulation of a heated tank.

Switching a heater fully on below a temperature and off above it (on/off control) works, but the temperature swings up and down around the target. When you need a steady value, such as a tank held at 60 degrees, a pressure at 4 bar or a motor at 1 500 rpm, you use PID control, the workhorse of process automation.

A PID controller continuously compares what you want (the setpoint, SP) with what you have (the process value, PV) and calculates how much to drive the actuator (the output, OUT). This lesson explains the three terms, anti-windup, and basic tuning, with a runnable simulation. The real PID function blocks of each vendor cannot run on this site, so the simulation is a labelled C model.

The control loop

flowchart LR
    SP["Setpoint SP"] --> E["Error<br/>e = SP - PV"]
    E --> PID["PID block"]
    PID --> OUT["Output OUT<br/>0..100 %"]
    OUT --> P["Process<br/>heater + tank"]
    P --> PV["Process value PV"]
    PV --> E

Figure: a feedback loop. The controller reacts to the error.

The error is e = SP - PV. The controller computes the output from the error over time:

OUT = Kp * e + Ki * (sum of e over time) + Kd * (rate of change of e)

The three terms

P: Proportional Reacts to the size of the error now
I: Integral Adds up past error to remove offset
D: Derivative Reacts to how fast error changes

Use only the terms you need

1 / 2

P: proportional

The output is proportional to the error. A big gap gives a big correction. Alone, P control leaves a steady-state offset: when the PV is a little below the SP the correction is a little too small to close the gap completely, so the loop settles short of the setpoint.

I: integral

The integral term accumulates the error over time. While any error remains, the output keeps growing, so I removes the offset. Too much I makes the loop overshoot and oscillate. Because it accumulates, it can wind up during a long error (for example when the heater is already at 100 %), causing a large overshoot afterward. The remedy is anti-windup: stop integrating while the output is saturated.

D: derivative

The derivative term reacts to how quickly the error is changing and brakes the response, which reduces overshoot. It also amplifies noise, so many loops use PI only, and D is added only when needed (slow, heavy processes such as temperature).

A runnable simulation

We model a tank whose temperature rises with heater power and loses heat to the room. The controller runs every second, and heater power is limited to 0 to 100 percent.

C Stand-in: P, PI and PID on a simulated tank (C)

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 scanf or fgets), it gets the next line from this box.

Compare the lines: P stops short of 60 (the offset), while PI and PID settle at the setpoint. Change kp, ki and kd and run again to see their effect on the peak.

Anti-windup in action

C Stand-in: why windup matters (C)

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 scanf or fgets), it gets the next line from this box.

Using a PID block

In a real PLC a PID is a function block. Typical interface (names vary by vendor):

TEXT
Heater_PID(
    PV     := Tank_Temp,       (* process value, REAL *)
    SP     := Temp_Setpoint,   (* setpoint, REAL *)
    Kp     := 5.0,
    Ti     := T#40s,           (* integral time *)
    Td     := T#10s,           (* derivative time *)
    OutMin := 0.0,
    OutMax := 100.0,
    Auto   := TRUE );
Heater_Out := Heater_PID.OUT;

Notice the Ti and Td forms: many vendors use integral time (Ki = Kp / Ti) instead of a gain. Also look for Auto/Manual mode, bumpless transfer, output limits and a cycle time that matches the task period. A PID block should run in a cyclic task of fixed period, not in the free-running main scan, or its time-based terms will drift (see the scan cycle).

Tuning basics

  1. Set I and D to zero. Raise Kp until the loop responds quickly but starts to oscillate, then back off by half.
  2. Add I slowly until the offset disappears; stop before it overshoots too much.
  3. Add D only if the response is sluggish or overshoots on a slow process.
  4. Test with setpoint steps and disturbances, with the real process safe to disturb.

Some alternatives: Ziegler-Nichols rules, relay auto-tune built into many PLCs, and, for very simple jobs, on/off control with hysteresis (ST control flow).

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What is the error in a PID loop?

    Error is the setpoint minus the process value.
  2. 2. Which term removes the steady-state offset left by P control?

    The integral accumulates the error until it is zero.
  3. 3. What does anti-windup do?

    Without it the integral can become huge while the actuator is at its limit, causing big overshoot.
  4. 4. Why does a PID block belong in a fixed-period cyclic task?

    Time-based terms assume a steady sample interval.

Summary

  • PID computes an output from the error: P (now), I (past), D (trend).
  • P alone leaves an offset; I removes it but can wind up; D adds damping and amplifies noise.
  • Use output limits, anti-windup, Auto/Manual and a fixed cycle time.
  • Tune one term at a time and test with real disturbances.

Next: PLC Timers - TON, TOF and TP