Training Ladder Logic & IEC 61131-3 Programming IEC 61131-3 Timers, Counters & Structured Text
2 / 2

IEC 61131-3 Timers, Counters & Structured Text

60 min Ladder Logic & IEC 61131-3 Programming

IEC 61131-3 Timers, Counters & Structured Text

Beyond Boolean combinational logic, industrial programs require time-based sequencing and event counting. IEC 61131-3 standardises Timer (TON, TOF, TP) and Counter (CTU, CTD, CTUD) function blocks with well-defined timing semantics. Structured Text (ST) provides a Pascal-like language for arithmetic, loops, and state machines that cannot be expressed concisely in ladder.

1. TON — On-Delay Timer

The TON function block has three parameters:

  • IN (BOOL) — enable input. Timer accumulates while IN = TRUE.
  • PT (TIME) — preset time, e.g., T#5s.
  • ET (TIME) — elapsed time output, incremented each scan: $ET_{n+1} = ET_n + T_{scan}$ while IN = TRUE.
  • Q (BOOL) — output. $Q = (IN \text{ AND } ET \geq PT)$.

Timing accuracy is limited by scan jitter: the true output delay is $PT \pm T_{scan}$, meaning a 5 s preset on a 10 ms scan can fire anywhere in [4.990 s, 5.010 s]. For sub-millisecond timing, a hardware timer interrupt or high-speed module is required.

2. CTU — Up-Counter

The CTU counts rising edges on the CU (count-up) input:

$$CV_{n+1} = \begin{cases} 0 & \text{if RESET = TRUE} \\ CV_n + 1 & \text{if CU rises and } CV_n < PV \\ CV_n & \text{otherwise} \end{cases}$$

Output $Q = (CV \geq PV)$. Counter preset $PV$ is an INT (−32768 to +32767) or DINT (32-bit). Overflow behaviour is implementation-defined — always use DINT for production batch counting.

3. Structured Text (ST) for Complex Logic

ST is the highest-level IEC 61131-3 language, syntactically close to Pascal. A PID algorithm in ST:

error := setpoint - process_var;
integral := integral + error * dt;
if integral > integral_max then integral := integral_max;
elif integral < integral_min then integral := integral_min;
end_if;
derivative := (error - prev_error) / dt;
output := Kp*error + Ki*integral + Kd*derivative;
prev_error := error;

ST is particularly powerful for:

  • Mathematical expressions with precedence (no need for multiple rungs)
  • FOR/WHILE loops over arrays (e.g., sliding-window averages)
  • CASE statements implementing state machines with dozens of states
  • Calling function blocks and user-defined functions (POU architecture)

4. Sequential Function Chart (SFC) — GRAFCET

SFC (IEC 61131-3 §6.8) models sequential processes as a directed graph of steps and transitions. A step is active or inactive; transitions fire when their condition becomes TRUE, deactivating the current step and activating the next. Convergence/divergence tokens model parallel branches (AND-divergence) and alternative branches (OR-divergence). SFC is preferred over long CASE statements for >10-state machines because the graph is self-documenting and auditable.

Worked Example — 3-Step Filling Sequence

States: S0 (idle), S1 (filling), S2 (draining).
Transitions: T0→S1: start button AND tank_empty; S1→S2: high_level_switch; S2→S0: low_level_switch.
In Structured Text (CASE implementation):

CASE state OF
  0: fill_valve := FALSE; drain_valve := FALSE;
     IF start AND tank_empty THEN state := 1; END_IF;
  1: fill_valve := TRUE;
     IF high_level THEN state := 2; END_IF;
  2: drain_valve := TRUE; fill_valve := FALSE;
     IF low_level THEN state := 0; END_IF;
END_CASE;

The TON in S2 ensures a minimum drain time of 30 s even if the level switch fails low: add IF low_level AND drain_timer.Q THEN state := 0;. ✓

IEC 61131-3 TON Timer Simulator
ET (elapsed) =0ms
Q (done) =0
Remaining =3000ms

Practice Problems

1. A conveyor belt must run for exactly 10 s after a part passes a photoeye. Write the TON ladder rung and specify PT. What is the worst-case timing error on a 20 ms scan?
2. A bottling line counts 144 bottles per case. Write the CTU/CTUD logic to count 144 rising edges on a photosensor and pulse a case-complete signal for one scan.
3. Convert the 3-step filling CASE statement above into an SFC diagram showing steps, transitions, and action blocks. Identify which transitions require both a sensor condition AND a timer condition.