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.
Three counters
Block | Direction | Distinct pins |
|---|---|---|
| up | |
| down | |
| both | |
And the pins they share:
Pin | Type | Meaning |
|---|---|---|
| INT | preset value |
| INT | current value |
| BOOL | the counter reached its target |
| BOOL | reset to zero (up counter) |
| BOOL | load |
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.
Qis a yes/no question;CVis the number. - A permanent reset: a hard-wired
R := TRUEkeeps the counter at zero forever. - Forgetting the ceiling:
CVis anINT, so 32767. A daily production total can pass that, so use aDINTin your own counter or reset at the start of each shift. - Expecting Q to reset the counter. It does not.
CVkeeps climbing abovePV. 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
- Run the program and press START: Left.CV starts at 8.
- Watch Total.CV rise with every box and Left.CV fall with the short boxes only.
- When Left.CV reaches 0 the belt stops and the lamp lights. Press START for a new order.
- 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
- 01What is Structured Text, and when to use it instead of ladder
- 02How a PLC runs your code: the scan cycle and the process image
- 03Program structure: POUs, PROGRAM, FUNCTION and FUNCTION_BLOCK
- 04Your first program: starting a pump with seal-in logic
- 05Data types: BOOL, INT, REAL, TIME and the rest
- 06Declaring variables: VAR, VAR_INPUT and RETAIN
- 07Literals and constants: 16#FF, 2#1010 and T#5s
- 08Type conversion and scaling a 4-20 mA signal
- 09Assignment and arithmetic in ST: := and operator precedence
- 10Boolean logic, comparisons and bit operations
- 11Standard functions: ABS, SQRT, MIN, MAX, LIMIT, SEL, MUX
- 12IF, ELSIF and ELSE: the rules and when to avoid them
- 13The CASE statement and your first state machine
- 14The FOR loop and the watchdog
- 15WHILE, REPEAT, EXIT, CONTINUE and RETURN
- 16Edge detection: R_TRIG, F_TRIG and the "it fired every scan" bug
- 17Timers: TON, TOF and TP
- 18Counters: CTU, CTD and CTUD
- 19SR and RS flip-flops: which one wins
- 20Writing your own function
- 21Writing a function block
- 22Arrays in Structured Text
- 23Structs and enumerations
- 24References and pointers
- 25The state machine pattern: a batch sequence that cannot get stuck
- 26Alarm management to ISA-18.2: delay, deadband, latch, acknowledge, first-out
- 27Analogue signal processing: counts to engineering units, NE43 and filtering
- 28Closed-loop control in ST: hysteresis first, then a practical PID
- 29Motors and valves in practice: permissives, feedback and travel time
- 30The same ST on every platform: SCL, Studio 5000, CODESYS, TwinCAT
- 31Testing and debugging ST: the watch table, forcing and simulation
- 32An ST style guide: naming, comments, constants and version control
- 33Final project: a complete tank station with a state machine and alarms
- 3420 interview questions on ST and PLC programming, with answers