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

BlockNameOutput Q behaviour
TONon-delaygoes TRUE once IN has been TRUE continuously for PT; resets immediately when IN = FALSE
TOFoff-delayTRUE as soon as IN = TRUE; stays TRUE for PT after IN goes FALSE
TPpulseon 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

BlockBehaviour
CTUrising edge on CU increments CV; Q = (CV ≥ PV); R resets CV to 0
CTDrising edge on CD decrements CV; Q = (CV ≤ 0); LD loads CV := PV
CTUDboth 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

BlockOutput Q
R_TRIGTRUE for one cycle after CLK goes from FALSE to TRUE
F_TRIGTRUE 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

Loading the map…

Sources

  1. TwinCAT 3 PLC Library Tc2_Standard — Beckhoff Automation
  2. CODESYS Standard library documentation — CODESYS GmbH
  3. IEC 61131-3:2025 Programmable controllers — Part 3: Programming languages — IEC

Updated 2026-10-01