Eight Structured Text patterns you'll write on almost every machine — motor control, timers, edge counting, analog scaling, a state machine, alarms, a reusable function block and an array loop — in plain IEC 61131-3 syntax, with the mistakes to avoid.
Structured Text in one paragraph
Structured Text (ST) is the textual, Pascal-like language defined in IEC 61131-3, alongside Ladder Diagram, Function Block Diagram and Sequential Function Chart. The fourth edition of the standard was published in May 2025; it removed the old Instruction List (IL) language, which had been deprecated since 2013, and added UTF-8 strings. ST is the language most vendors' tools can exchange, and it's what Siemens calls SCL. The examples below use standard syntax; vendor dialects differ in small ways (see the end of the article).
1. Motor start/stop with seal-in and interlocks
The ladder "start-stop-seal" circuit, in one line of ST. Stop and the overload are wired normally-closed, so a broken wire stops the motor.
VAR
StartPB : BOOL; (* NO push-button *)
StopPB : BOOL; (* NC push-button: TRUE = healthy *)
Overload : BOOL; (* NC overload contact: TRUE = healthy *)
GuardOK : BOOL;
MotorRun : BOOL;
END_VAR
MotorRun := (StartPB OR MotorRun) AND StopPB AND Overload AND GuardOK;
2. On-delay timer (TON)
Start a conveyor 3 seconds after the infeed sensor sees a part, and filter out a bouncing sensor at the same time.
VAR
PartSensor : BOOL;
tDelay : TON;
Conveyor : BOOL;
END_VAR
tDelay(IN := PartSensor, PT := T#3S); (* call EVERY scan *)
Conveyor := tDelay.Q;
(* tDelay.ET holds the elapsed time if you want to show it on the HMI *)
3. Counting parts with an edge trigger
Count each part once, not once per scan, and stop the line when the batch is complete.
VAR
PartSensor : BOOL;
ResetCount : BOOL;
BatchSize : INT := 50;
rPart : R_TRIG;
cParts : CTU;
BatchDone : BOOL;
PartCount : INT;
END_VAR
rPart(CLK := PartSensor);
cParts(CU := rPart.Q, RESET := ResetCount, PV := BatchSize);
BatchDone := cParts.Q;
PartCount := cParts.CV;
4. Scaling an analog input to engineering units
Convert a raw 0–27648 count (the range Siemens analog cards use for 4–20 mA) to 0–10 bar, clamp it, and flag an out-of-range signal such as a broken wire.
VAR
RawIn : INT;
Pressure : REAL; (* bar *)
SignalFault : BOOL;
END_VAR
VAR CONSTANT
RAW_MIN : REAL := 0.0;
RAW_MAX : REAL := 27648.0;
EU_MIN : REAL := 0.0;
EU_MAX : REAL := 10.0;
END_VAR
Pressure := EU_MIN + (INT_TO_REAL(RawIn) - RAW_MIN)
* (EU_MAX - EU_MIN) / (RAW_MAX - RAW_MIN);
Pressure := LIMIT(EU_MIN, Pressure, EU_MAX);
(* Out of range, e.g. wire break: check your card's manual for the exact values *)
SignalFault := (RawIn < -2000) OR (RawIn > 32511);
5. A state machine with CASE
Fill, heat and drain a tank. Each state does one thing and decides the next state — far easier to debug than a pile of latched bits.
TYPE E_TankState : (IDLE, FILLING, HEATING, DRAINING, FAULT); END_TYPE
VAR
State : E_TankState := IDLE;
StartCmd : BOOL;
ResetPB : BOOL;
LevelHigh : BOOL;
LevelLow : BOOL;
Temperature : REAL;
SetPoint : REAL := 65.0;
tStep : TON;
InletValve, Heater, DrainValve : BOOL;
END_VAR
(* Outputs off by default; each state switches on what it needs *)
InletValve := FALSE; Heater := FALSE; DrainValve := FALSE;
CASE State OF
IDLE:
IF StartCmd AND LevelLow THEN State := FILLING; END_IF;
FILLING:
InletValve := TRUE;
IF LevelHigh THEN State := HEATING; END_IF;
HEATING:
Heater := TRUE;
IF Temperature >= SetPoint THEN State := DRAINING; END_IF;
DRAINING:
DrainValve := TRUE;
IF LevelLow THEN State := IDLE; END_IF;
FAULT:
IF ResetPB THEN State := IDLE; END_IF;
ELSE
State := FAULT;
END_CASE;
(* Step watchdog: no state may last longer than 10 minutes *)
tStep(IN := (State <> IDLE) AND (State <> FAULT), PT := T#10M);
IF tStep.Q THEN State := FAULT; END_IF;
tStep(IN := FALSE) once when it changes.6. A delayed, latched alarm with acknowledge
VAR
TempHigh : BOOL; (* raw condition *)
AckPB : BOOL;
tAlarm : TON;
AlarmActive : BOOL; (* condition present after delay *)
AlarmLatched: BOOL; (* stays on until acknowledged and cleared *)
END_VAR
tAlarm(IN := TempHigh, PT := T#2S);
AlarmActive := tAlarm.Q;
IF AlarmActive THEN
AlarmLatched := TRUE;
ELSIF AckPB THEN
AlarmLatched := FALSE; (* only clears once the condition has gone *)
END_IF;
7. A reusable function block: valve with feedback monitoring
Write it once, use it for every valve. Each instance keeps its own timers and fault state.
FUNCTION_BLOCK FB_Valve
VAR_INPUT
OpenCmd : BOOL;
FbOpen : BOOL;
FbClosed : BOOL;
Reset : BOOL;
Timeout : TIME := T#5S;
END_VAR
VAR_OUTPUT
OutOpen : BOOL;
Fault : BOOL;
END_VAR
VAR
tOpen, tClose : TON;
END_VAR
OutOpen := OpenCmd AND NOT Fault;
tOpen(IN := OutOpen AND NOT FbOpen, PT := Timeout);
tClose(IN := NOT OutOpen AND NOT FbClosed, PT := Timeout);
IF tOpen.Q OR tClose.Q THEN
Fault := TRUE;
ELSIF Reset THEN
Fault := FALSE;
END_IF;
END_FUNCTION_BLOCK
(* Using it (I/O variables declared elsewhere) *)
VAR
V101, V102 : FB_Valve;
END_VAR
V101(OpenCmd := FillCmd, FbOpen := V101_ZSO, FbClosed := V101_ZSC, Reset := ResetPB);
V102(OpenCmd := DrainCmd, FbOpen := V102_ZSO, FbClosed := V102_ZSC, Reset := ResetPB,
Timeout := T#8S);
V101_SOL := V101.OutOpen;
V102_SOL := V102.OutOpen;
8. Arrays and a FOR loop: rolling average
Smooth a noisy reading by averaging the last 10 samples.
VAR
RawValue : REAL;
Samples : ARRAY[0..9] OF REAL;
Index : INT := 0;
i : INT;
Sum : REAL;
Average : REAL;
END_VAR
Samples[Index] := RawValue;
Index := (Index + 1) MOD 10;
Sum := 0.0;
FOR i := 0 TO 9 DO
Sum := Sum + Samples[i];
END_FOR;
Average := Sum / 10.0;
Mistakes that catch everyone
=vs:=.:=assigns,=compares.IF Level = 100 THENis a test;Level := 100;is a write.- Timers called inside an
IF. If the timer isn't called on the scan whereINgoes FALSE, it never resets. Call timers unconditionally. - Integer division.
7 / 2is3on integers. Convert toREALfirst. - Comparing REALs for equality.
IF Temp = 65.0may never be true. Use>=or a tolerance band. - Long loops. A
WHILEor a bigFORruns to completion inside one scan. Keep them bounded and watch your scan time. CASEwithoutELSE. An unexpected state value silently does nothing. Send it to a fault state.- Writing the same output in two places. The last write wins. One output, one place.
Dialect differences to know
- Siemens SCL (TIA Portal): local variables written with
#, global tags and blocks in double quotes, timers as multi-instances. See exporting logic to TIA Portal. - Rockwell Studio 5000 ST: tags are controller- or program-scoped; in ST, timers use the
TONRinstruction with anFBD_TIMERtag, and the preset.PREis in milliseconds. - Mitsubishi GX Works3 ST: close to standard, plus direct device access such as
D100andM0. - Enumerations,
VAR CONSTANTandTYPEblocks are supported in different places and with different syntax from tool to tool — check before you paste.
Try these in Logivyx
Logivyx Studio includes an IEC 61131-3-style Structured Text editor with a function palette, user types (UDTs) and function blocks. Its library covers TON, TOF, TP, CTU, CTD, R_TRIG, F_TRIG, PID, SCALE, LIMIT, MIN/MAX, maths, strings and conversions. Paste an example, bind its variables to screen widgets, and run it on the built-in simulator with a live watch window — before any hardware is connected.
The logic runs on Logivyx's embedded soft PLC, and can be exported to TIA Portal (SCL) or Studio 5000 (L5X) when a project calls for a hardware PLC.
Try it on your own project.
Design screens, write Ladder or Structured Text, and run both on the built-in simulator — or connect to your own Siemens, Allen-Bradley or Mitsubishi PLC. No separate PLC needed.