LadderLogicOnline

Learn › Getting started

How a PLC scan cycle works

A PLC runs your program in a loop: read the inputs, solve the rungs from top to bottom, update the outputs, repeat. Understanding that loop explains most of the "why did it do that?" moments new programmers run into.

The three steps of a scan

Every scan does the same three things, in the same order:

  1. Read inputs. The PLC takes a snapshot of its inputs: which buttons are pressed, which sensors are blocked. In the Playground, this is the simulator's push buttons, photo eyes and vehicle sensors writing their tags.
  2. Solve the program. Starting at rung 0, the PLC evaluates every rung in order, top to bottom. Within a rung it works left to right, deciding whether power reaches each instruction, then carrying out each output instruction.
  3. Write outputs. The results go out to the real world. Motors start, valves open, lights change. In the Playground, conveyors, transfers and traffic lights react to their tags.

Then it starts over immediately. On real hardware a scan typically takes somewhere between under a millisecond and tens of milliseconds, depending on the program and the controller. The Playground scans once per screen refresh, usually about 60 times a second. Either way, it's fast enough that a person pressing a button never notices the delay.

Top to bottom: why rung order matters

When an output instruction runs, it writes its tag right away. Any rung below it in the same scan sees the new value. Any rung above it has already been solved, so it won't see the change until the next scan.

 |   Start                                  Step_1  |
0|------] [--------------------------------( )----|
 |   Step_1                                 Step_2  |
1|------] [--------------------------------( )----|
When Start turns on, Step_1 and Step_2 both turn on in the same scan, because rung 1 reads the value rung 0 just wrote.

Now swap the rungs: put the Step_2 rung first. On the scan where Start turns on, rung 0 reads Step_1 while it's still off, so Step_2 stays off. Rung 1 then turns Step_1 on. On the next scan, Step_2 finally turns on. The difference is a single scan, a fraction of a second, but in logic that depends on exact timing (one-shots, sequences, handshakes between steps) that single scan can change the result.

A good habit: write rungs in the order things happen. Inputs and conditions near the top, the logic that uses them below, and outputs to the machine near the bottom.

The last rung to write a tag wins

Because outputs are written as the program is solved, if two OTE instructions write the same tag, the one lower in the program decides what that tag is at the end of the scan. The upper rung's result is simply overwritten. This is the most common bug in beginner programs: "my output won't turn on even though the rung is true." Search your program for a second rung writing the same tag. The OTE lesson covers this in detail.

Within a rung: left to right

Inside a rung, the PLC works column by column from left to right. At each column it decides which instructions get power. Where parallel branches join back together, they share their power: if any branch reaching the join is true, everything after the join gets power. That's how parallel branches make OR logic.

Output instructions act as soon as the solver reaches them. A rung can have more than one output (for example, a coil on one branch and a timer on another), and each one sees whether power reached it.

Transitions happen between scans

Some instructions care about changes rather than steady states. A counter (CTU) adds one only when its rung goes from false to true, not on every scan the rung stays true. It does this by remembering the rung's state from the previous scan in its .CU bit. One-shot instructions (ONS, OSR, OSF) work the same way, with a storage bit. If the scan didn't repeat, there would be nothing to compare with. See counters and one-shots.

Timers and the scan

Timers measure real time, not scans. Each scan, a running timer adds the time that has passed since the previous scan to its accumulator. That keeps timing accurate whether the scan rate is 60 times a second or 1,000. In the Playground, if a frame stalls (say the browser tab was in the background), a single scan adds at most a quarter of a second, so timers don't jump far ahead when you come back.

Try it in the Playground

  1. Open the Playground and add two BOOL tags, Start and Step_1, plus Step_2.
  2. Build the two rungs from the diagram above: XIC Start to OTE Step_1, then XIC Step_1 to OTE Step_2.
  3. Press Run and click Start in the tag list to toggle it. Both outputs come on together.
  4. Now add a third rung at the bottom that writes OTE Step_1 from a different tag, such as XIC Override. With Override off, Step_1 stays off no matter what Start does: the bottom rung wins. Delete it when you're done.

Key points