Skip to content
Tutorials Center
My learning

Topics

Lesson 20 of 37 ยท Timers, Counters and Sequences

PLC Counters - CTU, CTD and CTUD

Learn PLC counters: count-up (CTU), count-down (CTD) and up-down (CTUD) blocks, reset and load pins, edge triggering, plus runnable C stand-ins that model them.

Timers count time. Counters count events: bottles passing a sensor, parts entering a magazine, cycles of a press. A counter holds a number, changes it when an input goes from false to true, and raises an output when the number reaches a target. Counters are among the simplest PLC blocks, yet they hide one subtle idea that causes many bugs: they react to edges, not levels.

As in the rest of this track, a real PLC runtime is not available here. PLC code is shown read-only, and the runnable programs are C stand-ins that model the behavior of the blocks.

The three standard counters

IEC 61131-3 defines three counter blocks, each with a preset value PV and a current value CV:

Block Counts Output Q is true when
CTU up on each rising edge of CU CV >= PV
CTD down on each rising edge of CD CV <= 0
CTUD up on CU, down on CD QU: CV >= PV; QD: CV <= 0

CTU has a reset input R that sets CV to 0. CTD has a load input LD that sets CV to PV. CTUD has both. In Structured Text:

TEXT
(* Count 12 bottles, then trigger a case-packing step *)
bottleCount(CU := bottleSensor, R := caseFull, PV := 12);
packCase := bottleCount.Q;

Edge triggering

The count input is edge triggered. The counter increments when CU changes from FALSE to TRUE, not on every scan where CU is TRUE. This matters because a bottle may block a sensor for 80 ms, which is dozens of scans. A level-triggered counter would add dozens for one bottle.

TEXT
CU   __/โ€พโ€พโ€พโ€พโ€พโ€พโ€พ\______/โ€พโ€พโ€พโ€พ\____
CV   0 -> 1 ----------> 2 -------

Inside the block, the logic is "if CU and not previousCU then CV = CV + 1; previousCU = CU". The block stores previousCU for you. If you build a counter from basic contacts you must create that one-shot yourself; see edge detection.

A CTU in C

This stand-in keeps the previous input to find the rising edge. The scripted input is a noisy sensor sequence.

C C stand-in: an up counter with reset

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Watch how the run of four 1s at scans 6-9 adds only one to CV, and how Q stays true once the preset is reached, until reset.

CTD and CTUD in C

A down counter is the mirror image: load it with the preset, then count down. An up-down counter combines both, which suits storage: a parking garage adds a car at the entrance and subtracts one at the exit.

C C stand-in: up-down counter for a parking lot

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Notice the weakness: nothing stops CV from going negative when more cars leave than entered. Real systems either clamp the value or raise a fault when the count is impossible; a counter is only as good as its sensors.

Practical guidance

  • Choose the integer width. CTU counts in INT (up to 32,767). For long-running totals use CTU_DINT or CTU_LINT, or accumulate into a larger variable yourself.
  • Debounce the input. A bouncing contact makes false counts. Filter with a short on-delay timer (TON) before the counter.
  • Count fast events with hardware. If pulses arrive faster than the scan (an encoder at thousands of pulses per second) use a high-speed counter input, not a software block.
  • Plan the reset. Reset on completion of the batch, not at power-up unless you want to lose the count. Many PLCs keep counters in retentive memory so they survive a power cycle; know whether yours do.
  • Combine blocks. A counter plus a timer measures rates: count pulses for one second to get frequency.

Counters usually drive larger sequences, which you will build in sequencers and state machines.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. A sensor stays TRUE for 50 scans while one part passes. A CTU is connected to it. How much does CV increase?

    CTU is edge triggered: it counts the FALSE to TRUE change once, however long the input stays true.
  2. 2. Which pin of a CTD sets CV back to the preset value?

    CTD has a load input LD that copies PV into CV. R belongs to CTU, which sets CV to 0.
  3. 3. When is the output Q of a CTU true?

    CTU raises Q when the counted value reaches the preset, and keeps it until a reset lowers CV.
  4. 4. Why do bouncing switch contacts cause trouble for counters?

    One physical press can produce several quick true/false changes. Filter the signal first so only one edge reaches the counter.

Summary

  • Counters count rising edges, not levels; CTU counts up, CTD counts down, CTUD does both.
  • PV is the target, CV the current value, R resets to 0 and LD loads the preset.
  • Debounce noisy inputs, pick the right integer size, and use hardware counters for fast pulses.
  • The C programs are stand-ins that model the block logic.

Next: Edge Detection and One-Shots