Structured Text: Text-Based PLC Programming — Unlimited Power
Why Structured Text Instead of Ladder?
Structured Text (ST) is a high-level programming language defined in the IEC 61131-3 standard. It resembles Pascal and provides the expressive power needed for complex algorithms, mathematical calculations, and data processing that would be unwieldy in Ladder Logic.
Situations where Structured Text excels over Ladder:
- Mathematical calculations: scaling analog values, PID formulas, trigonometric functions
- String processing: parsing barcodes, building communication messages
- Complex decision trees: recipe management with dozens of parameters
- Loop operations: processing arrays of sensor data, batch calculations
- State machines: sequential control with many stages and transitions
Ladder Logic remains superior for simple discrete logic (motor start-stop, interlocks) where visual tracing during troubleshooting is valuable. Many industrial projects combine both languages.
Variables and Types in ST
Every Structured Text program begins with variable declarations. Variables are named storage locations that hold data during program execution.
VAR
bMotorRunning : BOOL := FALSE; // motor status flag
nPartCount : INT := 0; // produced parts counter
rTemperature : REAL := 0.0; // sensor reading in Celsius
sProductName : STRING := 'WidgetA'; // current product name
tDelayTime : TIME := T#5s; // configurable delay
END_VAR
Variables can be declared with different scopes: VAR (local), VAR_INPUT (passed in), VAR_OUTPUT (passed out), VAR_IN_OUT (passed in and modified), and VAR_GLOBAL (accessible from any program).
Industrial PLC programming commonly uses Hungarian notation prefixes: b for BOOL, n for INT, r for REAL, s for STRING, t for TIME, a for Array, and st for Struct.
Conditions: IF and CASE
Conditional statements control program flow based on variable values.
IF rTemperature > 85.0 THEN
bOverheatAlarm := TRUE;
bHeaterEnable := FALSE;
ELSIF rTemperature < 20.0 THEN
bUndertempAlarm := TRUE;
bHeaterEnable := TRUE;
ELSE
bOverheatAlarm := FALSE;
bUndertempAlarm := FALSE;
END_IF;
The CASE statement is ideal for state machines, which are common in sequential industrial processes:
CASE nMachineState OF
0: // IDLE
bConveyorRun := FALSE;
IF bStartButton THEN nMachineState := 1; END_IF;
1: // RUNNING
bConveyorRun := TRUE;
IF bStopButton THEN nMachineState := 2; END_IF;
2: // STOPPING
bConveyorRun := FALSE;
IF tStopDelay.Q THEN nMachineState := 0; END_IF;
END_CASE;
Loops: FOR, WHILE, and REPEAT
Loops execute a block of code multiple times. Use them carefully in PLCs because a long-running loop can extend the scan cycle and cause a watchdog timeout.
// FOR: Calculate average temperature from 10 sensors
rSum := 0.0;
FOR i := 0 TO 9 DO
rSum := rSum + aTemperatures[i];
END_FOR;
rAverageTemp := rSum / 10.0;
// WHILE: Find the first faulty sensor in the array
nFaultIndex := -1;
i := 0;
WHILE i < 10 AND nFaultIndex = -1 DO
IF aSensorStatus[i] = FALSE THEN
nFaultIndex := i;
END_IF;
i := i + 1;
END_WHILE;
// REPEAT: Execute until condition met
i := 0;
REPEAT
i := i + 1;
UNTIL i >= nBatchSize
END_REPEAT;
The FOR loop is most common in industrial programs because the iteration count is known in advance, preventing accidental infinite loops.
Practical Example: The Same Motor Circuit in ST
Here is the motor start-stop circuit from the Ladder Logic lesson, now written in Structured Text:
VAR
bStartButton : BOOL; // I0.0 - momentary start pushbutton
bStopButton : BOOL; // I0.1 - normally closed stop
bEmergencyStop : BOOL; // I0.2 - normally closed e-stop
bOverloadRelay : BOOL; // I0.3 - normally closed overload
bMotorContactor : BOOL; // Q0.0 - motor contactor output
bRunningLamp : BOOL; // Q0.1 - green indicator lamp
END_VAR
// Self-holding motor control logic
IF bStartButton OR bMotorContactor THEN
IF bStopButton AND bEmergencyStop AND bOverloadRelay THEN
bMotorContactor := TRUE;
ELSE
bMotorContactor := FALSE;
END_IF;
ELSE
bMotorContactor := FALSE;
END_IF;
// Running indicator follows motor state
bRunningLamp := bMotorContactor;
The stop button, emergency stop, and overload relay are all normally closed in the field wiring. In the PLC program, they read as TRUE when the circuit is healthy. When any one opens (button pressed or wire broken), it reads FALSE and the motor stops.
Comparison: Ladder Logic vs Structured Text
| Feature | Ladder Logic | Structured Text |
|---|---|---|
| Visual representation | Graphical, relay-like | Text-based, Pascal-like |
| Best for | Simple discrete logic | Complex algorithms |
| Troubleshooting | Easy visual tracing | Requires variable watch |
| Math operations | Clumsy, many boxes | Natural expressions |
| Loops and arrays | Not supported natively | Full support |
| State machines | Possible but verbose | Clean CASE structure |
| Maintenance staff | Very familiar | Requires training |
| Code reuse | Copy rungs | Functions and libraries |
In practice, the best industrial programs use both languages. A common architecture uses Ladder for basic I/O control and safety interlocks, and Structured Text for sequencing, calculations, and recipe management.
Summary
Structured Text is a high-level IEC 61131-3 language that provides variables with types, conditional logic (IF/CASE), and loops (FOR/WHILE/REPEAT) for complex industrial control tasks. It excels at mathematical calculations, state machines, and data processing. While Ladder Logic remains the best choice for simple discrete I/O and troubleshooting-friendly safety circuits, Structured Text is essential for modern automation projects. The most effective approach combines both languages, using each where it provides the greatest clarity and capability.