IEC 61131-3 · Programming
Structured Text is the most powerful IEC 61131-3 language. It reads like Pascal or C and is ideal for complex logic, math, and algorithms.
Watch the video first, then work through the interactive exercises below. Each section builds on the previous one.
ST is one of five languages defined in IEC 61131-3. Unlike Ladder Logic (graphical) or Instruction List (assembly-like), ST is a high-level textual language — making it ideal for complex calculations, state machines, and reusable function blocks.
Both do the same thing — ST is more readable for complex logic.
In ST, every variable must be declared with a type. The most common types are BOOL, INT, DINT, REAL, STRING, and TIME.
VAR
motor_speed : REAL := 0.0;
is_running : BOOL := FALSE;
cycle_count : DINT := 0;
END_VAR
Common Data Types
| Type | Size | Range / Example | Use case |
|---|---|---|---|
| BOOL | 1 bit | TRUE / FALSE | Digital signals, flags |
| INT | 16 bit | -32768 to 32767 | Counters, small values |
| DINT | 32 bit | ±2.1 billion | Large counters, positions |
| REAL | 32 bit | 3.14, -0.001 | Speeds, temperatures, ratios |
| STRING | variable | 'Conveyor A' | Labels, messages |
| TIME | 32 bit | T#500ms, T#2s | Timers, delays |
Declare a variable "temperature" of type REAL with an initial value of 20.0
Conditional logic in ST follows a clean, readable syntax. Always end your IF block with END_IF.
IF sensor_ok AND speed > 0.0 THEN
output := TRUE;
ELSIF emergency_stop THEN
output := FALSE;
ELSE
output := last_state;
END_IF;
↑ Live state machine — click a state to simulate the IF logic
Write an IF statement that sets "alarm" to TRUE if "temperature" exceeds 80.0
FOR loops are perfect for array iteration and repetitive calculations. They run for a fixed number of iterations.
FOR i := 0 TO 9 DO
total := total + values[i];
END_FOR;
average := total / 10;
Write a FOR loop that resets all 5 elements of array "flags" (ARRAY[0..4] OF BOOL) to FALSE
Test your knowledge before moving on.
Q1. What keyword ends an IF block in ST?
Q2. Which data type would you use for a motor speed of 1450.5 RPM?
Q3. What does the := operator do in ST?
Now that you understand ST fundamentals, here's where to go next: