IMDHMIControls LibraryShop main store

Troubleshooting

Why water flow from pulse counts reads wrong in ladder logic

PLC programming Published 6 min read By IMDHMI controls team

Short answer

Three pulses of 1 m³ in a 5-minute gate must read 36 m³/h. When the number reads wrong, the ladder is at fault in one of three ways: the counter increments on the input level instead of the rising edge, the counter is cleared before the compute rung, or the gate TON never restarts. Count edges, compute before clearing, and break the timer enable for one scan at each gate end.

On this page
  1. The formula and the worked example
  2. Build the gate window from a TON
  3. Count each pulse on the rising edge
  4. Clear after the compute rung
  5. Prove the window on the bench
  6. Resolution and the decision by symptom
  7. Questions asked

Your arithmetic is right and the ladder is what breaks the reading. Three pulses of 1 m³ in a gate of 5 minutes (1/12 h) must give 36 m³/h, and a correct program shows that after every gate. Wrong values trace back to three faults: the counter increments on the input level instead of the rising edge, the counter is cleared before the compute rung reads it, or the gate TON never restarts and the display freezes. Each fault has its own symptom, and each fix is one rung.

The formula and the worked example #

Flow in cubic metres per hour equals the pulse count times the pulse weight divided by the gate time in hours. In integer form, multiply by 60 and divide by the gate time in minutes, where GateMinutes is that gate time in minutes:

Flow (m³/h) = Count × Weight × 60 ÷ GateMinutes

Worked example: a 5-minute gate is 5 ÷ 60 = 1/12 h, about 0.0833 h. Three pulses at 1 m³ each collect 3 m³ in the gate, and 3 ÷ (1/12) = 3 × 12. The integer form confirms it: 3 × 1 × 60 ÷ 5 = 180 ÷ 5 = 36 m³/h. Multiply before you divide, because integer division truncates and a small product loses less than a large one.

Counting pulses inside a timed window is the same method as frequency measurement from timed pulse counts on an S7-200; a flow rung only adds the weight multiplication after the window closes.

Build the gate window from a TON #

One TON and two binary inputs are enough. The tag names here are mine for this example, not your platform's: GateTimer is the TON with its preset equal to the gate time in the timer's own time base; PulseIn is the pulse input, I1 in your sketch; PrevPulse and GateEnd are BOOL bits that start FALSE; PulseCount and FlowRate are DINT registers that start at 0; RunEnable is the second binary input, used as a run permit or a manual reset.

Build and recycle the window with two rungs:

  1. Rung 1: RunEnable and NOT GateEnd in series enable GateTimer.
  2. Rung 2: the GateTimer done bit and NOT GateEnd in series drive the GateEnd coil, so GateEnd is on for exactly one scan.

The reset is already inside rung 1: on the gate-end scan, NOT GateEnd breaks the timer enable, the accumulator clears, the done bit drops, and the TON restarts on the next scan. Do not instead put the done bit in series with the timer's own enable — that also resets the timer, but the edge lasts less than one scan and no rung after it can act on it. The pattern assumes coils update as each rung is solved; the bench test below confirms the sequence on your controller.

Count each pulse on the rising edge #

A level-based increment adds one count on every scan while the input is high, so a single pulse lands as tens or hundreds of counts and the flow reads far too high. Count the transition, not the level:

  1. Edge rung: PulseIn and NOT PrevPulse in series add 1 to PulseCount.
  2. Last rung of the program: move PulseIn into PrevPulse, so the stored state follows the input by one scan.

An edge rung fixes double counting, not lost pulses. When the pulse is short against the scan time and the input filter, counts go missing instead, and why a PLC misses short input pulses and how to catch them walks through that timing.

Symptom at the counter Cause First fix
Jumps by tens or hundreds per pulse level counting: the rung fires every scan while the input is high add the PrevPulse edge rung
Misses pulses pulse width close to the scan time or the input filter setting widen the pulse or move it to a high-speed input
Never clears clear rung not driven by GateEnd drive the clear from the GateEnd rung
Correct count, flow reads 0 clear sits above the compute rung move compute above clear

Clear after the compute rung #

On the gate-end scan, rung order decides the number. Compute and store first, clear second:

  • Compute rung: on GateEnd, write FlowRate = PulseCount × Weight × 60 ÷ GateMinutes into the holding register.
  • Clear rung: below the compute rung, on GateEnd, move 0 into PulseCount.

If the clear sits above the compute, the compute reads an empty register and the flow reads 0 after every gate, which is the most common zero-reading fault. Write FlowRate only when GateEnd is true, so the display holds between gates instead of collapsing while the counter sits empty. For a pulse weight below one cubic metre, compute in the smaller unit and scale once at the display rung.

If your controller ships a totalizer instruction instead, use it: the Studio 5000 Logix Designer instruction set documents a Totalizer (TOT) instruction that does gated counting and accumulation, and the rungs above rebuild that core from a plain TON where no totalizer exists.

Prove the window on the bench #

  1. Set the GateTimer preset to 1 minute and Weight to 1, so the arithmetic stays trivial.
  2. Pulse the input exactly 3 times inside one gate. PulseCount must step 1, 2, 3: one count per pulse and nothing more.
  3. At the gate end, FlowRate must read 3 × 1 × 60 ÷ 1 = 180 m³/h.
  4. Restore the 5-minute preset, pulse 3 times again, and confirm 36 m³/h.
  5. Confirm PulseCount returns to 0 and FlowRate holds steady through the next gate until the next GateEnd.
  6. Add a first-scan bit that clears PulseCount, GateEnd and the timer, and discard the first result after every power-up: a gate cut short by a power cycle or a mode change leaves a partial count against a full gate divisor, which reads low.

Resolution and the decision by symptom #

The display moves in steps of one pulse per gate. With a 1 m³ pulse weight, one extra pulse adds 12 m³/h. Lengthen the window to narrow the step: a 10-minute gate makes one pulse worth 1 ÷ (10 ÷ 60) = 6 m³/h, and a smaller pulse weight narrows it the same way.

Then fix by symptom:

  • Reads far too high and jumps wildly: level counting. Add the PrevPulse edge rung.
  • Reads 0 after every gate: the clear sits above the compute. Swap them on the GateEnd scan.
  • Collapses to 0 between gates: FlowRate is recomputed from an empty counter. Write it only on GateEnd.
  • Freezes at one value: the done bit never drops because the timer never restarts. Break the enable with NOT GateEnd for one scan.
  • Reads low right after power-up: a partial gate against a full divisor. Clear on the first scan and discard that first result.

Questions asked

What does the display show when no pulse arrives during a gate?

The compute rung stores 0, so the reading drops to 0 m³/h, and that is a correct reading rather than a fault: real flow is below one pulse per gate. With a 1 m³ pulse weight, flow under 12 m³/h shows as 0 in most gates and as one step when a pulse lands near the gate end.

Can the gate be timed without a TON?

Yes, on a platform with a real-time clock. In a logger sketch, the compare NE RTC_Sec, REG2, Q1 fires once per second by detecting each change of the seconds register; counting 300 of those ticks reproduces the same window length, and the tick counter's finished value replaces the TON done bit in rung 2. That frees you from the timer entirely on clock-equipped controllers.

How do I handle a pulse weight that is not a whole cubic metre?

Compute in the smaller unit and scale once at the display. A meter that pulses every 10 L collects 30 L in a 5-minute gate, and 30 × 60 ÷ 5 = 360 L/h, which the display rung divides by 1000 to show 0.36 m³/h. Multiply before dividing so integer math stays exact.

Does the gate TON need a separate reset instruction?

No. A TON resets whenever its enable rung goes false: the accumulator clears and the done bit drops. Rung 1 breaks the enable with NOT GateEnd for the one scan after the gate ends, and that is the whole reset. A separate reset coil belongs only to timer instructions that latch the done bit; drive it from the same GateEnd pulse that runs the compute.

Keep reading

  • How-to

    Build a siren pulse count sequence in S7-1200 ladder logic

    Use a positive-edge request to load a target of 3, 2, or 1 pulses, then run a four-state ladder sequence — idle, start, stop, count — that drives Q6.2 (START) and Q6.1 (STOP) from separate states so the two SIMOCODE Pro V commands never overlap. With a 2.0 s on-time and a 1.0 s gap, the three patterns finish in 11.0 s, 7.0 s, and 3.0 s.

    PLC programming

  • Selection guide

    Ladder logic simulator selection by instruction coverage

    Score a ladder logic simulator by instruction coverage, proven with execution rather than animation. Build one test rung per instruction class - on-delay and off-delay timers, a seal-in and a set/reset bit across a restart, an up counter, and math with negative and limit values - and compare observed behavior with the standard's timer definitions. A tool that holds the elapsed value after the timer input drops, or wraps integer overflow silently, is out for sequence work; a tool that passes every class goes on the shortlist.

    PLC programming

  • Troubleshooting

    When a CODESYS TON works in structured text but not ladder

    The structured text routine counts because its four statements run in sequence inside one scan; the ladder version fails when the reset network clears Timer.Q before the addition samples it, or an unconditional reset restarts the timer every cycle. Put the call, the Q test, the addition and the reset of the same TON instance in one ladder network, left to right in that order, and it counts exactly one B per interval.

    PLC programming