PLC Programming

Counters in TIA Portal - How to Use CTU, CTD and CTUD in Siemens PLCs

Simon Adamek 10 min read Updated

In Siemens S7-1200 and S7-1500 PLCs, counters play a crucial role. They enable counting of events, pulses, and operations in control programs, making them essential for product counting, cycle tracking, batching, and position monitoring.
This article describes the three main types of counters used in TIA Portal: CTU (Count Up), CTD (Count Down), and CTUD (Count Up/Down).
All examples refer to S7-1200 and S7-1500 PLCs, although the counter logic remains identical for both families.

General Structure and Parameters of Counters

Counters in TIA Portal are implemented as IEC_COUNTER structures, which are stored inside data blocks (DBs).
Each counter needs its own instance of this structure (an instance DB, a multi-instance in an FB or a tag in a global DB), which allows counters to operate independently.

The CTU, CTD, and CTUD instructions share a common set of parameters. The data type of PV and CV is selected when the instruction is inserted - most commonly Int, but DInt, UInt, UDInt, SInt, USInt, LInt, or ULInt can be used depending on the expected count range.

  • CU - Count Up input (Bool). A rising edge (0→1) at CU increments the current value CV by 1. Available in CTU and CTUD.
  • CD - Count Down input (Bool). A rising edge (0→1) at CD decrements CV by 1. Available in CTD and CTUD.
  • R - Reset input (Bool). A signal of 1 resets CV to 0 and clears the output Q. Available in CTU and CTUD. R has the highest priority - while R = 1, counting is blocked.
  • LD - Load input (Bool). A signal of 1 loads the preset value PV into CV. Available in CTD and CTUD. Used to preload the counter to a starting value before a countdown.
  • PV - Preset Value. Defines the target value the counter compares CV against. For CTU, Q switches to 1 when CV ≥ PV. For CTD, PV is the starting value loaded by LD.
  • Q - Logical output (Bool), used in CTU and CTD. Indicates whether the counting target has been reached. Can be used in further logic or for visualization purposes.
  • QU - Up counter status (Bool), available only in CTUD. QU = 1 when CV ≥ PV.
  • QD - Down counter status (Bool), available only in CTUD. QD = 1 when CV ≤ 0.
  • CV - Current Value. Shows the actual count. Can be read in program logic, archived, or displayed on HMI.

⚙️ Each IEC_COUNTER structure should be assigned to only one counter instruction to avoid unwanted interactions between CU, CD, R, and LD signals.

When programming in LAD, counter instructions should be placed at the end of the network, and it is recommended to use tag outputs (Q, QU, QD, CV) instead of directly accessing DB variables. Remember that CU and CD react to rising edges only - if you need to count a continuous level, combine the counter with an edge detector (see our guide on R_TRIG and F_TRIG edge detection).

How the CTU Counter in TIA Portal Works - Count Up

Principle of Operation

CTU is the most commonly used counter in PLC programming. The current value CV is incremented by 1 on every rising edge of the input signal CU (transition from 0 to 1).

CV keeps increasing with each rising edge until it reaches the upper limit of its data type (for Int, this is 32 767). Once CV ≥ PV, output Q switches to 1. Q remains at 1 as long as CV is greater than or equal to PV, regardless of whether CU keeps pulsing.

If R is set to 1, CV is immediately reset to 0 and Q goes low. While R = 1, the counter ignores rising edges on CU - reset has absolute priority.

https://support.industry.siemens.com/

Typical Applications

Count-up counting is useful when you need to track the number of events or completed operations, for example:

  • Counting products on a conveyor line
  • Counting machine cycles completed
  • Tracking operations until scheduled maintenance (e.g. every 10 000 cycles)
  • Counting rejected parts or alarms over a shift
  • Triggering an action after a defined number of events (bundling, packaging)

How the CTD Counter in TIA Portal Works - Count Down

Principle of Operation

The CTD counter works opposite to CTU. Before counting starts, the preset value PV is loaded into CV using input LD - a signal of 1 at LD copies PV into CV in a single scan.

After loading, each rising edge at input CD decrements CV by 1. When CV reaches 0 or goes below, output Q switches to 1. Q stays at 1 as long as CV ≤ 0.

Unlike CTU, the CTD counter has no reset input. To restart a countdown, simply pulse LD = 1 again - this reloads PV into CV and clears Q.

https://support.industry.siemens.com/

Typical Applications

The CTD counter is used when a process has to run for a defined number of steps and then signal completion, such as:

  • Counting down the remaining parts in a production batch
  • Decrementing a ticket or token pool
  • Countdown to an alarm or maintenance notification
  • Step-by-step machine sequences where each step decrements the remaining count
  • Dispensing a preset number of items and stopping automatically

How the CTUD Counter in TIA Portal Works - Count Up/Down

Principle of Operation

The CTUD counter combines the behavior of CTU and CTD in a single instruction. CV is incremented on a rising edge at CU, decremented on a rising edge at CD, reset to 0 when R = 1, and loaded with PV when LD = 1.

CTUD exposes two independent status outputs:

  • QU = 1 when CV ≥ PV (up limit reached)
  • QD = 1 when CV ≤ 0 (down limit reached)

Priority between inputs is fixed: R has the highest priority, followed by LD, then the count inputs. If both CU and CD get a rising edge in the same scan, CV remains unchanged.

https://support.industry.siemens.com/

Typical Applications

CTUD counters are ideal wherever you need bidirectional counting - tracking a quantity that can both grow and shrink - for example:

  • Inventory or buffer tracking (parts in, parts out)
  • Position counting on linear and rotary axes
  • Parking lot or warehouse occupancy monitoring
  • Counting items between two stations in a production line
  • People counting at entry/exit points

Typical Problems and Best Practices

When implementing counters in TIA Portal, a few practical issues come up often:

  • Missing edges - CU and CD count only rising edges. If the driving signal is noisy or bouncy, use a debounce filter or an edge detector (R_TRIG) before the counter input.
  • Overflow on Int counters - the default Int range is −32 768…32 767. For high-speed or long-running counters, switch PV/CV to DInt or UDInt.
  • Counter not resetting - check if R is truly transitioning to 1. Reset has priority, but if R stays at 0 due to logic errors, the counter will keep counting.
  • Shared DB between instances - never point two counter calls to the same instance DB. Each counter needs its own IEC_COUNTER instance.
  • Retentivity after restart - by default, CV is not retained after a power cycle. If you need the count to survive restarts (e.g. total operating cycles), mark the DB variables as retentive in the DB properties.

Counter example in SCL - counting parts on a conveyor

Counters are not limited to ladder logic. In SCL you declare the counter as a static variable of a function block (FB) and call it like any other instance. This practical example counts parts detected by a sensor and signals when a batch is complete:

FUNCTION_BLOCK "FB_PartCounter"
VAR_INPUT
  Sensor : Bool;       // photoelectric sensor on the conveyor
  ResetCount : Bool;
  BatchSize : Int;
END_VAR
VAR_OUTPUT
  BatchDone : Bool;
  PartCount : Int;
END_VAR
VAR
  PartCtu : IEC_COUNTER;   // multi-instance up counter (Int)
END_VAR
BEGIN
  #PartCtu(CU := #Sensor, R := #ResetCount, PV := #BatchSize);
  #BatchDone := #PartCtu.Q;
  #PartCount := #PartCtu.CV;
END_FUNCTION_BLOCK

Because the counter is a multi-instance, every instance of "FB_PartCounter" has its own counter, so you can reuse the block on several conveyors. IEC_COUNTER is the instance data type for an Int counter; choose the DInt variant (IEC_DCOUNTER) if the count can exceed 32 767.

Counting with R_TRIG instead of a counter instruction

For a simple counter you can also build the logic yourself. The R_TRIG function block creates a one-shot pulse for one scan when the signal changes from 0 to 1, and the program adds 1 to an integer:

VAR
  EdgeSensor : R_TRIG;
  Count : Int;
END_VAR

#EdgeSensor(CLK := #Sensor);
IF #EdgeSensor.Q THEN
  #Count := #Count + 1;
END_IF;

This works, but the IEC counter is usually the better choice because it already contains edge detection, preset value comparison and reset logic. For more about edge detection, see R_TRIG and F_TRIG in CODESYS; the logic is the same in TIA Portal.

Testing and troubleshooting counters in PLCSIM

Before you use a new counter on a live machine, test it in simulation. This short tutorial explains the workflow in Siemens TIA Portal:

  • Prerequisites - a project with an S7-1200 or S7-1500 CPU, PLC tags for the inputs (for example a sensor at address %I0.0) and PLCSIM installed.
  • Configure the counter - insert CTU in the Main OB (OB1), confirm the Call options dialog to create the instance DB, and connect CU, R and PV.
  • Download and simulate - compile the program, download it to PLCSIM and set the CPU to RUN.
  • Verification - in a watch table, modify the sensor input from 0 to 1 and back. The current count CV should increase by exactly 1 per transition, and Q should switch to 1 when CV reaches PV.
  • Reset test - set R to 1 and check that CV returns to 0 on the next scan.

If the counter in Siemens PLCs misses pulses, remember that a standard counter is evaluated once per scan cycle. Pulses that are shorter than the cycle time are not seen in real-time by the program. For fast encoders or flow meters, use the high-speed counters (HSC) built into the S7-1200 CPU, described in our article on digital inputs of the Siemens S7-1200 PLC. Also note that the S7-1200 does not support STL, so STL counter examples from older STEP 7 projects only apply to the S7-1500 and S7-300/400.

Summary

Counters in Siemens PLCs are essential tools for event-based logic.
Understanding the behavior of CTU, CTD, and CTUD allows you to build reliable product counting, batching, position tracking, and inventory monitoring in TIA Portal. Combined with timers (see our guide on Timers in TIA Portal) and edge detectors, counters form the core of almost every industrial sequence.

FAQ

What is the difference between CTU, CTD, and CTUD in TIA Portal?

CTU counts up on each rising edge of CU. CTD counts down from a preset value loaded via LD, on each rising edge of CD. CTUD combines both - it can count up, count down, reset, and load PV in a single instruction, with two outputs QU and QD.

Do counters in TIA Portal count levels or edges?

Counters in TIA Portal react to rising edges only at CU and CD inputs. A steady 1 at the input will not increment or decrement the counter - only a 0→1 transition does.

What is the IEC_COUNTER data block?

IEC_COUNTER is the instance structure that stores a counter's internal state (CV, Q, QU, QD, status bits). Every CTU, CTD, or CTUD call requires its own IEC_COUNTER instance DB so that counters do not interfere with each other.

What data types can I use for PV and CV?

TIA Portal supports Int, DInt, UInt, UDInt, SInt, USInt, LInt, and ULInt for PV and CV. Choose the type based on the expected count range - Int is fine for typical applications, DInt or UDInt for high-speed or long-running counters.

How do I reset a CTD counter without a reset input?

CTD has no R input. To "reset" it, pulse LD = 1, which reloads PV into CV and drives Q back to 0. This effectively starts a new countdown from the preset value.

If you want to learn more about PLC programming and counters in TIA Portal, check out our Premium Siemens S7-1200/1500 Programming course with certificate and teacher's support.

Online courses

Want to program PLCs in practice?

Siemens, CODESYS, communication and safety courses with a certificate and instructor support. Start with a free mini-course.

See courses
Select the fields to be shown. Others will be hidden. Drag and drop to rearrange the order.
  • Image
  • SKU
  • Rating
  • Price
  • Stock
  • Availability
  • Add to cart
  • Description
  • Content
  • Weight
  • Dimensions
  • Additional information
Click outside to hide the comparison bar
Compare