Timers, counters and edges (TON, TOF, TP, CTU, R_TRIG)
Behaviour of the standard IEC 61131-3 timers, counters and edge detectors, timing in words, and common usage mistakes.
All blocks below are standard IEC 61131-3 function blocks. They are in the CODESYS Standard library, TwinCAT Tc2_Standard and TIA Portal (on Siemens as IEC timers/counters TON, CTU etc. with an instance in a DB).
Timers
| Block | Name | Output Q behaviour |
|---|---|---|
| TON | on-delay | goes TRUE once IN has been TRUE continuously for PT; resets immediately when IN = FALSE |
| TOF | off-delay | TRUE as soon as IN = TRUE; stays TRUE for PT after IN goes FALSE |
| TP | pulse | on a rising edge of IN gives a pulse of length PT; further edges during the pulse are ignored |
ET (elapsed time) shows the elapsed time. Typical uses:
- TON — debouncing and filtering short signals, feedback monitoring ("motor did not confirm start within 3 s"), alarm delays on level.
- TOF — fan run-on after stop, holding a signal after it drops.
- TP — coil pulse, fixed-length command.
tFeedback(IN := Run AND NOT Feedback, PT := T#3S);
IF tFeedback.Q THEN
Fault := TRUE; (* no feedback within 3 s *)
END_IF;
Counters
| Block | Behaviour |
|---|---|
| CTU | rising edge on CU increments CV; Q = (CV ≥ PV); R resets CV to 0 |
| CTD | rising edge on CD decrements CV; Q = (CV ≤ 0); LD loads CV := PV |
| CTUD | both combined, outputs QU and QD |
Counters respond to an edge, not a level: a held input counts once. Counting is limited by the width of CV (usually INT/WORD) and by the pulse having to last longer than the PLC cycle. For high-frequency encoders and flow meters use high-speed counter modules.
Edges
| Block | Output Q |
|---|---|
| R_TRIG | TRUE for one cycle after CLK goes from FALSE to TRUE |
| F_TRIG | TRUE for one cycle after CLK goes from TRUE to FALSE |
Internally the block just remembers CLK from the previous cycle. Manual equivalent:
StartPulse := Start AND NOT StartPrev;
StartPrev := Start;
Timer and edge pitfalls in the scan cycle
- One timer instance used in two places: the second call overwrites the first one's state; the timer never completes or fires at the wrong time.
- Conditionally called timer (
IF x THEN t(IN := ...)): while the condition is false the block is not called and never sees IN going FALSE — its output freezes. - An edge on a signal that changes twice within one cycle is missed — the process image only captures the state at the start of the cycle.
- R_TRIG on an output computed later in the same cycle: the edge appears one cycle later than expected.
- Using a timer to measure short intervals precisely: resolution is limited by the cycle time.
Tools
- Function Block Generator — motor and valve with a TON for feedback monitoring.
- PLC Code Reviewer — repeated instance call rule (PLC-W011).
- Interlock Matrix Builder — generates interlock logic in ST.
Connections
Open in Knowledge Graph →Loading the map…
Sources
- TwinCAT 3 PLC Library Tc2_Standard — Beckhoff Automation
- CODESYS Standard library documentation — CODESYS GmbH
- IEC 61131-3:2025 Programmable controllers — Part 3: Programming languages — IEC
Updated 2026-10-01