02Getting started

How a PLC runs your code: the scan cycle and the process image

The scan cycle is the foundation of PLC programming: read the inputs, run the program, write the outputs, repeat.

Lesson 2 of 3410 minIEC 61131-3

The loop that never stops

A PLC does not run your program once. It repeats it thousands of times a minute. Each repetition is a scan cycle with three phases:

  1. Read inputs — the controller takes a snapshot of every field input into memory called the input image.
  2. Execute — your program runs from the first line to the last, reading the input image, not the terminal.
  3. Write outputs — the output image is transferred to the field terminals in one go.

Then it starts again. The lab on this site uses a fixed 50 ms cycle; a real PLC is typically 1 to 20 ms.

Why the image matters

An input value cannot change mid-scan. If you read Start at the top of the program and again at the bottom, you get the same value in that scan even if the operator pressed the button in between. That is what makes behaviour predictable.

Order matters

The program runs top to bottom, so the last assignment wins:

Pump := TRUE;
Pump := FALSE;   (* this is what reaches the output; the line above is lost *)

Scan time and the watchdog

Every PLC has a watchdog: if one scan takes longer than allowed, the controller stops and raises a fault. So:

  • Never write a WHILE whose condition may never become false.
  • Never put a million-iteration FOR in one scan.
  • Spread heavy work across several scans.

Tasks

A modern PLC lets you put your program in a cyclic task every 10 ms, and slow work such as communications in a 100 ms task. Timers count real time, so they stay correct whatever the scan time is.

Try it in the lab

The lab runs in your browser: an ST editor, a virtual tank with a pump and a valve, a watch table, and export to Siemens SCL.

What to try

  1. Run the program and watch the SCAN counter at the top.
  2. Press "One scan" instead of running, and see the counter advance by one.
  3. Swap the last two lines and see how the result changes.

Terms in this lesson

People find this lesson by searching

plc scan cycleprocess image plcscan timeplc watchdoghow does a plc workinput image output imagecyclic task plc

Course outline

  1. 01What is Structured Text, and when to use it instead of ladder
  2. 02How a PLC runs your code: the scan cycle and the process image
  3. 03Program structure: POUs, PROGRAM, FUNCTION and FUNCTION_BLOCK
  4. 04Your first program: starting a pump with seal-in logic
  5. 05Data types: BOOL, INT, REAL, TIME and the rest
  6. 06Declaring variables: VAR, VAR_INPUT and RETAIN
  7. 07Literals and constants: 16#FF, 2#1010 and T#5s
  8. 08Type conversion and scaling a 4-20 mA signal
  9. 09Assignment and arithmetic in ST: := and operator precedence
  10. 10Boolean logic, comparisons and bit operations
  11. 11Standard functions: ABS, SQRT, MIN, MAX, LIMIT, SEL, MUX
  12. 12IF, ELSIF and ELSE: the rules and when to avoid them
  13. 13The CASE statement and your first state machine
  14. 14The FOR loop and the watchdog
  15. 15WHILE, REPEAT, EXIT, CONTINUE and RETURN
  16. 16Edge detection: R_TRIG, F_TRIG and the "it fired every scan" bug
  17. 17Timers: TON, TOF and TP
  18. 18Counters: CTU, CTD and CTUD
  19. 19SR and RS flip-flops: which one wins
  20. 20Writing your own function
  21. 21Writing a function block
  22. 22Arrays in Structured Text
  23. 23Structs and enumerations
  24. 24References and pointers
  25. 25The state machine pattern: a batch sequence that cannot get stuck
  26. 26Alarm management to ISA-18.2: delay, deadband, latch, acknowledge, first-out
  27. 27Analogue signal processing: counts to engineering units, NE43 and filtering
  28. 28Closed-loop control in ST: hysteresis first, then a practical PID
  29. 29Motors and valves in practice: permissives, feedback and travel time
  30. 30The same ST on every platform: SCL, Studio 5000, CODESYS, TwinCAT
  31. 31Testing and debugging ST: the watch table, forcing and simulation
  32. 32An ST style guide: naming, comments, constants and version control
  33. 33Final project: a complete tank station with a state machine and alarms
  34. 3420 interview questions on ST and PLC programming, with answers