The Scan Cycle
A PLC does not run your program once. It runs it over and over, in a fast loop called the scan. Understanding that loop explains almost everything about when an output changes — and it works the same whether you write in Ladder Logic or Structured Text.
The loop
Each scan, the PLC does three things in order:
- Read all inputs into memory.
- Evaluate your whole program, top to bottom.
- Write all output results out.
Then it starts again — typically many times a second. (In rungs.dev the simulator runs one scan every 100 ms, ten scans a second.)
Because the entire program is re-evaluated every scan, outputs always reflect the latest inputs. There is no "stuck" logic: an output turns off automatically the moment its condition becomes false — unless you deliberately latch it.
Before the first scan
When a program starts — on a real controller, the moment you switch it to Run — the PLC makes one preparation pass before the first scan. Rockwell® controllers call it prescan, and rungs.dev mirrors it. Your logic does not run yet; instead, every instruction is put into a known starting state:
- Timers reset. A
TONstarts from0; aTOFstarts already timed out, so its.DNbit is off. - One-shots and counters treat the starting inputs as already seen. An input that is already on when the program starts is not a new event — nothing fires and nothing counts until that input goes off and comes on again.
- Your rungs and assignments write nothing. Outputs simply start from their tag values, and non-retentive coils (
OTE) start off.
That second rule is the one to remember, because it protects real machines: a line powering up with a part already sitting on a sensor must not count that part as a fresh arrival. If your logic needs to react to "already on at startup", read the level (XIC) instead of an edge (ONS, a counter).
In rungs.dev, prescan runs every time you press Start in the simulator, and at the start of every test case.
Top to bottom, every scan
Step 2 — "evaluate top to bottom" — is the part that surprises people. The PLC works through your program in order:
- In Ladder Logic, it solves rung 0, then rung 1, then rung 2, and so on.
- In Structured Text, it runs the first statement, then the next, down the routine.
As it goes, it updates the bits each rung or statement writes, and everything below sees those updated values right away. Everything above has already run for this scan, using the values from before.
Later wins
One direct consequence: if two places write the same output, the last one to run wins, because its write is the one that survives to the end of the scan.
- In Ladder Logic, the lower rung wins.
- In Structured Text, the later statement wins.
The fix is the same in both: drive each output from exactly one place.
When a scan stops early
Some mistakes are serious enough that a real controller does not shrug them off — it raises a major fault and stops executing. rungs.dev mirrors this: when your program reads or writes an array with an index that is out of range (or divides by zero inside the index), or runs a timer with a negative preset, the scan stops at that instruction. Everything before it in the scan stays applied; everything after it does not run, and the value being written is left untouched. The fault appears in the status panel naming what went wrong, and a test scan that faults fails its test case — the same way the loop watchdog works.
This is deliberate: on a plant floor a fault like this halts the machine, so the simulator treats it as a bug to fix, not a value to work around.
Why order matters more than it looks
"Later wins" is the easy case — two writes to the same bit. The subtler case is when one rung or statement reads a bit that another one writes. Then the order decides whether the read is fresh or a scan old — and an output can end up changing one scan early or late even though the logic looks correct.
That is the next page: Scan Order and One-Scan Delays.
Summary
- A PLC runs your program in a loop: read inputs, evaluate top to bottom, write outputs, repeat.
- Before the first scan, a prescan pass resets timers and arms one-shots and counters — an input already on at startup never fires an edge.
- Rungs (Ladder) and statements (Structured Text) both run top to bottom, every scan.
- If two places write the same output, the later one wins — drive each output from one place.
- The order in which things run decides the timing of your outputs, not just the logic.
See the scan in action in the Rising Edge exercise — produce a single-scan pulse on each 0→1 transition.