Implementing Wrap-Around Up/Down Counter Logic on Siemens LOGO!

David Krause13 min read
HMI ProgrammingSiemensTutorial / 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

1. Problem Definition: The Non-Wrapping Counter

On a Siemens LOGO! 8 base module paired with a LOGO! TD text display, the user-visible behavior of the integrated Up/Down counter block (function block range B001–B010 in LOGO! Soft Comfort) is asymmetric. Incrementing from 0 toward the upper threshold (4) cleanly rolls back to 0 the instant the count exceeds the On threshold, producing the desired sequence:

0 → 1 → 2 → 3 → 4 → 0 → 1 → 2 → 3 → 4 …

Decrementing, however, stops at 0 instead of wrapping to 4:

4 → 3 → 2 → 1 → 0 [STOP]

The required round-robin behavior for menu navigation (4 text screens, F-key scrolling on the LOGO! TD) is:

4 → 3 → 2 → 1 → 0 → 4 → 3 → 2 → 1 → 0 …

This is a logical wrap-around, not a counter fault. The standard counter block saturates at the lower threshold (On/Off = 0/0); it does not roll over. The fix is to add external logic that recognizes the lower-bound transition and pulses the count input in the opposite direction, or replace the counter with a shift-register-based ring.

Note: The same wrap-around deficiency appears on Up/Down counters, Up counters, and Hour counters in LOGO! Soft Comfort V8.0 through V8.4. None of these primitives implement a modulus operator natively. You must build modulus behavior with combinational logic.

2. Hardware and Software Prerequisites

Item Specification
Base module LOGO! 8.3 or 8.4 (e.g. 6ED1052-1MD08-0BA2, 12/24 RCE)
Text display LOGO! TD (6ED1055-4MH08-0BA1) with 4 cursor keys + 4 function keys
Programming software LOGO! Soft Comfort V8.3 or later
Firmware on base module FS:04 or higher (LOGO! 8.3 / 8.4)
Connection TD → base LOGO! TD cable or Ethernet (LOGO! 8.3+ with TD Ethernet variant)
Display messages 4 message texts configured in Soft Comfort (Message Configuration)

Verify the firmware version on the LOGO! base module by selecting ESC → Diagnostics → Software on the unit or by reading the device in Online Test mode in Soft Comfort. The current LOGO! 8 generation with Web Server (8.3 / 8.4) provides 400 function blocks; legacy 8.0/8.1 firmware caps at 200.

3. LOGO! Counter Block Reference

The Up/Down counter block in LOGO! Soft Comfort has the following signal interface:

Pin Function Behavior
R Reset Sets the current count (C5 internal) to zero on a rising edge
Cnt Count input Rising edge increments or decrements C5
Dir Direction 0 = count down, 1 = count up
Q Output 1 when C5 ≥ On threshold, else 0

Parameter set on the block (double-click in FBD):

  • On threshold — value at which Q goes high (default 1000)
  • Off threshold — value at which Q returns to low
  • Count direction at Cnt = 1 (Dir = 0) — down, at Cnt = 1 (Dir = 1) — up
  • Remanence — on/off, retains C5 across power cycles

The default ramp from 0 to 4 and back is monotonic; reaching the Off threshold of 0 with Dir = 0 simply parks the counter. The block does not roll over from 0 to the On threshold. This is by design, not a bug.

4. Root Cause Analysis

Mathematically, the desired behavior is:

C5_next = (C5 + Dir) mod (N + 1)

where N = 4, and Dir ∈ {0, 1}. The Up/Down counter block implements:

C5_next = clamp(C5 + (2·Dir − 1), 0, N)

clamping at 0. The clamp is the wrap-around deficit. To restore modulus arithmetic, three things must occur when a 0-down pulse arrives:

  1. Detect that C5 == 0 and Dir == 0 (a downward step at the lower bound).
  2. Suppress the natural 0→-1 transition.
  3. Generate a forced C5 = N (4) on the next count cycle.
Field note: Some legacy application notes from Siemens suggested a shift register as the cleanest solution, and the same pattern is documented in Siemens' own LOGO! 8 application examples (entry 64143308 in the Siemens Industry Online Support). For 4 or 8 steps, the shift-register approach is shorter, more scan-deterministic, and easier to debug than comparator logic.

5. Solution 1 — Threshold Trigger with Comparator (Classic)

This solution uses the Analog threshold trigger block plus an OR gate and a one-shot pulse. It keeps the standard Up/Down counter and adds wrap logic externally.

Building blocks (FBD in Soft Comfort):

  1. Up/Down counter B001 with On/Off = 4/0, Dir = F1, Cnt = Up pulse, R = Reset.
  2. Analog comparator B002: A = C5 of B001, threshold = 0, hysteresis = 0 → output 1 when C5 == 0.
  3. NOT gate B003 on B002 output (to get "exactly 0" high).
  4. Edge-triggered pulse flag from F1 (F1 → B004 edge, rising → pulse).
  5. AND B005: (F1 pressed) AND (B003 = 1) → "we just tried to go below 0".
  6. RS flip-flop B006: set by B005, reset by next Cnt pulse.
  7. OR B007: real count input OR B006 → B001.Cnt.
  8. RS B008: set when B006 fires, drives B001.R briefly to force C5 to 0, then re-asserts Cnt in the opposite direction.

The cleanest version uses a small OR-tree:

     F1 (UP) ─────┐
                  ├── AND ── OR ──► Cnt
     F4 (DOWN) ───┤        │
                            │
     C5 == 0 AND F4 ───────┘   (wrap from 0 to N)
     C5 == 4 AND F1 ──────────► (wrap from N to 0 — not needed for standard counter, but useful for symmetry)

Wire the message text enable input to C5 via a demultiplexer if you need discrete screen indices 0..4. LOGO! Soft Comfort's Message Text block accepts a numeric value (block number of the message) and an enable flag; the simplest approach is to drive the En input from C5 directly, since 0..4 maps 1:1 to message-text index 0..4 (offset by +1 in Soft Comfort's message editor).

6. Solution 2 — Shift Register Round-Robin (Recommended)

For a fixed number of steps (e.g. 4 menu screens), a single shift register block gives true ring-counter behavior with no boundary conditions to manage.

Concept: maintain a 1-hot bit in a shift register. Pressing F1 shifts the bit "up" (toward the LSB or MSB depending on convention); pressing F4 shifts it "down". At either end, the bit that would fall off the end is re-inserted at the opposite end, producing an automatic wrap.

Block list (LOGO! Soft Comfort V8.3+, FBD editor):

  1. B001 — Shift register with 4 stages (S1..S4). In/Out = Q1..Q4.
  2. B002 — AND: F1 ↑ edge AND S4 → bit recycling input (F1 wraps 4→0).
  3. B003 — AND: F4 ↑ edge AND S1 → bit recycling input (F4 wraps 0→4).
  4. B004 — OR: combine F1 pulse, F4 pulse, recycle pulses, and external reset.
  5. Shift direction input: Inp = OR of pulses; Dir = (F1 edge XOR F4 edge) — but for a single direction bit use a dedicated flag toggled by a separate counter. Practically: use two separate shift registers, one for each direction, and combine their outputs with a priority encoder.

Because a single shift register is unidirectional, the typical implementation is to maintain a binary position counter (the standard Up/Down counter) for screen addressing, and use a second shift register or output of the Up/Down counter for the wrap detection. The shift register's value is the actual screen index.

For the common 4-screen menu case, an even simpler pattern is:

F1 edge → set M1
F4 edge → set M2
M1 → counter B001 Cnt (up)
M2 → counter B001 Cnt (down)
Dir = 0 always, but use OR(F1) for up, OR(F4) for down
Wrap logic: comparator detects 0+down OR 4+up and forces C5 to wrap

The shift register's role in the LOGO! 8 application example catalog is to provide the "running light" effect used as a teaching reference. The wrap-around for menu navigation uses the same building blocks but with discrete steps.

7. Solution 3 — Arithmetic Modulus with Math Block

LOGO! Soft Comfort V8.3 added the Math instruction (basic math block, function block category "Special functions"). The Math block supports modulo on signed integer inputs (Cxx data type). Use:

Math block: result = (C5 + 5) MOD 5
  where MOD is implemented as:
    IF (x >= 0) THEN result = x - 5*FLOOR(x/5)
    ELSE result = x + 5*CEIL(-x/5)

Practical implementation: feed Math block with (Up_count - Down_count), and clamp to {0, 1, 2, 3, 4}. Use the output as a 0-based screen index. This approach is firmware-dependent: LOGO! 8.3 build 04+ required for full modulo; on 8.0/8.1, simulate modulo with comparison cascades.

8. Implementing Menu Navigation on LOGO! TD

The LOGO! TD presents 4 function keys (F1–F4) plus 4 cursor keys. The C1–C4 button labels used in Soft Comfort's I/O name list refer to the LOGO! base module's cursor keys (Up, Down, ESC, OK), but the same names appear in the TD's function-key mapping for many program references. Confirm in the LOGO! 8 manual chapter on the TD which F-key maps to which I/O bit.

For this application:

TD Key Soft Comfort I/O name Function
F1 I5 / C1 (Up) Increment screen index
F4 I8 / C4 (Down) Decrement screen index
F2 I6 / C2 (Down alt) Optional — decrement
F3 I7 / C3 (Up alt) Optional — increment

Create four message texts in Programming → Message Text. For each message, set:

  • En input = (C5 = N) — enabled when the running index equals the message index.
  • Acknowledgement = no.
  • Tickertape = no.
  • Line 1 / Line 2 = your screen content.

The visible behavior on the TD: pressing F1 advances one screen, pressing F4 retreats, and either direction cycles the four screens indefinitely.

9. Wiring and I/O Mapping

Terminal Signal Source
I1 External UP Pushbutton (NO) to +24 V
I2 External DOWN Pushbutton (NO) to +24 V
I3 Reset to 0 Optional hard-wired reset
Q1 Heartbeat / Run Indicator
TD F1 / F4 UP / DN On-device buttons

For LOGO! 12/24 RCE base modules, the digital inputs are bidirectional; for the 230 RCE variant, treat the I terminals as 230 V AC inputs with the same logic. The TD cable is keyed and not hot-swappable on a powered base module — disconnect mains before attaching the TD.

10. Step-by-Step Implementation in LOGO! Soft Comfort

  1. Open LOGO! Soft Comfort, create a new project, select your base module (e.g. 6ED1052-1MD08-0BA2).
  2. Drag an Up/Down counter from the Special Functions palette into the FBD canvas. Rename it B001.
  3. Double-click B001. Set On threshold = 4, Off threshold = 0, Remanence = off.
  4. Wire B001.Cnt to a pulse generator driven by F1 (use an edge-triggered pulse flag or a Boolean edge block).
  5. Add a Threshold trigger (analog) B002. A1 = C5 of B001, On = 0, Off = 0. Output = 1 when C5 == 0.
  6. Add an AND B003: inputs = B002 output, F4 pulse. Output = "wrap trigger".
  7. Add a second Threshold trigger B004: A1 = C5 of B001, On = 4, Off = 4. Output = 1 when C5 == 4.
  8. Add an AND B005: inputs = B004 output, F1 pulse. Output = "up overflow trigger".
  9. Add an OR B006: real F1, real F4, wrap trigger, up-overflow trigger. This is the final Cnt pulse to B001.
  10. Wire B001.R to a one-shot pulse from B003 AND B005 (so the counter is reset to 0 the instant the wrap occurs), with a small monostable delay to ensure the count input is re-driven on the next scan.
  11. In Programming → Message Text, create 4 message texts. Set En input of message N to (C5 of B001 = N).
  12. Compile (Ctrl+B) and transfer to the LOGO! base module.
  13. Test on the bench with the TD connected.
Watch-out: LOGO! Soft Comfort's scan cycle is 1–10 ms depending on program length. The wrap trigger must be evaluated on the same scan as the overflow comparator; otherwise the counter briefly displays 0 (or 4) before wrapping. Use the Edge-triggered pulse generator block (Bxxx with "retain" off) to ensure deterministic one-cycle pulses.

11. Verification and Commissioning Checks

  1. From the home screen, press F1 five times. The screen index should sequence 0 → 1 → 2 → 3 → 4 → 0. The TD should show the 5th screen (index 0, message text #1) without pause.
  2. Press F4 five times. The screen should sequence 4 → 3 → 2 → 1 → 0 → 4. Verify the wrap at 0 returns to 4 (this is the bug fix).
  3. Power-cycle the LOGO! base module. If remanence is enabled, the screen index persists; if disabled, it returns to 0. Validate the spec against the operator's expectation.
  4. Hold F1 for >2 seconds. The contact debounce of the TD is 30 ms; bounce is not expected. If multiple increments occur, add a 50 ms on-delay (using the On-delay block) between F1 and the Cnt input.
  5. Press F1 + F4 simultaneously. With the comparator-based solution, the counter freezes; with the modulo-Math solution, the index oscillates by ±1 per scan (configure priority encoder to handle this).
  6. Open Online Test in Soft Comfort. Verify C5 of B001 cycles 0–4 with no negative transient values. A negative value in C5 indicates a missing wrap gate.

12. Troubleshooting Matrix

Symptom Likely Cause Remedy
Counter decrements to 0 and stops Missing wrap-around logic at lower bound Add AND(F4, comparator(C5=0)) into Cnt OR-tree
Counter briefly displays 0 then jumps to 4 Comparator evaluated on scan after the count pulse Place the comparator in series with the Cnt input, not in parallel
Counter shows negative values in Online Test Threshold trigger misconfigured (Off > On) Set On = 0, Off = 0 for the lower-bound detector
TD does not update Message Text En input wired to wrong block Verify En receives C5, not the Q output of B001
F1/F4 do nothing Function-key mapping differs from Soft Comfort default Re-assign C1–C4 I/O names in project → I/O list
Single press triggers multiple counts Contact bounce or missing edge Insert edge-triggered pulse generator before Cnt
Counter wraps but message text flickers Two message texts enabled during wrap Use strict-equal comparators (On = N, Off = N+1) for each message
Safety caveat: The Up/Down counter is a non-retained volatile block by default. Loss of power returns the index to 0. If the controlled process assumes a known starting state, set Remanence = on in the block properties, or hard-wire a startup reset to a defined index.

13. Frequently Asked Questions

Why does my LOGO! up/down counter stop at zero and not wrap to 4?

The Up/Down counter block in LOGO! Soft Comfort V8.x clamps the running value between 0 and the On threshold. It does not implement modulo arithmetic. To wrap from 0 back to 4 on a downward step, add an external AND gate between the down input and a comparator that detects C5 = 0, and route that combined signal into the counter's Cnt input.

Which function key on the LOGO! TD corresponds to C1 and C4 in the program?

On the LOGO! TD (6ED1055-4MH08-0BA1), the four function keys F1–F4 map to the C1–C4 inputs in the program, in that order. C1 = F1 (Up), C2 = F2, C3 = F3, C4 = F4 (Down). You can verify the mapping under Project → I/O List in LOGO! Soft Comfort.

Can I use a shift register instead of a counter for the menu navigation?

Yes, and for a fixed small number of screens (4 or 8) it is often the cleanest solution. Wire a single shift register stage for each screen, shift on F1 edge, shift the opposite direction on F4 edge, and feed the bit that would fall off the end back into the opposite end to create a ring. For arbitrary N, retain the counter plus the comparator wrap logic.

What firmware version supports the Math (modulo) block on LOGO!?

The Math instruction block is fully supported in LOGO! 8.3 firmware (FS:04 and later) and in LOGO! Soft Comfort V8.3. On LOGO! 8.0/8.1 (FS:01–FS:03), the Math block is not present, and modulo must be simulated with comparator cascades.

How do I keep the screen index across a power cycle?

Open the Up/Down counter block's properties, enable Remanence for the C5 internal value, and recompile. After power-up the counter resumes at the last value, so the TD displays the same message text the operator was viewing before the outage.

Back to blog