Counter Operations
CTU, CTD, CTUD & Legacy S5
Event counting is at the heart of industrial control. Master standard IEC counters and legacy instructions in TIA Portal for your production applications.
What are Counter Operations in TIA Portal ?
Counter operations in TIA Portal allow you to count internal program events or external signals (rising edges). These are functional blocks compliant with the IEC 61131-3 standard that handle incrementing, decrementing, resetting, and comparison with a preset value (PV).
TIA Portal offers 3 types of IEC counters (CTU, CTD, CTUD) as well as S5 compatibility instructions (Legacy) and counter coils. Each counter instance requires its own instance data block (IDB) or can be integrated as a multi-instance in an FB.
IEC Counters
The 3 standard counting functional blocks — flexible and robust
CTU — Count Up
What it does
Counts rising edges on the CU count input. Each time CU transitions from FALSE to TRUE, the current count value CV increases by 1. When CV becomes greater than or equal to the preset value PV, the Q output goes to TRUE. The R (Reset) input resets CV to 0 and Q to FALSE.
When to use it
Used to count parts on a conveyor, machine cycles, or the number of times an alarm has occurred. Ideal for any process where you need to reach a numerical target to trigger an action.
Pro Tips
Counting stops at the maximum value of the data type used (Int, DInt, etc.). CV does not automatically 'loop' back to zero without a Reset.
The R input has priority: as long as R is TRUE, the counter stays at 0 and ignores edges on CU.
In SCL: myCTU(CU := trigger, R := reset, PV := 10); IF myCTU.Q THEN ... END_IF;
CTD — Count Down
What it does
Counts down from an initial value. Each time there is a rising edge on the CD input, the current value CV decreases by 1. When CV is less than or equal to 0, the Q output goes to TRUE. The LD (Load) input loads the preset value PV into CV.
When to use it
Useful for managing stock (subtracting parts), process countdowns, or monitoring the remaining capacity of a tank or magazine.
Pro Tips
The LD input is used to 'reload' the counter. As long as LD is TRUE, CV is equal to PV.
Unlike CTU, the Q output condition is met when CV reaches 0 (or less).
The counter stops at the minimum value of the data type (e.g., -32768 for an Int).
CTUD — Bidirectional Counter (Up/Down)
What it does
Combines CTU and CTD functions into a single block. It has two counting inputs (CU to increment, CD to decrement) and two outputs (QU for 'High count reached', QD for 'Zero reached'). It has both a Reset (R) input and a Load (LD) input.
When to use it
Perfect for simple position tracking (forward/backward), parking management (vehicle entry/exit), or dynamic inventory tracking where items are added and removed.
Pro Tips
QU goes to TRUE if CV >= PV. QD goes to TRUE if CV <= 0.
R (Reset) resets CV to 0. LD (Load) initializes CV to PV. R has priority over LD.
Very useful for automated warehouses: increment when a package enters, decrement when it leaves.
Legacy Instructions and Coils
S5 Counters and coil-type instructions for LAD/FBD
S_CU / S_CD / S_CUD
What it does
Old counting instructions (Legacy S5) used on S7-300/400. They use global counter numbers (C0...C255) instead of instance DBs. They allow you to parameterize, count, and reset in a single complex instruction.
When to use it
Only for maintenance of existing programs or migration of S5/S7-300 projects to S7-1500. Avoid for new developments.
Pro Tips
These counters are not 'typed' like IEC counters. Their range is limited (0-999).
Their behavior in response to rising edges may differ from IEC standards.
Systematically replace them with IEC CTU/CTD when refactoring for better portability.
—(SC)— Set Counter
—(SC)—What it does
Coil-type instruction that loads a preset value into a specified counter when the RLO transitions to TRUE. This is the graphical equivalent of the Load (LD) input.
When to use it
In LAD/FBD to force a value to be loaded into a counter from a separate network.
Pro Tips
Mainly used with legacy counters.
The loaded value is often defined by a constant or an integer variable.
For IEC counters, use the block's LD input instead or access the instance directly.
—(CU) / —(CD)— Counter Coils
—(CU)—What it does
Coil-type instructions that increment (CU) or decrement (CD) a counter at each rising edge of the RLO. They allow you to drive a counter without using the full functional block in the network.
When to use it
For very compact LAD programming where you just want to trigger a count at the end of a line.
Pro Tips
These coils reference a specific counter (e.g., C1 or an IEC counter DB).
Beware of readability: it is harder to see the current value (CV) with coils than with blocks.
Prefer FB blocks for better code self-documentation.
Note on S5 Compatibility
Global Counters
S5 counters (S_CU, S_CD, S_CUD) are limited to 256 global instances and a range of 0 to 999. IEC counters (CTU, CTD, CTUD) are preferable because they use instance DBs, support wider data types (LInt), and have no fixed number limit.
Counter Comparison — Which one to use?
Choose the right counter for your industrial application
| You need... | Use this counter | Example |
|---|---|---|
| Count increasing events | CTU | Number of finished products packed |
| Count down to zero | CTD | Number of shelf spaces remaining |
| Track bidirectional flow | CTUD | Number of vehicles in a parking lot |
| Load a target value | PV / LD | Set a batch of 50 pieces via HMI |
| Automatic reset | Reset (R) | End of batch, ready for the next one |
| Manage millions of units | IEC (DInt/LInt) | Annual production totalizing counter |
Frequently Asked Questions
What is the difference between an IEC counter and an S5 counter?
IEC counters use instance Data Blocks (DBs), allowing for an infinite number of them (limited by memory). They support 16, 32, or 64-bit integers. S5 counters are limited to 256 global numbers (C0-C255) and only count up to 999.
How can I make the counter restart from zero automatically?
You can connect the counter's Q output to its own R (Reset) input, often via a reset coil or a rising edge. This way, as soon as the target is reached, the counter is instantly cleared.
Can I use a counter to measure speed?
A counter alone does not measure speed. You must count pulses for a fixed duration (time window managed by a Timer) and then perform the calculation: Speed = Pulses / Time.
Do counters lose their value when the PLC stops?
As with Timers, the current value (CV) is stored in the instance DB. If the DB is configured as 'Retentive', the counter will regain its value after a restart or power failure.