A complete course · 24 chapters · free

Structured Text: Zero to Advanced

Learn Structured Text the way a PLC runs it. Every example on these pages is a live program on a real ST engine: change it, run it, watch every variable scan by scan.

Start chapter 1

How each chapter works

  • Run it — Examples are live programs. Flip the inputs, edit the code and watch the engine execute it one 100 ms scan at a time.
  • Predict it — Before the answer, commit to a guess. A wrong guess shows you what you expected — that is where the learning is.
  • Prove it — Challenges are graded by a plant test bench: several cases, in order, on a virtual clock. Typing the answer in will not pass.

Contents

Part 1 · First contact

Read and write a working PROGRAM, and know what one scan does.

  1. Your first program and the scan

    Write a PROGRAM that runs, and learn the read–execute–write scan that every line of Structured Text lives inside.

  2. Variables and data types

    Declare BOOL, INT, DINT, REAL, TIME and STRING variables, give them initial values and choose the type that fits the signal.

  3. Expressions and operators

    Arithmetic, precedence, integer division, MOD and type conversion — and the rounding mistakes that hide in them.

  4. Boolean logic and interlocks

    AND, OR, NOT and XOR as machine permissives: seal-in circuits, fail-safe stops and interlocks you can read aloud.

Part 2 · Decisions

Make the program choose — cleanly, at the boundary, without surprises.

  1. IF, ELSIF and ELSE

    Choose between paths with IF, order ELSIF branches by priority, and avoid the missing-ELSE trap that latches outputs.

  2. Comparisons and thresholds

    Compare at the right boundary, add hysteresis and dead bands, and use LIMIT, MIN, MAX and SEL instead of long IF chains.

  3. CASE statements

    Select one behaviour from many with CASE: label lists, ranges, ELSE, and mode selectors that stay readable.

  4. Edges, latches and memory

    Detect rising and falling edges, set and reset latches with clear priority, and decide what the program must remember.

Part 3 · Time and counting

Use timers, counters and states to control what happens next.

  1. On-delay timers (TON)

    Use TON for delays, debouncing and confirmation times, and read its IN, PT, Q and ET pins scan by scan.

  2. Off-delay and pulse timers

    Keep things running after a signal drops with TOF, fire fixed pulses with TP, and build a flasher from two timers.

  3. Counters

    Count parts and events with CTU, CTD and CTUD, reset them deliberately, and count edges instead of scans.

  4. State machines

    Turn a machine sequence into named states and transitions with CASE, timers and clear entry conditions.

Part 4 · Data

Hold many values at once and process them inside one scan.

  1. Arrays and FOR loops

    Store many readings in an ARRAY and process them with FOR: totals, averages, minimum and maximum, all inside one scan.

  2. WHILE, REPEAT and EXIT

    Loop until a condition with WHILE and REPEAT, leave early with EXIT, and never use a loop to wait for the machine.

  3. STRUCT and ENUM types

    Group related values into a STRUCT, name states with an ENUM, and build arrays of structures for real equipment.

  4. Strings and time values

    Build messages with STRING functions, compute with TIME values, and keep text out of the control decisions.

Part 5 · Reusable code

Package logic as functions and blocks, and control real signals.

  1. Functions

    Write FUNCTIONs that turn inputs into one result with no memory, and use them for scaling, limits and checks.

  2. Function blocks

    Write FUNCTION_BLOCKs with inputs, outputs and memory, create instances, and reuse one motor block for many motors.

  3. Analog signals and alarms

    Scale 4–20 mA raw counts to engineering units, filter noise, detect broken wires and raise delayed alarms.

  4. Ramps and closed-loop control

    Ramp setpoints, close a loop with proportional and PI control, and limit the output so the actuator survives.

Part 6 · Professional ST

Write programs other engineers trust, test and maintain.

  1. Program structure and style

    Name, comment and organise ST so it reads like the machine, and carry it across CODESYS, TwinCAT, TIA and Studio 5000.

  2. Alarms and fault handling

    Detect faults, latch alarms until acknowledged, record the first-out cause and drive every output to a safe state.

  3. Testing and debugging

    Test ST with plant cases before the machine exists, watch values online, and debug from a hypothesis instead of guessing.

  4. Capstone: a bottle filling line

    Build a complete filling-line controller in ST — modes, sequence, timers, counts, alarms and tests — then plan what to learn next.

Where this course fits

This course teaches the language. When you want to practise, design a machine or go further, these sit next to it: