28Industrial patterns
Closed-loop control in ST: hysteresis first, then a practical PID
Start with on/off control and a dead band, then a PID written as arithmetic in ST with anti-windup and bumpless transfer, plus tuning explained in plain words.
First: do you actually need PID?
Plenty of plant loops do not. If the final element is a pump that is either on or off, hysteresis control is the right answer, not the humble one:
IF PV <= SP - 3.0 THEN
OnOff := TRUE;
ELSIF PV >= SP + 3.0 THEN
OnOff := FALSE;
END_IF;The dead band sets the starts per hour. A narrow band means tight control and a burnt motor. Large motors have a permitted number of starts, so the dead band is equipment life, not just precision. Use PID when the final element is modulating: a control valve, a VFD, a heater.
PID as arithmetic, line by line
IEC 61131-3 does not define a PID block, so every vendor ships their own. When you need yours:
Err := SP - PV;
P := Kp * Err;
Integ := Integ + Ki * Err * Ts;
D := Kd * (Err - PrevErr) / Ts;
CV := LIMIT(0.0, P + Integ + D, 100.0);
PrevErr := Err;Ts is the scan time in seconds. On most process loops Kd stays at zero: derivative amplifies noise, and in practice it earns its place only on slow temperature loops.
Anti-windup
This is where hand-written controllers usually fail. If the tank is empty and the pump is at 100%, the error is large and persistent, so the integral climbs to an absurd number. When the value finally reaches setpoint the integral is still holding that number, so the process sails past the target and stays there for a minute.
The fix: do not integrate while the output is saturated and the error pushes it further into saturation.
CVraw := Kp * Err + Integ;
CV := LIMIT(0.0, CVraw, 100.0);
Winding := (CVraw > 100.0 AND Err > 0.0) OR (CVraw < 0.0 AND Err < 0.0);
IF NOT Winding THEN
Integ := Integ + Ki * Err * Ts;
END_IF;Then clamp the integral anyway: Integ := LIMIT(-100.0, Integ, 100.0);. Belt and braces.
Bumpless transfer
The controller is in manual, the operator has it at 40%, and they switch to auto. If your integral is zero, the output jumps. The trick is simple: make the integral track the real output while in manual.
IF NOT Auto THEN
Integ := CVmanual - Kp * Err;
END_IF;On the first auto scan the computed output equals exactly what manual was holding, and the line feels nothing. The same principle applies after a fault reset, a range change, or a power-up.
Tuning in plain words
You want | Change | Side effect |
|---|---|---|
Faster response | raise Kp | oscillation if you overdo it |
Remove a standing offset | raise Ki | overshoot, slower settling |
Shave the overshoot peak | raise Kd | sensitivity to noise |
A calmer loop overall | lower Kp and Ki | slower to catch up |
A method that always works: set Ki = 0 and Kd = 0, raise Kp until the process oscillates gently and continuously, then halve it. Now raise Ki until the offset closes in a time you accept. And the golden rule: a calm loop that arrives in twenty seconds beats a nervous one that arrives in five and hunts.
Time proportioning
A PID output is 0-100%, and the pump here is on or off. The bridge is time proportioning: a ramp climbing 0 to 100 every two seconds, with the pump on while the demand is above the ramp. A 30% demand gives 0.6 s of run out of every 2 s. It is exactly what an industrial heater thermostat does.
Try it in the lab
The lab runs in your browser: an ST editor, a virtual process that suits the lesson, the same program as a live ladder diagram, a watch table, and export to Siemens SCL.
What to try
- Run the program, press START and watch SP, Level, CV and Integ in the watch table.
- Notice the pump pulsing instead of running steadily: that is CV time-proportioned.
- The lamp lights once the level settles near 60%.
- Change Ki to 0.0 and download again: it settles below the set point (steady-state error). Change Kp to 30.0: it swings more.
Terms in this lesson
People find this lesson by searching
pid in structured textpid controller plc code exampleanti windup plc pidbumpless transfer pidpid tuning practical guidehysteresis on off control plctime proportioning output plcproportional integral controller st
Course outline
- 01What is Structured Text, and when to use it instead of ladder
- 02How a PLC runs your code: the scan cycle and the process image
- 03Program structure: POUs, PROGRAM, FUNCTION and FUNCTION_BLOCK
- 04Your first program: starting a pump with seal-in logic
- 05Data types: BOOL, INT, REAL, TIME and the rest
- 06Declaring variables: VAR, VAR_INPUT and RETAIN
- 07Literals and constants: 16#FF, 2#1010 and T#5s
- 08Type conversion and scaling a 4-20 mA signal
- 09Assignment and arithmetic in ST: := and operator precedence
- 10Boolean logic, comparisons and bit operations
- 11Standard functions: ABS, SQRT, MIN, MAX, LIMIT, SEL, MUX
- 12IF, ELSIF and ELSE: the rules and when to avoid them
- 13The CASE statement and your first state machine
- 14The FOR loop and the watchdog
- 15WHILE, REPEAT, EXIT, CONTINUE and RETURN
- 16Edge detection: R_TRIG, F_TRIG and the "it fired every scan" bug
- 17Timers: TON, TOF and TP
- 18Counters: CTU, CTD and CTUD
- 19SR and RS flip-flops: which one wins
- 20Writing your own function
- 21Writing a function block
- 22Arrays in Structured Text
- 23Structs and enumerations
- 24References and pointers
- 25The state machine pattern: a batch sequence that cannot get stuck
- 26Alarm management to ISA-18.2: delay, deadband, latch, acknowledge, first-out
- 27Analogue signal processing: counts to engineering units, NE43 and filtering
- 28Closed-loop control in ST: hysteresis first, then a practical PID
- 29Motors and valves in practice: permissives, feedback and travel time
- 30The same ST on every platform: SCL, Studio 5000, CODESYS, TwinCAT
- 31Testing and debugging ST: the watch table, forcing and simulation
- 32An ST style guide: naming, comments, constants and version control
- 33Final project: a complete tank station with a state machine and alarms
- 3420 interview questions on ST and PLC programming, with answers