Overview
The Siemens LOGO! 8 logic module ships with a Math instruction block capable of addition, subtraction, multiplication, division, and analog value processing. In continuous energy-metering and totalization applications, engineers quickly encounter a hard ceiling: the Math block evaluates signed 16-bit integers with a range of -32,768 to +32,767. The Retaining (Remanence) memory area on a LOGO! 8 (LOGO 8.FS4 and later firmware) supports persistent values only within the 0-3599 range when combined with retaining operands, so simple persistence is not enough to accumulate year-long energy totals.
This reference documents field-verified methods to cumulatively totalize a value derived from the Math block, extend the count range far beyond 32,767, and display the running total on the LOGO! onboard text display or LOGO! TD without overflow, drift, or loss of synchronization. The techniques apply to LOGO! 8 (6ED1052-xxxx08-0BA1 / -0BA2) and the LOGO! 8.3 (6ED1052-xxxx08-0BA3) variants running LOGO! Soft Comfort V8.x project files.
Prerequisites
- LOGO! 8 base module (BM) or signal board (SB) variant with digital and analog I/O sufficient for the application. For heating/flow metering, AI1-AI4 (0-10 V or PT100/PT1000) are typically used.
- LOGO! Soft Comfort V8.x or later for offline programming. FBD (Function Block Diagram) is the recommended language for totalization circuits because of the Math block's native placement.
- Firmware version LOGO! 8.FS4 or newer (6ED1052-1xx08-0BA2) for full retaining range support. Earlier FS variants are limited to 0-239 for retained values.
- Current version of the LOGO! Web-Based Management tool for live monitoring of tags during commissioning.
- An application that requires totalization of a slowly varying quantity (e.g., 0-3600 W power sampled once per minute, producing 0-60 Wh increments).
Math Instruction Block: Configuration Reference
The Math instruction block (sometimes called the "Arithmetic instruction") in LOGO! 8 has the following relevant parameters:
| Parameter | Value Range / Options | Notes |
|---|---|---|
| Operator | +, -, ×, ÷, AND, OR, XOR | Boolean operators apply bitwise to two integer operands. |
| Operand 1 (Op1) | -32,768 to +32,767 (signed) | Can be a constant, analog input, network input, or block output. |
| Operand 2 (Op2) | -32,768 to +32,767 (signed) | Same source options as Op1. |
| Priority of Op1/Op2 | 0 = Value, 1 = Reference | 0 binds the operand to a live value; 1 references the parameter field. |
| Output Range | Off / ±10,000 / ±100,000 (0.01 scale) / ±1,000,000 (0.001 scale) | Selects the analog gain and offset for the AQ output tag. |
| Behavior on En=0 | Off / Last Value / 0 / 1 | "Last Value" retains the previous output when enable is de-asserted, which is the key behavior used for running totals. |
Source: LOGO! 8 System Manual, function description of the "Math" special function. See LOGO! 8 System Manual (SIOS entry 109741041).
Step-by-Step: Basic Cumulative Totalization
The most direct technique uses the Math block's Last Value behavior combined with an edge-triggered pulse generator that adds the new sample to the previous output every cycle.
- Place the Math block in FBD with operator = +, Op1 = the new instantaneous sample (e.g., 0-3600 W from a power-calculation Math block upstream), Op2 = a feedback tag that mirrors the block's own output (M-flags or a global network input).
- Set Behavior on En=0 to "Last Value". This ensures that when the enable signal is low, the output holds the previously computed sum, which is essential to prevent the total from resetting to zero between samples.
- Insert a cycle timer (On/Off pulse generator) with a configurable period. A 60-second period (TH = TL = 30 s) is a common choice for minute-by-minute accumulation, but the technique generalizes to any period where the per-cycle increment is known.
- Add a positive-edge (P) trigger on the cycle timer output. Wire its output as the En input of the Math block. The P-trigger guarantees one addition per cycle transition, preventing multiple adds if the cycle timer has any bouncing.
-
Wire the Math block output to a Message Text configured for text display. Use the placeholder
%AQ1or block-output tag to display the integer running sum. For decimal display, enable the Output Range and use the parameter field scaled appropriately.
The Math block, configured for self-feedback with edge-triggered enable, will add the Op1 value exactly once per cycle, producing a running integer sum. With a 60-s period and a 14 W average sample, the block will increment by 14 every minute indefinitely — until it reaches 32,767, at which point the output rolls over to -32,768 and the totalization logic fails.
Working Around the 32,767 Limit
Three field-verified strategies extend the totalization range. Each has trade-offs that must be matched to the application.
Strategy 1: Asynchronous Add/Subtract with Cycle Timer
This method preserves the 16-bit Math block range but uses a sub-second cycle timer to break a large per-period addition into multiple smaller additions that are guaranteed to stay within range.
- Configure a cycle timer (B001-type On/Off pulse) with TH = 0.99 s, TL = 0.01 s, giving a 1.0 s period with 99% duty cycle.
- Use a P-trigger on the rising edge of the cycle timer to add the per-period increment (e.g., 14 W × 60 s / 60 = 14 W per minute, but if you want a faster totalization for testing, 8 impulses per cycle × 3600 W = 28,800 W per hour is the documented upper test bound).
- Insert a second, asynchronous pulse generator that fires four short impulses at t = 0.1 s, 0.3 s, 0.5 s, 0.7 s after each cycle start. These impulses subtract 3,600 W (or multiples thereof) from a downstream register whenever the running sum approaches the ceiling.
- Bound the timing: total elapsed time of all four subtractions must fit inside the 0.99 s "on" phase of the cycle timer. Reducing TH and TL to 0.05 s allows up to 8 sub-impulses per cycle, raising the maximum addable power to 8 × 3,600 W = 28,800 W per cycle before the math overflows.
Strategy 2: Counter-Based Range Extension (Recommended for Energy Meters)
For energy applications, the standard pattern uses an Up/Down counter to count units of 1 Wh (or 1 kWh) and a separate counter to count higher-order units.
- Compute the energy per cycle in Wh. For a constant 14 W load sampled once per minute, each sample represents 14 Wh / 60 = 0.233 Wh — too small to register on a 1-Wh counter. Aggregate to 1-minute periods so the per-cycle increment is a whole number of Wh.
- Set the counter's On threshold (TH) to 1,000 pulses for a kWh counter, or to 1 for a Wh counter if 999,999 counts is sufficient.
- When the Wh counter reaches 1,000, generate a single pulse that increments a kWh Up counter and resets the Wh counter. Repeat for 1,000,000 kWh → MWh.
- Display the Wh counter directly on the LOGO! text display, with a screen-priority switch to the kWh and MWh counters.
| Stage | Counter Type | Range | Reset Trigger | Display Tag |
|---|---|---|---|---|
| 1 | Up counter (Wh) | 0 to 999 | On = 1000 | %C001 |
| 2 | Up counter (kWh) | 0 to 999 | On = 1000 | %C002 |
| 3 | Up counter (MWh) | 0 to 32,767 | Manual | %C003 |
This structure effectively extends the totalization range to 32,767 MWh before the MWh counter itself saturates, which is sufficient for the vast majority of building-scale energy applications.
Strategy 3: Divide and Sum with Two Math Blocks
When the instantaneous power exceeds 32,767 W, split the value into a high word and a low word before adding.
- Use a first Math block to compute
Mod 1000approximation by computingvalue - (value/1000)*1000.LOGO! 8 has no native MOD or integer-division operator. This must be implemented using repeated subtraction with a down counter, which adds significant block count to the program. - Use a second Math block to count how many times 1000 was subtracted, representing the high word.
- Add the high and low words to dedicated counters as in Strategy 2.
This is functionally equivalent to Strategy 2 but uses the Math block for the modulo operation. It is the most block-inefficient approach and should be used only when the alternative counter configurations cannot achieve the required gate time.
Improving Measurement Accuracy
Cumulative energy errors come from three principal sources, in order of magnitude:
- Flow sensor quantization: Converting a pulse output to kg/min introduces a rounding error inversely proportional to the gate time. Use the longest practical gate time on the high-speed counter (I1-I4 on LOGO! 8 BM) to minimize this loss.
- Power calculation linearization: Multiplying the kg/min value by the (T_in - T_return) delta and the specific heat of water (4.186 kJ/(kg·K)) is sensitive to noise on the PT100/PT1000 inputs. Average the temperatures over the same gate time as the flow.
- Per-period aggregation rounding: The Math block truncates toward zero. With increments of 0.233 Wh/min, the LOGO! will round to 0 every cycle, losing all data. Always scale your period to make the increment an integer number of Wh.
Configuration of the Flow Sensor Gate Time
For pulse-output flow sensors (e.g., 1 pulse/litre), the LOGO! high-speed counter block has these relevant parameters:
| Parameter | Typical Setting for Energy Metering | Effect |
|---|---|---|
| Gate time (TH) | 10-60 s (shorter for fast dynamics) | Determines the integration window for pulse counting. |
| On threshold (On) | Pulses equivalent to 1 Wh at minimum flow | Triggers one count event per Wh or kWh. |
| Off threshold (Off) | 0 (no hysteresis) or 1 pulse | Prevents re-triggering on noise. |
| Counter direction | Up | Energy only accumulates in one direction. |
Selecting the gate time is the single largest accuracy lever. A 10 s gate at 1 Hz pulse rate yields 10 counts/Wh maximum resolution; a 60 s gate yields 60 counts/Wh. If the output of the threshold trigger is too low, accuracy suffers from rounding; if it is too high, the value deviates from the true moving average.
Display Configuration on the LOGO! TD / Onboard Display
- Open Message Text in FBD and configure the text buffer. Use the
&operator to embed block-output values:Total: %AQ1 kWh - Set the message to scroll or static depending on the number of digits. For values > 9,999, enable the bar-graph flag for better readability on the 4-line TD display.
- Use the ESC + OK key combination to clear all counters in commissioning, or wire a digital input to the "Reset" parameter of all counters in the chain.
- For long-term installations, schedule a monthly read-out using LOGO! Web-Based Management or push the counters to a higher-level SCADA via Modbus TCP from the LOGO! 8 BM.
Verification Procedure
- Apply a known constant load (e.g., a 1,000 W resistive heater) to the system.
- Start a stopwatch in parallel with the LOGO! totalization. Run for exactly 1 hour.
- Read the kWh counter at the end of the test. Expected value: 1.000 kWh ± the rounding error of one Wh (0.1%).
- Verify the program cycle time on LOGO! diagnostic view stays below the configured cycle timer TH value. If the cycle time exceeds TH, the asynchronous add/subtract logic will drift.
- Cycle power to the LOGO! and confirm the retained counters restore their last value within 0-3599 of the actual count. Counter values outside this range are NOT retained and will reset on power loss.
Troubleshooting Matrix
| Symptom | Likely Cause | Corrective Action |
|---|---|---|
| Total saturates at 32,767 | Math block 16-bit overflow | Implement Strategy 2 (counter-based range extension). |
| Total resets to 0 on power cycle | Counter setpoints exceed retaining range | Bound each counter to 0-3599; cascade to higher-order counters. |
| Total increases faster than expected | Math block adding more than once per cycle | Add a positive-edge (P) trigger between the cycle timer and the Math En input. |
| Total is constant despite load | Per-period increment rounds to 0 | Increase the aggregation period so the per-cycle Wh increment is ≥ 1. |
| Display shows ---- or overflow indicator | Output Range parameter does not match value | Set Output Range to Off for raw integer display, or rescale using gain/offset. |
| Drift accumulates over days | Asynchronous sub-impulses exceeding cycle timer high phase | Reduce TH and TL to 0.05 s; verify program cycle time stays below 0.99 s. |
| Last Value not held when En=0 | Behavior on En=0 set to Off/0/1 instead of Last Value | In the Math block properties, set the retentive output behavior explicitly to Last Value. |
Boundary Conditions and Limits
- Maximum per-cycle addition without overflow: 32,767 (Math block signed 16-bit limit).
- Maximum retained counter value: 3,599 (LOGO! 8 with firmware FS4 and later).
- Maximum counter chain range: 3,599 × N stages; for energy metering in kWh, three stages (Wh, kWh, MWh) yield a practical range of 3,599 MWh before upper saturation.
- Maximum number of Math blocks per program: 200 (LOGO! Soft Comfort V8.x, BM 12/24 RCE or higher).
- Maximum number of Up/Down counters per program: 24 (LOGO! 8 BM with sufficient program memory).
- Program cycle time upper bound: 0.99 s in the asynchronous add/subtract strategy; longer cycles require a longer cycle timer TH.
Best Practices
- Always aggregate your measurement to a per-period integer of Wh before applying the counter threshold. Sub-Wh granularity in the LOGO! 8 Math block is unsupported and will produce drift.
- For accuracy-critical applications, use the longest practical gate time on the high-speed counter and verify the gate time against the slowest expected flow rate.
- Cascade counters in powers of 10 (Wh → kWh → MWh) to keep the per-stage counter value within the retaining range.
- Display the current per-period value on one screen and the lifetime total on a second screen; engineers in the field need both for commissioning and verification.
- If the totalization must survive power loss indefinitely, integrate the LOGO! counters into a higher-level PLC or SCADA via Modbus TCP; the LOGO! 8 BM supports Modbus TCP server on port 502 (configurable in Tools → Ethernet Connections).
What is the maximum value the LOGO! 8 Math instruction block can compute?
The Math block in LOGO! 8 (firmware 8.FS4 and later) operates on signed 16-bit integers, giving a hard limit of -32,768 to +32,767. The "Retain" option for a Math block parameter covers only the 0-3,599 subrange, so persistent accumulation beyond 3,599 requires a separate Up/Down counter.
How do I retain the last computed Math value when the enable signal drops?
Open the Math block properties and set Behavior on En=0 to Last Value. This option freezes the output at its last computed state, which is essential for cumulative totalization across multiple sample cycles.
Can the LOGO! 8 count past 32,767 to record long-term energy totals?
Yes. Use a cascaded Up/Down counter chain: a 0-999 Wh counter drives a 0-999 kWh counter, which drives a 0-32,767 MWh counter. Each counter must stay within the 0-3,599 retaining range to persist across power cycles, and the cascade yields a usable range of 3,599 MWh before the top stage saturates.
Why does my totalization drift upward when the load is constant?
Drift on constant load usually indicates that the Math block is being triggered more than once per cycle. Insert a positive-edge (P) trigger on the cycle timer output and wire its Q output to the Math block's En input. The P-trigger ensures exactly one addition per rising edge, eliminating duplicate adds caused by contact bounce or slow cycle timing.
How do I prevent overflow when adding values above 3,600 W per cycle?
Use the asynchronous add/subtract strategy: configure a 1 s cycle timer (TH = 0.99 s, TL = 0.01 s) and insert short sub-impulses (TH = TL = 0.05 s) that subtract 3,600 W from a downstream register before the next addition. Up to 8 sub-impulses fit in the 0.99 s high phase, giving a maximum addable power of 28,800 W per cycle. The program cycle time must stay below 0.99 s to avoid drift.