Field fix / guideManual / ReferenceVendor-agnosticIEC 61131-3industrial-automation

Structured Text (ST) Programming — Engineering Guide

Shared by TSIMEREZIVO Team · 4 Oct 2026 · 50 views

# Structured Text (ST) Programming Structured Text is the most powerful and expressive IEC 61131-3 language — Pascal-like syntax with rich data types, loops, and function blocks. Ideal for complex mathematical algorithms, recipe handling, and array processing. This guide takes you from syntax basics to a working PID function block. 30 min ## ST Syntax Basics ```iecst (* Variable declarations — in VAR block *) VAR blink_state : BOOL := FALSE; (* Output state *) blink_timer : TON; (* On-delay timer instance *) blink_period_ms : TIME := T#500ms; (* 500ms half-period *) END_VAR (* Program body — executes every scan *) blink_timer(IN := NOT blink_state, PT := blink_period_ms); IF blink_timer.Q THEN blink_state := NOT blink_state; (* Toggle *) blink_timer(IN := FALSE); (* Reset timer *) END_IF; DIGITAL_OUT_01 := blink_state; (* Write to tag *) ``` ## Variable Declarations ```iecst (* --- VAR block types in IEC 61131-3 --- *) VAR (* Local — per scan cycle, NOT retained *) setpoint : REAL; error : REAL; output : REAL; END_VAR VAR_RETAIN (* Retained — survives power cycle *) total_runtime : DINT; (* Accumulated seconds *) batch_count : INT; END_VAR VAR_INPUT (* Read from external scope / caller *) pv_scaled : REAL; (* Process variable from AI module *) sp_from_hmi : REAL; (* Setpoint from operator HMI *) END_VAR VAR_OUTPUT (* Written to external scope / caller *) cv_to_valve : REAL; (* Control variable to AO module *) in_alarm : BOOL; (* Alarm flag to HMI *) END_VAR VAR_IN_OUT (* Read/write pass-through — avoid unless necessary *) recipe_step : INT; END_VAR VAR CONSTANT (* Compile-time constants — never written at runtime *) MAX_FLOW : REAL := 1000.0; (* L/min *) MIN_FLOW : REAL := 0.0; SCAN_MS : REAL := 0.1; (* 100ms scan = 0.1s *) END_VAR ``` ## Control Flow ```iecst IF temperature > 350.0 THEN alarm_hihi := TRUE; output := 0.0; (* Shut down *) ELSIF temperature > 300.0 THEN alarm_hi := TRUE; ELSE alarm_hi := FALSE; alarm_hihi := FALSE; END_IF; ``` ```iecst CASE operating_mode OF 0: (* STOPPED *) motor_out := FALSE; valve_cmd := FALSE; 1: (* STARTING *) motor_out := TRUE; start_tmr(IN:=TRUE, PT:=T#3s); IF start_tmr.Q THEN operating_mode := 2; END_IF; 2: (* RUNNING *) motor_out := TRUE; 3: (* FAULT *) motor_out := FALSE; fault_latch := TRUE; ELSE operating_mode := 0; END_CASE; ``` ```iecst (* Sum all 16 temperature sensors *) VAR i : INT; temp_sum : REAL := 0.0; temp_avg : REAL; temps : ARRAY[1..16] OF REAL; END_VAR temp_sum := 0.0; FOR i := 1 TO 16 DO temp_sum := temp_sum + temps[i]; END_FOR; temp_avg := temp_sum / 16.0; ``` ```iecst {`(* Wait for temperature to stabilise *) WHILE ABS(pv - setpoint) > 2.0 DO (* Still settling — don't advance step *) (* Note: avoid infinite WHILE in PLC! *) (* Use state machine + scan-based checks *) EXIT; (* Always EXIT if no real wait *) END_WHILE; (* Safe pattern: use a BOOL flag instead *) IF ABS(pv - setpoint) ## Complete PID Function Block This is a production-quality ISA-S5.1 standard Proportional-Integral-Derivative function block with anti-windup, bumpless auto/manual transfer, and output clamping. Use it as-is or adapt it for your process. ```iecst {`(* * TSIMEREZIVO_PID — ISA-compatible PID Function Block * IEC 61131-3 Structured Text * Anti-windup, bumpless A/M, output clamp *) FUNCTION_BLOCK TSIMEREZIVO_PID VAR_INPUT PV : REAL; (* Process Variable — scaled EU *) SP : REAL; (* Setpoint — scaled EU *) AUTO_MODE : BOOL := TRUE; (* TRUE = Automatic, FALSE = Manual *) MAN_OUT : REAL := 0.0; (* Manual output (0–100%) *) Kp : REAL := 1.0; (* Proportional gain *) Ki : REAL := 0.1; (* Integral gain (1/Ti) *) Kd : REAL := 0.05; (* Derivative gain *) OUT_LO : REAL := 0.0; (* Output clamp low (%) *) OUT_HI : REAL := 100.0; (* Output clamp high (%) *) SCAN_TIME : REAL := 0.1; (* Scan period in seconds *) END_VAR VAR_OUTPUT OUT : REAL; (* Control variable output 0–100% *) ERROR : REAL; (* Current error = SP - PV *) IN_AUTO : BOOL; (* Status: in automatic mode *) SATURATED : BOOL; (* Output at limit — anti-windup active *) END_VAR VAR integral : REAL := 0.0; (* Integral accumulator *) prev_error : REAL := 0.0; (* Previous error for derivative *) p_term : REAL; i_term : REAL; d_term : REAL; out_raw : REAL; END_VAR (* --- PID Body --- *) IN_AUTO := AUTO_MODE; ERROR := SP - PV; IF AUTO_MODE THEN (* P term *) p_term := Kp * ERROR; (* I term with anti-windup: freeze integration when output saturated *) IF NOT SATURATED THEN integral := integral + (Ki * ERROR * SCAN_TIME); END_IF; i_term := integral; (* D term — derivative on measurement, not error, to prevent setpoint kick *) d_term := Kd * (prev_error - ERROR) / SCAN_TIME; out_raw := p_term + i_term + d_term; (* Output clamp *) IF out_raw >= OUT_HI THEN OUT := OUT_HI; SATURATED := TRUE; ELSIF out_raw **Tuning PID in LogicForge** — Use the TSIMEREZIVO built-in step-response tuning wizard: Tags → PID Tuning. It applies a step change to the output, records the response curve, and calculates initial Kp, Ki, Kd using Ziegler-Nichols or Cohen-Coon methods. Always verify tuning in Simulator before applying to live plant. ### Next Steps | href | label | desc | |---|---|---| | /dashboard/docs/pid-control | PID Loop Config | Set SP, alarm limits, trend displays | | /dashboard/docs/simulator-intro | Simulate & Test | Validate your ST logic safely | | /dashboard/docs/scada-builder-intro | Build HMI Screens | Connect your PID to a SCADA faceplate |

More like this

Rate, comment, and get full access

Members can download files, rate contributions, and discuss fixes with the engineers who posted them — free.

Join the library