IEC 61131-3 · Programming

Structured Text (ST) 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.

1
2
3

📺 Video Lesson

Watch the video first, then work through the interactive exercises below. Each section builds on the previous one.

⚡ Why Structured Text?

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.

LADDER LOGIC

I1 Q1 Sensor_OK Motor_Run

STRUCTURED TEXT

IF Sensor_OK AND NOT E_Stop THEN
  Motor_Run := TRUE;
END_IF;

Both do the same thing — ST is more readable for complex logic.

Variables & Data Types

In ST, every variable must be declared with a type. The most common types are BOOL, INT, DINT, REAL, STRING, and TIME.

Structured Text
VAR
  motor_speed : REAL := 0.0;
  is_running  : BOOL := FALSE;
  cycle_count : DINT := 0;
END_VAR

Common Data Types

TypeSizeRange / ExampleUse case
BOOL1 bitTRUE / FALSEDigital signals, flags
INT16 bit-32768 to 32767Counters, small values
DINT32 bit±2.1 billionLarge counters, positions
REAL32 bit3.14, -0.001Speeds, temperatures, ratios
STRINGvariable'Conveyor A'Labels, messages
TIME32 bitT#500ms, T#2sTimers, delays
Exercise 1

Declare a variable "temperature" of type REAL with an initial value of 20.0

Use the VAR...END_VAR block with the := operator for initialization.

IF / ELSIF / ELSE

Conditional logic in ST follows a clean, readable syntax. Always end your IF block with END_IF.

Structured Text
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

Exercise 2

Write an IF statement that sets "alarm" to TRUE if "temperature" exceeds 80.0

Use the > operator and don't forget END_IF;

FOR Loops

FOR loops are perfect for array iteration and repetitive calculations. They run for a fixed number of iterations.

Structured Text
FOR i := 0 TO 9 DO
  total := total + values[i];
END_FOR;
average := total / 10;
Exercise 3

Write a FOR loop that resets all 5 elements of array "flags" (ARRAY[0..4] OF BOOL) to FALSE

Iterate from 0 TO 4 and assign FALSE to each element.

🎯 Quick Quiz

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?