Software engineers coming from general-purpose computing are often surprised by their first week programming a PLC. A PLC does not wait for a user click and exit when main() returns; it runs an endless, time-bounded loop — the PLC scan cycle — designed to guarantee determinism: given an input change on a sensor, the controller must evaluate logic and drive the corresponding output within a predictable upper time bound, every single time.
Misunderstanding how the scan cycle interacts with I/O image tables, multitasking schedulers and watchdog timers is behind some of the hardest-to-reproduce bugs on the factory floor: missed high-speed part sensors, race conditions between rungs, and intermittent CPU watchdog trips during HMI bursts or recipe loads.
The four phases of a classic PLC scan
In a traditional single-task cyclic PLC scan, the operating system repeats four distinct phases continuously:
- Input scan (read inputs into the process image): The CPU copies the physical state of local and remote field inputs into a memory buffer called the Process Image Input (PII) table.
- Program execution: The CPU executes your IEC 61131-3 user logic — Ladder Diagram rungs top-to-bottom, left-to-right, or Structured Text / FBD / SFC routines in configured order — reading from the input image and writing to the Process Image Output (PIQ) table.
- Output scan (write outputs to hardware): At the end of logic execution, the CPU transfers the output image table to physical output cards and remote fieldbus devices.
- System housekeeping and communications: The CPU services HMI/SCADA polling, engineering tool online monitoring, OPC UA subscriptions, diagnostics and background memory checks.
+-------------------+ +---------------------+ +--------------------+ +-----------------------+
| 1. Read Physical | --> | 2. Execute User | --> | 3. Write Output | --> | 4. Comms, Diagnostics |
| Inputs -> PII | | Logic (PII->PIQ) | | Image -> Field | | & Housekeeping |
+-------------------+ +---------------------+ +--------------------+ +-----------------------+
^ |
+---------------------------------- Repeat Cycle ----------------------------------+
Why process image tables matter
Reading and writing through a frozen snapshot (PII/PIQ) instead of hitting physical I/O mid-scan ensures logical consistency: if PartSensor is TRUE on Rung 1, it is guaranteed to still be TRUE on Rung 240 of the same scan.
However, not all modern PLCs behave strictly synchronously:
| Platform Family | Default I/O Update Behavior | Practical Implication for Programmers |
|---|---|---|
| Siemens S7-1200 / S7-1500 | Synchronous to OB1 (or assigned PIP — Process Image Partition for cyclic/isochronous OBs) | Inputs remain consistent throughout the OB unless you explicitly use peripheral direct access (:P) |
| Rockwell ControlLogix / CompactLogix | Asynchronous to the continuous task (I/O updates at each module's RPI independently of logic execution) | An input tag can change state halfway through a routine! Best practice: buffer inputs into internal tags at the start of the task and map outputs at the end |
| Beckhoff TwinCAT / CODESYS | Synchronous per task (each real-time task has its own cycle and mapped EtherCAT/fieldbus process image) | Deterministic per-task snapshots; cross-task shared variables need care to avoid torn reads/writes |
Ignoring asynchronous I/O on a Logix controller is a classic trap: if Rung 10 checks XIC StartButton and Rung 85 checks XIO StartButton expecting mutual exclusion within one scan, an asynchronous RPI update landing between those rungs can make both branches true — or both false — in the same scan.
Calculating worst-case response time (the 2× scan rule)
Suppose a PLC has a 10 ms scan time and a photoelectric sensor detects a bottle passing at high speed. What is the worst-case delay between the sensor turning ON and a pneumatic reject valve energizing?
- The sensor turns ON 1 microsecond after the Input Scan finishes (so the CPU misses it on Scan 1) → wait almost 1 full scan (10 ms).
- Scan 2 reads the input, runs logic and updates the output at the end of Scan 2 → another 1 full scan (10 ms).
- Add hardware input filter delay (often 1–5 ms configurable), fieldbus cycle time (e.g., 2–8 ms on PROFINET or EtherNet/IP) and actuator mechanical response (e.g., 15 ms solenoid valve).
Thus, the controller's own worst-case I/O latency is roughly 2 × Scan Time + I/O filter + fieldbus latency. Furthermore, to guarantee a pulse from a moving part is never missed in normal cyclic logic, the pulse duration must exceed at least 1 × worst-case scan time + input filter time (in practice, leave a 2× safety factor).
Modern multitasking: continuous, periodic and event tasks
Modern PACs and soft-PLCs rarely dump everything into one giant continuous loop. Instead, engineers split code into prioritized tasks:
- Isochronous / high-priority cyclic tasks (0.5–4 ms): Motion control, closed-loop fast PID control (pressure, tension, flow), high-speed registration and servo coordination.
- Standard periodic tasks (10–50 ms): General machine sequencing, conveyors, interlocks, valve actuation and PackML state machines.
- Slow periodic tasks (100–500 ms): Temperature loops, tank level scaling, HMI data aggregation, OEE counters and diagnostics.
- Event / hardware interrupt tasks: Triggered immediately by a digital input rising edge, high-speed counter match or fieldbus alarm — bypassing normal scan latency for microsecond-level reactions.
The golden rule of periodic vs continuous tasks
Use periodic tasks for PID loops, totalizers and velocity calculations so the time step Δt is deterministic and known. Running a PID instruction inside a free-running continuous task whose cycle fluctuates between 4 ms and 22 ms depending on recipe branches causes integral and derivative gain distortion.
Keep total CPU utilization across all real-time tasks safely below 60%–70% so communication bursts, online code edits and diagnostic logging never starve lower-priority tasks or cause periodic task overruns.
Watchdog timers: what they protect and why they trip
Every PLC monitors execution time with a watchdog timer (typically defaulted to 100–500 ms, configurable per task). If a scan fails to complete before the watchdog expires, the CPU assumes software has locked up, immediately transitions to STOP / Major Fault, and drives outputs to their safe fallback state.
Five common causes of watchdog faults in production
- Unbounded
WHILEorREPEATloops in Structured Text: Waiting inside a loop for a field sensor to change (WHILE NOT ValveOpen DO ... END_WHILE;) freezes the scan completely — the PLC can never reach the next Input Scan to seeValveOpenbecome true! Never block inside PLC code; always advance state machines one scan at a time. - Array/recipe search spikes: Sorting or scanning a 10,000-element array in one scan during a rare batch lookup pushes one scan past the watchdog limit. Spread heavy loops across multiple scans (chunking 100 items per scan).
- Excessive indirect addressing or recursive calls: Deep function calls or malformed pointer indexes that loop unexpectedly.
- Communication time-slice starvation: On controllers where system communications share the scan budget (such as poorly tuned continuous-task overhead percentages), heavy SCADA/OPC UA browsing or simultaneous HMI screens can stretch cycle times unexpectedly.
- Forgetting to separate safety from scan timing: Never rely on a standard PLC watchdog alone for personnel safety — functional safety functions belong in certified safety PLCs with dedicated dual-channel watchdogs and ISO 13849 / IEC 62061 architectures.
Commissioning checklist for deterministic PLC timing
Before signing off Factory Acceptance Testing (FAT), capture and document these five metrics in the PLC diagnostics buffer:
- Current, average and maximum scan time for every task under full simulated production load (including active SCADA, historian and HMI polling).
- Worst-case jitter on periodic motion and PID tasks — confirm zero task overruns over a 24-hour soak test.
- Input debounce filter times matched to the fastest sensor pulses on the machine (or moved to a hardware high-speed counter module when pulse rates exceed ~200 Hz).
- Explicit I/O buffering routines if deploying on an asynchronous-I/O controller family.
- Watchdog threshold and fault-handler behavior verified so outputs fail to their documented safe states on a fault.