Using Counters in S7 GRAPH Actions in TIA Portal V17

David Krause11 min read
SiemensTIA PortalTutorial / How-to
Licensed PE Working through this on a live machine? A Maine-licensed engineer can take it from here — included with IMD hardware, by the hour for everything else. Book an engineer

Using Counters in S7 GRAPH Actions in TIA Portal V17

S7-GRAPH is the Siemens programming language for sequential control on S7-300, S7-400, S7-1200, and S7-1500 CPUs. It separates the control flow (steps and transitions) from the actions executed in each step. Counting parts, tracking cycles, counting process events, and supervising time-quantity relationships are common requirements, and S7-GRAPH supports them natively through counter operations that are bound to events inside an action. This reference covers the supported counter operations, the COUNTER parameter type, IEC counter integration, common compile/runtime errors, and verified configuration paths in TIA Portal V17 (and V18/V19/V20, where the syntax is unchanged).

1. Prerequisites

  • TIA Portal V17 Update 4 or later (V18, V19, and V20 share the same GRAPH counter syntax). Refer to the Siemens Industry Online Support entry for the SIMATIC S7-1500/ET 200 release notes.
  • S7-GRAPH option package installed and licensed for the engineering station. Without this package the GRAPH FB type does not appear in the project tree.
  • An S7-300, S7-400, S7-1200, or S7-1500 CPU whose firmware supports GRAPH actions with counter events. S7-1200 requires firmware V4.0 or higher; S7-1500 requires V1.5 or higher for full event syntax.
  • For S7-1500: a CPU 1500 series with the optional GRAPHSYS block available in the program (it is supplied automatically by the GRAPH compiler).
Important: Counter operations inside GRAPH actions are compiled into the _GLOBAL_DB (or in S7-1500, into the instance DB of the GRAPH FB). You do not need to declare them manually unless you want to monitor them by HMI.

2. Counter Types Available in an S7 System

Two distinct counter families exist in STEP 7 / TIA Portal, and they are not interchangeable in GRAPH:

Table 1 — Counter families in STEP 7
Counter family Data type / block Address space Usage in GRAPH
SIMATIC counter COUNTER (16-bit BCD legacy) / S7-1500 IEC_COUNTER variants C0 ... C255 (S7-300/400), HW-counter words on 1500 Direct via parameter type COUNTER in the GRAPH FB interface
IEC counter (FB-based) CTU, CTD, CTUD (multi-instance capable) Instance DB / multi-instance Called inside an action as an FB call (SCL/ST/FBD), or wired via a tag of type CTU/CTD/CTUD

Legacy COUNTER addresses are tied to the PLC's system counter area and are incremented/decremented by GRAPH counter operations. IEC counters are self-contained function blocks you can call from any language editor (LAD, FBD, SCL, GRAPH). Both can coexist in the same GRAPH FB; the choice depends on whether the rest of the project uses the legacy counter area or the IEC counter concept.

3. Counter Operations Supported in GRAPH Actions

According to the official Siemens TIA Portal V20 documentation — Counter operations (S7-300, S7-400, S7-1500), the following counter operations are valid inside an action:

Table 2 — GRAPH counter operations
Operation Symbol Description Argument
Count up CU Increments the addressed counter by 1 on every event Counter number (C#) or COUNTER tag
Count down CD Decrements the addressed counter by 1 on every event Counter number (C#) or COUNTER tag
Counter reset CR Resets the counter to 0 Counter number or COUNTER tag
Counter set CS Loads the counter with the preset value of the parameter Counter number, preset value, COUNTER tag

All four operations require an event in the action qualifier. The event defines when the operation fires. Without an event, the action produces a compile error: "Event missing for counter operation".

3.1 Event qualifiers used with counter operations

Table 3 — Event qualifiers compatible with counter operations
Event Meaning Typical use
S1 Step becomes active Initialize counter on entry
S0 Step becomes inactive Reset/latch on exit
L0 Interlock falls (1 → 0) Stop counting on a fault
L1 Interlock rises (0 → 1) Resume counting once interlock returns
S1 + L1 (combination) Both required Step is active AND interlock is high

4. Declaring the COUNTER Parameter in the GRAPH FB Interface

Before a counter operation can be edited in the action, the GRAPH FB must expose a parameter of type COUNTER. In TIA Portal:

  1. Open the GRAPH FB in the project tree.
  2. Right-click the FB block title → Properties → Interface.
  3. Switch to the Input, Output, or InOut section as required by the sequence.
  4. Add a new parameter; set the data type to COUNTER (not IEC_COUNTER, not CTU, not INT).
  5. Confirm. The new parameter appears in the action editor's tag selector.

The COUNTER parameter type accepts:

  • An absolute counter address in the form C1, C2, ... up to C255 on S7-300/400, and the equivalent IEC counter tags on S7-1500 that have been declared in the PLC tag table as Counter data type.
  • A PLC tag defined in the PLC tag table with data type COUNTER or IEC_COUNTER (S7-1500 specific).
Common error: attempting to use a tag of type CTU, CTD, or CTUD directly in the action editor produces the compile error: "Data type Counter is not allowed here". The remedy is either (a) change the tag type to COUNTER, or (b) call the IEC counter as a multi-instance inside the action instead of wiring it to a counter operation.

5. Configuration Walk-Through: Count-Up in a GRAPH Action

The following procedure reproduces the canonical pattern shown in the Siemens TIA Portal V20 documentation for counter operations and works identically in V17.

5.1 Prerequisites in the project

  • A GRAPH FB named FB_Sequence with at least two sequential steps.
  • A COUNTER parameter PartCounter declared in the FB interface (see Section 4).
  • An input bit PartDetected in the FB interface used as the event trigger.

5.2 Step 1 — Declare the COUNTER parameter

  1. Open FB_Sequence.
  2. In the FB interface, add a parameter cnt_ProducedParts : COUNTER.
  3. Save and compile the FB.

5.3 Step 2 — Add a counter operation in the action

  1. Select the step where the count must increment (e.g., Step_3_Running).
  2. In the Actions table of the step, choose an empty row.
  3. Click the Event cell and select an event — for a one-shot on step entry, select S1. To count on every rising edge of a process signal within the step, use L1 or a custom event expression.
  4. Click the Action cell on the same row. The action editor opens.
  5. Type the counter operation, e.g.:
    CU  C1
    or, when using the FB parameter:
    CU  cnt_ProducedParts
  6. Press Apply. The action editor closes, and the step is marked with a counter symbol.

5.4 Step 3 — Optional: reset on step exit

Add a second action row in the same step or in the next step with event S0 and the CR operation:

CR  cnt_ProducedParts

This forces the counter to 0 the moment the step deactivates.

5.5 Step 4 — Compile and download

  1. Right-click the GRAPH FB → Compile > Software (only).
  2. Resolve any Data type Counter is not allowed here warnings — they are usually caused by an IEC counter tag being wired to a counter operation; switch the tag to COUNTER or to an absolute C address.
  3. Download the program to the CPU. After a STOP → RUN transition, the GRAPH sequencer is ready.

6. Integrating an IEC Counter (CTU/CTD/CTUD) Inside GRAPH

Many modern code bases avoid the legacy COUNTER area and rely entirely on IEC counters. GRAPH supports them, but the integration model is different: IEC counters are called in the action body rather than triggered by a counter operation.

6.1 Single-instance IEC counter

  1. Add a tag of type CTU, CTD, or CTUD in the FB interface (e.g., iec_ProducedParts : CTU).
  2. Open the action cell, switch the language to SCL if you need full expression access (right-click action body → Change language).
  3. Insert a call to the IEC counter:
    iec_ProducedParts(CU := PartDetected, R := ResetCounter, PV := 1000, Q => PartsReached, CV => CurrentCount);
  4. Bind the event in the action qualifier (e.g., L1) — the event determines when the call is executed, not when the internal CU edge of the FB is generated.

6.2 Multi-instance IEC counter (recommended)

  1. Inside the GRAPH FB, add a static variable Multi_Counter : CTU in the Static section of the FB interface.
  2. In the action, call:
    #Multi_Counter(CU := SensorEdge, R := Reset, PV := 500, Q => #Done, CV => #Actual);

Multi-instance IEC counters keep the counter encapsulated inside the GRAPH FB's instance DB, eliminating name conflicts across FB copies.

Why you cannot mix the two: A GRAPH counter operation (CU/CD/CR/CS) generates a CU, CD, FR, or FR instruction on the legacy counter area. An IEC counter (CTU/CTD) is a function block that you call by name. Wiring an IEC counter to a counter operation will not work; the compiler rejects it because the data type signatures differ.

7. Common Compile and Runtime Errors

Table 4 — Diagnostic matrix for counter issues in GRAPH
Symptom Likely cause Resolution
"Data type Counter is not allowed here" An IEC counter tag (CTU/CTD) is used as the operand of a counter operation Change tag type to COUNTER or call the IEC counter as a multi-instance in SCL
"Event missing for counter operation" Action row has CU/CD/CR/CS but no event qualifier Add an event (S1, S0, L0, L1, or a combination)
"Unknown operand Cxxx" Counter number exceeds the CPU's range (e.g., C300 on a CPU 1511) Use a counter number inside the supported range; S7-1500 supports 0..2047 in the PLC tag table
Counter increments continuously while step is active Event S1 re-fires every cycle because it is not edge-detected Switch event to L1 (rising interlock) or use a custom signal that pulses on the event to count
Counter does not increment on PLC Action qualifier is locked or supervising step is not active Verify the supervising step is active in the GRAPH sequencer; check the SW_GRAPH_STATE word
CPU goes to STOP with SF LED + "Counter range exceeded" BCD counter overflow (16-bit wrap) Use a 32-bit DINT, or set a CS preset below 999, or migrate to IEC counter (CTU)

8. Counter vs. IEC Counter — Selection Guide

Table 5 — When to use which counter family in GRAPH
Criterion COUNTER (legacy) IEC counter (CTU/CTD/CTUD)
Range 0 to 999 (BCD) on S7-300/400; 0 to 65535 on S7-1500 IEC_COUNTER 0 to 32767 (INT) or 0 to 2,147,483,647 (DINT)
Multi-instance No (counter number is global) Yes, encapsulated in the FB's instance DB
Reusability across FB copies Limited — multiple FBs must use distinct C addresses Excellent — each FB instance gets its own multi-instance
HMI visibility Direct via C address in HMI tag DB Indirect via the GRAPH instance DB
Compliance with PLCopen style No (Siemens-proprietary) Yes
Recommended for new projects No Yes

9. Verifying Counter Behavior Online

  1. Download the program and switch the CPU to RUN.
  2. Open the GRAPH FB in Online > Monitor.
  3. Force the supervising step to active by setting the transition condition.
  4. Watch the COUNTER value in the Sequence view; for IEC counters, add a watch table on the GRAPH instance DB and observe the CV field.
  5. Trigger the count event (a process signal or a forced input). Confirm that the value increments on each event edge.
  6. To verify the reset path, deactivate the step (set the transition true) and check that CR brings the value to 0.
Tip: For a smoke test without a real process, drive a 1 Hz pulse generator (e.g., a clock memory bit) into the counter event and confirm the value rises by 1 each cycle while the step remains active.

10. Migration Notes: S7-300/400 to S7-1500

If a legacy GRAPH FB is migrated from S7-300/400 to S7-1500 in TIA Portal V17, the migration tool will convert COUNTER declarations to IEC counter tags. The counter operations remain valid, but verify the following:

  • The number of available system counters on the S7-1500 (default 2048 IEC counters).
  • That no HMI tag references the old C address with an offset that no longer matches.
  • That programs that assumed BCD behavior (values up to 999) are updated if a wider range is now in use.

11. Field-Proven Tips

  • Prefer IEC counters for any new GRAPH FB. The COUNTER parameter type is kept for compatibility with migrated code.
  • When using the legacy COUNTER area, allocate a dedicated address range per GRAPH FB to avoid overlap with other FBs.
  • Use a comment column in the action table to record the meaning of the counter — GRAPH actions are not searchable by symbol name.
  • If the action body needs complex math around the count, switch the action language to SCL and call a CTUD instance directly.
  • For safety-related sequencers, route counter events through the interlock and use L1 / L0 events so that the counter reacts to process state changes, not step activation.

12. FAQ

Why does GRAPH show "Data type Counter is not allowed here" when I wire a CTU tag?

The COUNTER parameter type in GRAPH expects a legacy COUNTER or an absolute C address, not an IEC counter instance. Either change the tag's data type to COUNTER, or call the IEC counter as a multi-instance inside an SCL action body using #CounterName(CU := ..., R := ..., PV := ...).

How do I use Count Up (CU) and Count Down (CD) in the same step?

Add two action rows: one with the S1 event and the CU operation, another with the L1 event (or a different event) and the CD operation. Both can target the same COUNTER parameter; CU and CD on the same counter cancel each other if the value is 0 or the preset is reached.

Do I need a separate event for every counter operation?

Yes. Every counter operation (CU, CD, CR, CS) must be qualified by an event in the action. The event determines when the operation is executed. Without an event, the compiler reports "Event missing for counter operation".

Can I use GRAPH counter operations on an S7-1200?

Yes, S7-1200 firmware V4.0 or higher supports S7-GRAPH and the same counter operations as the S7-1500, with the same COUNTER parameter type. Refer to the S7-GRAPH manual for S7-1200/1500 in the Siemens Industry Online Support portal.

How do I reset a COUNTER when the step deactivates?

Add an action row with event S0 and the CR (counter reset) operation targeting the same counter parameter. The counter returns to 0 the moment the step becomes inactive, regardless of its previous value.

Back to blog