Programming PLCs: IEC 61131-3, response time and tasks
Programming PLCs with IEC 61131-3: the five languages, worst-case response time, tasks and priorities, function blocks in structured text.
https://www.electronics-engineering.com/en/plc-programming-iec-61131-3-structured-text-response-time/
The software of a modern plant can have hundreds of thousands of lines and stay in service for decades, modified by different people. The IEC 61131-3 standard fixes the PLC languages, but writing a good program also requires the rules of software engineering. In this lesson we see the languages of the standard, the worst-case response time of the PLC with its derivation and an example, tasks and priorities, function blocks with an example in structured text, and good design and testing practices.
The languages
IEC 61131-3 defines five languages, which can coexist in the same project:
- LD (ladder diagram): the relay schematic, readable by electricians, suited to simple Boolean logic (lesson on PLCs in Process Control);
- FBD (function block diagram): blocks connected by wires, natural for control loops;
- ST (structured text): a text language similar to Pascal, with expressions, loops and structures; the most suitable for algorithms, calculations and state machines;
- SFC: sequences (lesson on sequences);
- IL (instruction list), similar to assembly, declared obsolete in the third edition of the standard.
The third edition (2013) added object-oriented programming: methods, interfaces, inheritance of function blocks. The fourth edition, IEC 61131-3:2025, added UTF-8 strings and gathers in an annex the list of functions added, removed or deprecated since 2013: in new projects IL should no longer be used.
Which language to choose
Within the same project it is best to use each language where it works better: LD for interlocks and simple logic that the maintenance engineer must read on the plant, FBD for control loops and signal diagrams, ST for calculations, data handling and state machines, SFC to describe the phases of a sequence, which then call actions written in the other languages.
The cycle and the response time
A PLC runs in a cycle: it reads all the inputs into a memory image, runs the program on that image, writes all the outputs. The cycle lasts
, from less than a millisecond to a few tens of milliseconds.

Theorem. The time between the change of an input and the change of the output that depends on it is at most about
, plus the delays of the input filter and of the output.
Proof. In the worst case the input changes right after the inputs are read: the program of that cycle still uses the old value, and the new one is read at the start of the next cycle, after almost
. The program processes it and the output is written at the end of that cycle, after another
. In total almost
, to which the input filter (against bounces and disturbances) and the switching time of the output are added. ∎
Example. Cycle of 10 ms, input filter of 3 ms, relay output of 10 ms: up to
ms. An input pulse shorter than one cycle may never be seen at all. For fast events one uses interrupt inputs, fast counters implemented in hardware or tasks with a shorter cycle.
When the worst case is not enough
The cycle time is not constant: it depends on the branches of the program executed, on the communications and on the diagnostics. For the calculation one uses the maximum measured cycle time (PLCs show it in the diagnostics), not the average. If the signal comes from a fieldbus, the cycle of the bus must also be added to the response time.
Tasks and priorities
The program is divided into tasks with different periods and priorities: a fast task (1-2 ms) for the critical functions, a normal one (10-20 ms) for the logic, a slow one (100 ms or more) for the service functions, plus tasks triggered by events. It is the same fixed-priority scheduling as real-time systems (lesson on real-time systems in Operating Systems): the load of the fast tasks must be kept low, and the PLC supervises the cycle time with a watchdog that brings the outputs to a safe state if it is exceeded.
Example. A fast task with period 2 ms and execution of 0.4 ms, a normal one with period 10 ms and execution of 3 ms and a slow one with period 100 ms and execution of 10 ms load the CPU for
![Rendered by QuickLaTeX.com \[ U = \sum_i \frac{C_i}{T_i} = \frac{0{,}4}{2} + \frac{3}{10} + \frac{10}{100} = 0{,}2 + 0{,}3 + 0{,}1 = 0{,}6 \]](https://www.electronics-engineering.com/wp-content/ql-cache/quicklatex.com-53efd156ba2e1897b98e1054c04f9b43_l3.png)
With fixed priorities assigned by period (shorter period, higher priority) and deadlines equal to the periods, the Liu and Layland bound for three tasks is
: with
all the tasks meet their deadlines. It is a sufficient condition, not a necessary one: above the bound a finer analysis of the response times is needed.
Function blocks
A function block (FB) is a module with inputs, outputs and internal memory; as many instances are created as there are identical machines. The standard provides some standard ones, such as timers (TON, TOF, TP) and edge detectors (R_TRIG). An example in structured text: the command of a pump as a state machine, with thermal protection and reset.
FUNCTION_BLOCK FB_Pompa
VAR_INPUT
Avvio, Arresto, Termico : BOOL; (* Termico = relè termico scattato *)
END_VAR
VAR_OUTPUT
Contattore, Allarme : BOOL;
END_VAR
VAR
Stato : INT := 0; (* 0 ferma, 1 in marcia, 2 guasto *)
END_VAR
CASE Stato OF
0: Contattore := FALSE;
IF Avvio AND NOT Arresto AND NOT Termico THEN Stato := 1; END_IF;
1: Contattore := TRUE;
IF Termico THEN Stato := 2;
ELSIF Arresto THEN Stato := 0; END_IF;
2: Contattore := FALSE;
Allarme := TRUE;
IF NOT Termico AND Arresto THEN (* riarmo: guasto sparito e conferma *)
Allarme := FALSE; Stato := 0;
END_IF;
END_CASE;
END_FUNCTION_BLOCKEach state explicitly declares the outputs; the fault takes precedence over the stop; the reset requires that the cause has disappeared and an operator action, so the pump does not restart by itself. Ten pumps are ten instances of the same block, tested once.
Notes on the code
- The state is an integer for simplicity; in a real project an enumerated type is better (for example
(Ferma, InMarcia, Guasto)), so that the compiler checks the values and the code reads by itself. - The stop button is normally wired closed: if the wire breaks, the program sees a stop and the pump stops. In the block the input
Arrestomust therefore be obtained by negating the contact. - The block does not replace the safety guards wired or made with a safety PLC: it manages the command, not the emergency stop.
Good practices
- Structure: small blocks with a clear task; no global variables used everywhere; meaningful names and a shared convention.
- Separation between process logic, mode management, alarms and the operator interface.
- Libraries of tested blocks for recurring components (motors, valves, controllers), which make the code uniform and reduce errors.
- Version control of the code, with the history of changes: modern environments export the projects in text format (PLCopen XML, standardised as IEC 61131-10) precisely for this.
- Tests: tests of the blocks on a simulator, automatic regression tests, and virtual commissioning with a model of the plant (lesson on commissioning).
- Comments that explain the why, not the how.
- For motion control, the PLCopen specifications standardise the blocks (MC_MoveAbsolute and similar) across different manufacturers (lesson on drives).
In summary
- IEC 61131-3 defines LD, FBD, ST, SFC and IL; since 2013 there is object-oriented programming, the current edition is from 2025 and IL is no longer used in new projects.
- The PLC works in a cycle: the worst-case response time is about
plus the input and output delays (example: 33 ms with a 10 ms cycle). - Periodic tasks with fixed priority are verified as in real-time systems: in the example
, below the limit of 0.78. - A function block in ST with an explicit state machine, instantiated for each identical machine, makes the code tested once and reusable.
- Structure, libraries, version control and automatic tests are the same good practices of software, applied to the plant.






Ciao, sono l'autore di «Programmare i PLC: IEC 61131-3, tempo di risposta e blocchi funzionali». Domande, dubbi, segnalazioni di errori o richieste di approfondimento sono benvenuti: scriveteli qui sotto e vi rispondo io. Potete anche votare la pagina con le stelle all'inizio dell'articolo.Hi, I am the author of “Programming PLCs: IEC 61131-3, response time and function blocks”. Questions, doubts, error reports and requests for more detail are all welcome: write them below and I will answer. You can also rate the page with the stars at the top of the article.