Skip to content
Tutorials Center
My learning

Topics

Lesson 3 of 37 ยท Getting Started

The PLC Scan Cycle

Understand the PLC scan cycle: input image, program execution, output image, scan time and why rung order matters, with runnable C simulations of a scan.

Every PLC program lives inside a loop called the scan cycle. Understanding it is the single most important step in PLC programming, because many baffling bugs ("why did my output change one scan late?") are just the scan cycle doing exactly what it is designed to do.

In this lesson you will follow one scan from start to finish, learn what the input image and output image are, see why the order of your logic matters, and simulate a scan in C.

The four steps of a scan

flowchart TD
    A["1. Read all inputs<br/>copy to input image"] --> B["2. Execute the program<br/>top to bottom"]
    B --> C["3. Write the output image<br/>to the output modules"]
    C --> D["4. Housekeeping<br/>communications, diagnostics"]
    D --> A

Figure: one pass of the loop is one scan. It repeats forever while the CPU is in RUN.

  1. Input scan. The CPU reads every physical input once and stores the values in a memory area called the input image table (process image of inputs).
  2. Program scan. The program runs from the first instruction to the last. It reads the image, not the live terminals.
  3. Output scan. Results written during the program go to the output image table, and only now are copied to the real output terminals.
  4. Housekeeping. Communication with HMIs and programming tools, self-checks, and updating timers and watchdogs.

One scan

Read inputs Freeze a snapshot
Run logic Use the snapshot
Write outputs All at once

Repeat every few ms

1 / 2

Why an image?

Imagine an input changed halfway through your program. Rung 3 might see 0 while rung 40 sees 1 for the same sensor. Snapshotting inputs at the start gives the whole program one consistent view of the world for the entire scan. The same holds for outputs: they change together at the end.

Scan time

The total time of one loop is the scan time. It grows with program size and communication load. A typical small PLC scans in 1-20 ms. Two consequences matter:

  • A short input pulse (shorter than the scan time) can be missed entirely. Fast signals need special hardware such as high-speed counters or interrupt inputs.
  • If the scan takes too long, the CPU's watchdog timer trips and the PLC faults to STOP, a safety feature that detects a program that is stuck.
C Stand-in: a scan with input and output images (C model)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Rung order matters

Within one scan, the program runs in order and later logic sees values changed by earlier logic. Consider two statements. If B is computed before A is updated, B uses the old A for this scan; the new A reaches B one scan later.

C Stand-in: execution order changes the result (C model)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

In the second order, b lags a by exactly one scan. In ladder logic the same thing happens when a rung that reads a coil is placed above the rung that writes it.

Write logic in the order of cause and effect: read inputs, derive conditions, then drive outputs. This keeps scan-delay surprises out of your program.

Double coils and the last write wins

If two places in the program write to the same output, the last write in the scan wins, because the output image is overwritten. This "double coil" bug is common and hard to spot. Write each output in exactly one place.

C Stand-in: the double coil problem (C model)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

The motor is off although start is true, because the second statement overwrote it. The fix is to combine conditions in one place: motor = start && !guard_open;.

Interrupts and tasks

Some jobs cannot wait for the main scan. Modern PLCs support periodic tasks (run a program every 10 ms exactly, for example for PID) and interrupt tasks (run when an event occurs). The main task is the ordinary scan described above; others preempt it. You will see how IEC 61131-3 organizes programs and tasks in the next lessons.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does the program read during the program scan?

    Inputs are copied into an image at the start, and the program uses that snapshot.
  2. 2. Why can a very short pulse be missed?

    Inputs are only sampled once per scan, so pulses shorter than the scan can fall in the gaps.
  3. 3. If two rungs write the same output, which one decides its final state?

    The last write overwrites the output image, so the earlier one is lost.
  4. 4. What is the watchdog for?

    If the scan exceeds the watchdog time, the PLC faults to prevent running a stuck program.

Summary

  • A scan = read inputs, run program, write outputs, housekeeping, repeat.
  • Programs read an input image and write an output image, so each scan sees a consistent snapshot.
  • Order of logic changes behavior by one scan; avoid double coils.
  • Scan time limits how fast a signal the PLC can reliably see.

Next: IEC 61131-3 Languages