ExamRobotic

The PLC scan cycle

Level: Basic 8 min read 45 questions

A PLC does not “watch” its inputs all the time or react instantly like a relay circuit. It works in rounds: it reads, thinks, acts and starts again, hundreds of times per second. That round is called the scan cycle. Really understanding it is what most separates a good programmer from a bad one: it explains why an output written twice ignores you, why a counter counts too much or why a PLC goes to STOP for no apparent reason.

What the PLC does in each scan

With small differences between manufacturers, every PLC repeats these steps:

  1. Read the inputs. The PLC copies the state of all the physical inputs into an area of memory called the process image of the inputs.
  2. Execute the program. It runs through the program from top to bottom, network by network, using that copy, not the real inputs. In Siemens PLCs, the main cyclic program is organisation block OB1 (“Main”).
  3. Write the outputs. The results the program has left in the process image of the outputs are copied to the physical outputs.
  4. Internal tasks. Communications (with the HMI, other PLCs or the computer), diagnostics and self-tests.

And round again. The time a complete round takes is the cycle time (or scan time).

1. Read inputs copy to the input image 2. Run the program top to bottom (OB1) 3. Write outputs from the output image 4. Internal tasks comms and diagnostics Cycle time = one complete scan
The scan cycle: the program works with a “snapshot” of the inputs, and the outputs are updated at the end.

The process image: the program works with a snapshot

Having the program use a copy of the inputs has a huge advantage: during the whole scan, an input has the same value in every network, even if the real sensor changes halfway through. Without it, network 3 could see a part and network 40 not see it, and the program would make contradictory decisions.

The same happens with the outputs, but the other way round: the program writes to the image and the physical output only changes at the end of the scan. That is why, if an output is written in two places, only the last write counts. We will come back to this with the double coil.

Direct accessWhen a signal must be read or written at the exact moment, without waiting for the cycle, many PLCs allow direct access to the I/O. In TIA Portal this is done by adding :P to the address (for example, %I0.0:P). It is for specific cases, not a habit: it breaks the advantage of the consistent “snapshot”.

The cycle time

A scan normally lasts a few milliseconds, but not always the same: it depends on the CPU, the size of the program, which parts run in that scan and the communications. What you need to know:

Signals faster than the cycle

If a signal switches on and off between two input reads, the program never finds out. It happens with very short pulses: an encoder, a sensor watching small parts go by very fast, a pulse from another device. For those cases there are specific solutions:

What does not work is trying to “read faster” in the normal program: the problem is not the program, it is when the input image is updated.

sensor pulse readreadreadread Every read sees 0: the program never finds out about the pulse
A pulse that starts and ends between two input reads is lost.

Order matters: top to bottom

The program runs in order. If a memory bit is calculated in network 10 and used in network 3, network 3 reads the value it had in the previous scan. Often it does not matter, but sometimes it causes one-scan delays or strange behaviour that is hard to spot. Rule of thumb: calculate things before using them, and organise the program in a logical order (first inputs and conditions, then sequences, outputs at the end).

The double coil: the classic mistake

Writing the same output (or the same memory bit) with two coils in different places in the program is a very common mistake. Since the physical output is only updated at the end, the last coil executed wins and the first one is useless. The symptom is baffling: you look at the first network, see that its condition is true, and yet the output does not turn on.

Typical mistakeTurning the motor on “from automatic” in one network and “from manual” in another, each with its own coil. The right way is a single coil per output, with the conditions combined: Motor = Auto_requests_motor + Manual_requests_motor. Set and Reset instructions are the exception, but even then it is best to keep them together and in plain sight.

Edges: detecting the change, not the state

A push button stays pressed for many scans. If you use it to increment a counter “while it is 1”, it will count once per scan: hundreds of times in one press. To do something only once per press you use an edge:

To know whether there has been a change, the PLC compares the current value with the one from the previous scan, which it stores in a memory bit belonging to that instruction. That bit cannot be shared between two different edge detections: if two edges use the same memory bit, one of them will stop working.

Loops: the program cannot wait

Another mistake made by people who do not understand the cycle is writing something like this in Structured Text:

WHILE NOT "Part_sensor" DO
    ;  // wait for the part to arrive
END_WHILE;

It looks logical, but while the program is inside the loop the input is not read again: the process image is only updated when the scan ends. The loop never ends, the scan grows until it exceeds the maximum time and the PLC goes to STOP. The right way is to ask in every scan: “has the part arrived? if not, carry on with everything else and look again in the next scan”. Waits are programmed with steps (a sequence) and timers, never with loops.

Timers and the cycle

A timer measures real time, but the program only finds out its state when it executes the timer instruction. That is why the timer instruction must run in every scan: if it sits inside a jump or a block that is not always called, it may not update the way you expect. And a timer is no more precise than the cycle: with a 10 ms cycle, the output of a 1 s timer will be seen with an error of up to one scan.

Start-up, STOP and retentive memory

Typical mistakes that give away someone who does not understand the cycle

Summary

Test what you have learned

45 practical questions with explained answers. Free, no sign-up.

Start the test