Structured Text for Beginners: A Complete Guide to PLC Programming

Start your PLC programming journey with this beginner-friendly guide to Structured Text — the most versatile IEC 61131-3 language.

ST and the IEC 61131-3 standard

Structured Text (ST) is a high-level programming language for Programmable Logic Controllers (PLCs), defined in the IEC 61131-3 standard. Ladder Diagram and Function Block Diagram are graphical; ST is written as text, with a syntax close to Pascal or C. That makes it well suited to complex logic, heavy maths and data processing.

Why learn ST?

ST runs on Siemens S7, Allen-Bradley, CODESYS, Beckhoff TwinCAT, Schneider Electric and more, so what you learn on one platform carries over to the next. It handles loops, arrays, structs and complex maths that are tedious to draw in a graphical language. Text is also easier to document, keep under version control and maintain than a graphical program. And there is demand for it: automation engineers with ST skills tend to earn more.

Your first Structured Text program

Here is a simple motor start/stop program:

PROGRAM MotorControl
VAR
    StartButton : BOOL := FALSE;
    StopButton  : BOOL := FALSE;
    MotorRunning : BOOL := FALSE;
END_VAR

IF StartButton AND NOT StopButton THEN
    MotorRunning := TRUE;
ELSIF StopButton THEN
    MotorRunning := FALSE;
END_IF;
END_PROGRAM

What each part does

  • PROGRAM / END_PROGRAM defines a Program Organization Unit (POU).
  • VAR / END_VAR declares the variables, with their types and optional initial values.
  • BOOL is the Boolean data type, TRUE or FALSE.
  • IF / ELSIF / END_IF is conditional logic, which you will use in almost every PLC program.
  • Structured Text data types

    TypeDescriptionExample
    BOOLBoolean (TRUE/FALSE)sensor_active : BOOL
    INT16-bit integercount : INT := 0
    REALFloating-point numbertemperature : REAL := 22.5
    STRINGText stringname : STRING := 'Pump_01'
    TIMEDuration valuedelay : TIME := T#5s

    Control structures

    IF / THEN / ELSE

    IF temperature > 80.0 THEN
        alarm := TRUE;
        coolingFan := TRUE;
    ELSIF temperature > 60.0 THEN
        alarm := FALSE;
        coolingFan := TRUE;
    ELSE
        alarm := FALSE;
        coolingFan := FALSE;
    END_IF;
    

    This is a syntax fragment. To run it, declare temperature : REAL;, alarm : BOOL; and coolingFan : BOOL; in the VAR panel.

    FOR loop

    FOR i := 1 TO 10 DO
        total := total + sensorValues[i];
    END_FOR;
    average := total / 10.0;
    

    This is a syntax fragment too. Declare i : INT;, total, average : REAL; and sensorValues : ARRAY[1..10] OF REAL; in the VAR panel.

    CASE statement

    CASE machineState OF
        0: status := 'IDLE';
        1: status := 'RUNNING';
        2: status := 'PAUSED';
        3: status := 'ERROR';
    END_CASE;
    

    Another syntax fragment: declare machineState : INT; and status : STRING; in the VAR panel.

    Next steps

    With the basics in place, open the interactive ST editor to write and simulate your own programs. Lesson 1 of the curriculum has you build a traffic light controller, which is a good first project.

    What to take away

    Structured Text is the most flexible of the IEC 61131-3 PLC languages. It uses constructs you may know from other programming: variables, conditionals and loops. ST skills transfer across all the major PLC platforms. Practise in a simulator before you deploy anything to real hardware.