Skip to content

18Standard function blocks

Counters: CTU, CTD and CTUD

The standard counters and their pins CU, CD, R, LD, PV, Q and CV, counting batches, and why an R_TRIG belongs in front of every one of them.

Lesson 18 of 3411 minIEC 61131-3

Three counters

Block

Direction

Distinct pins

CTU

up

CU, R

CTD

down

CD, LD

CTUD

both

CU, CD, R, LD, plus QU and QD

And the pins they share:

Pin

Type

Meaning

PV

INT

preset value

CV

INT

current value

Q

BOOL

the counter reached its target

R

BOOL

reset to zero (up counter)

LD

BOOL

load PV into CV (down counter)

CTU · counting up

CV starts at zero and gains one on every rising edge of CU. Q goes TRUE when CV >= PV. R forces it back to zero and wins over counting.

Batches(CU := PartEdge.Q, R := Reset, PV := 12);
Full  := Batches.Q;
Count := Batches.CV;

CTD · counting down

LD loads PV into CV, then every edge on CD takes one away. Q goes TRUE when CV <= 0. This is the "fill 20 drums and stop" shape: load 20, count down, and CV tells the operator how many are left.

CTUD · both ways

For stock: a part in counts up, a part out counts down. Note: the lab on this site supports CTU and CTD, not CTUD. The lab program below therefore uses a CTU and a CTD side by side, which is also what you do on a platform that lacks CTUD.

The golden rule: an edge in front of the counter

A standard counter detects the edge internally, but the signal reaching it is often not clean. Wire CU := LSH straight through while the level hovers at 90% and every wobble counts as a batch. Put an R_TRIG on the logical event that means "a batch finished", not on a raw sensor state:

FullEdge(CLK := LSH);
Batches(CU := FullEdge.Q, R := Stop, PV := 5);

If the sensor itself chatters, pass it through a short TON first and then the edge.

Common mistakes

  • Reading Q when you wanted CV for the HMI. Q is a yes/no question; CV is the number.
  • A permanent reset: a hard-wired R := TRUE keeps the counter at zero forever.
  • Forgetting the ceiling: CV is an INT, so 32767. A daily production total can pass that, so use a DINT in your own counter or reset at the start of each shift.
  • Expecting Q to reset the counter. It does not. CV keeps climbing above PV. Resetting is your job.

Try it in the lab

The lab runs in your browser: an ST editor, a virtual process that suits the lesson, the same program as a live ladder diagram, a watch table, and export to Siemens SCL.

What to try

  1. Run the program and press START: Left.CV starts at 8.
  2. Watch Total.CV rise with every box and Left.CV fall with the short boxes only.
  3. When Left.CV reaches 0 the belt stops and the lamp lights. Press START for a new order.
  4. Change PV := 8 to 3 and download again.

People find this lesson by searching

CTU structured textCTD down counter plcCTUD counterplc counter examplebatch counter plcCU R PV CV counterIEC 61131-3 counters

Course outline

  1. 01What is Structured Text, and when to use it instead of ladder
  2. 02How a PLC runs your code: the scan cycle and the process image
  3. 03Program structure: POUs, PROGRAM, FUNCTION and FUNCTION_BLOCK
  4. 04Your first program: starting a pump with seal-in logic
  5. 05Data types: BOOL, INT, REAL, TIME and the rest
  6. 06Declaring variables: VAR, VAR_INPUT and RETAIN
  7. 07Literals and constants: 16#FF, 2#1010 and T#5s
  8. 08Type conversion and scaling a 4-20 mA signal
  9. 09Assignment and arithmetic in ST: := and operator precedence
  10. 10Boolean logic, comparisons and bit operations
  11. 11Standard functions: ABS, SQRT, MIN, MAX, LIMIT, SEL, MUX
  12. 12IF, ELSIF and ELSE: the rules and when to avoid them
  13. 13The CASE statement and your first state machine
  14. 14The FOR loop and the watchdog
  15. 15WHILE, REPEAT, EXIT, CONTINUE and RETURN
  16. 16Edge detection: R_TRIG, F_TRIG and the "it fired every scan" bug
  17. 17Timers: TON, TOF and TP
  18. 18Counters: CTU, CTD and CTUD
  19. 19SR and RS flip-flops: which one wins
  20. 20Writing your own function
  21. 21Writing a function block
  22. 22Arrays in Structured Text
  23. 23Structs and enumerations
  24. 24References and pointers
  25. 25The state machine pattern: a batch sequence that cannot get stuck
  26. 26Alarm management to ISA-18.2: delay, deadband, latch, acknowledge, first-out
  27. 27Analogue signal processing: counts to engineering units, NE43 and filtering
  28. 28Closed-loop control in ST: hysteresis first, then a practical PID
  29. 29Motors and valves in practice: permissives, feedback and travel time
  30. 30The same ST on every platform: SCL, Studio 5000, CODESYS, TwinCAT
  31. 31Testing and debugging ST: the watch table, forcing and simulation
  32. 32An ST style guide: naming, comments, constants and version control
  33. 33Final project: a complete tank station with a state machine and alarms
  34. 3420 interview questions on ST and PLC programming, with answers