IMDHMIControls LibraryShop main store

How-to

Measure water flow by counting pulses in a gated ladder window

PLC programming Published 9 min read By IMDHMI controls team

Short answer

Count meter pulses inside a self-resetting TON gate and multiply by the pulse weight: flow in m3/h equals pulses times volume per pulse times 3600 divided by gate seconds. With the exercise's numbers — 3 pulses of 1 m3 in a 300 s gate — the held result is 36 m3/h. Feed the counter from a rising-edge one-shot and clear the count only after the result is held.

On this page
  1. The formula: counts to cubic metres per hour
  2. Build the ladder rung by rung
  3. Worked example: 3 pulses of 1 m3 in a 300 s gate
  4. Order the end-of-gate rungs so the result survives
  5. Catch bounce and missed pulses
  6. Wire the run input and read the 1-hour span
  7. Test the program before it goes on the machine
  8. Questions asked

Flow is a count divided by a window. Let a TON timer run as a free-returning gate, count rising edges of the meter pulse input inside that window, and at each gate end compute flow as pulses times the volume per pulse times 3600, divided by the gate time in seconds. The exercise's own numbers give the scale: three pulses of 1 m3 inside a 300 s gate hold 36 m3/h on the display until the next gate ends. Three details decide whether the program works: the counter must see edges, never a held level; the timer must reset itself from its own done bit; and the compute rung must run before the counter clears. The build below uses only what the exercise allows — two binary inputs and one TON.

The formula: counts to cubic metres per hour #

The conversion is one line, straight from the definition of flow as volume over time:

Q (m3/h) = N × V × 3600 ÷ T

N is the pulses counted in the finished gate, V is the pulse weight — the volume one pulse represents, read from the meter nameplate — and T is the gate time in seconds. The 3600 is seconds per hour: dividing the gate's volume by its length in seconds gives a per-second rate, and multiplying by 3600 restates it per hour, the unit the operator expects.

The same line reads as a multiplier: K = 3600 ÷ T is the number of gates in an hour, and every pulse in a gate lifts the reading by K cubic metres per hour — K is both the gate count per hour and the size of one display step. That makes it the resolution knob: a longer gate refreshes the rate more slowly but steps it more finely; a shorter gate refreshes faster and steps more coarsely.

The pulse weight comes from the meter or its datasheet, never from the PLC. If the primary measurement is an open channel rather than a pulse-output meter there is no pulse to count, and measuring effluent flow in an open channel is the different problem to settle first.

Build the ladder rung by rung #

Six rungs cover it. The tag names are illustrative — rename them for your platform — and this edge-into-counter pattern is the same one that counts flowmeter pulses in running plant code, for instance in a two-pump station driven by flowmeter pulses on an FX3U.

  1. Gate timer. Rung 1 enables a TON — the IEC 61131-3 on-delay timer — T1, with the gate preset, from input I2 (RUN) in series with a normally closed contact of T1's own done bit. While RUN is on, T1 times to preset, the done bit sets, that contact opens, the timer resets and timing begins again. The done bit is true for roughly one scan per gate, and that single scan is the entire end-of-gate event.
  2. Pulse debounce. Rung 2 feeds input I1 into a second TON, T2, preset to tens of milliseconds — long enough to bridge contact chatter, shorter than the meter's minimum pulse width from its datasheet. T2's output is the cleaned pulse level.
  3. Edge into counter. Rung 3 puts a rising-edge (positive-transition) contact of the cleaned level in series with the count input of an up-counter, C1, the IEC 61131-3 CTU. One closure now yields exactly one single-scan count, and C1's accumulated value is the gate count N, an integer counting up from 0. Leave C1's reset input unassigned here; the clear belongs to the end-of-gate rungs.
  4. Compute at gate end. Rung 4 is enabled by T1's done bit and evaluates the formula: multiply N by K (the constant 3600 ÷ T) and move the product into the held flow register FLOW_H. Use REAL arithmetic, or a wide integer with every multiply done before any divide.
  5. Clear last. Still under the same done-bit contact — the next instruction on rung 4, or the rung immediately below — reset C1 and zero N. This must be the final action of the end-of-gate scan; everything after it sees an empty count.
  6. Run and abort. Rung 6 watches I2. When RUN drops, reset T1, clear C1 and move 0 into FLOW_H, so the display does not carry a stale rate across a stopped period.

Worked example: 3 pulses of 1 m3 in a 300 s gate #

The inputs, each taken from the exercise:

  • Pulse count N = 3: input I1 is triggered three times inside one gate.
  • Pulse weight V = 1 m3 per pulse: the weight the exercise assigns to I1.
  • Gate time T = 300 s: the exercise's five-minute gate, put into seconds because the formula divides by seconds.

The rule in words: flow in cubic metres per hour equals the pulses counted in the gate, times the volume one pulse represents, times the seconds in one hour, divided by the gate time in seconds.

The arithmetic:

  • Volume in the gate: 3 pulses × 1 m3 = 3 m3.
  • Gates per hour: 3600 s ÷ 300 s = 12.
  • Flow: 3 m3 × 12 = 36 m3/h. The single-line form gives the same figure: 3 × 1 × 3600 ÷ 300 = 10,800 ÷ 300 = 36.

Result: at gate end the compute rung moves 36 m3/h into FLOW_H, and the display holds that value until the next gate end.

Decision: at this gate preset every pulse is worth 12 m3/h on the display, so one contact bounce that slips past the debounce inflates the rate by a full step. Accept the program when a hand-pulsed gate holds that figure at gate end, C1 restarts from 0, and the next empty gate drops the display to 0.

Order the end-of-gate rungs so the result survives #

The known failure here is the clear that outruns the compute. A done bit true for one scan gets eaten rung by rung, and a compute rung placed after the clear multiplies an empty count and writes 0 at every gate. The quieter path is a reset wired into C1's reset input from the same done bit, which empties N before the compute rung is evaluated. Both signatures, plus the level-versus-edge trap, are worked through in why water flow from pulse counts reads wrong in ladder logic.

Inside your own program the fix is order: multiply, move to FLOW_H, then clear, in that sequence, all under the same done-bit contact, with exactly one clear instruction. An empty gate must still write — the compute rung fires on the done bit, not on the count — so zero pulses move 0 into FLOW_H and the display falls to 0 instead of freezing at the last rate. If the pulse weight is fractional, do the whole calculation in REAL or scale the weight to litres: integer arithmetic truncates, and a truncation anywhere before the final divide rounds the rate down.

Catch bounce and missed pulses #

A reed or relay pulse contact bounces: one closure can arrive as two or three counts, and every phantom pulse lifts the gate result by one full step of K. Rung 2's debounce bridges the chatter and rung 3's edge contact makes a held level harmless. Prove the pair before trusting any rate: force I1 closed for several seconds and watch C1. A climb of one count per scan means the one-shot is missing; a jump of two or three per closure means the debounce preset is too short for that contact.

The design's hard limit is scan time. A pulse must survive two scans to be counted — one for the input image to latch it, one to produce the edge — so the fastest pulse train this software path follows has a period of about two scan times; that failure and its remedies are set out in why a PLC misses short input pulses and how to catch them. A faster meter goes to hardware: route the pulse to a high-speed counter input, let it accumulate between gate ends, and read that counter in the compute rung instead of C1 — the gate-and-volume formula applies unchanged. Counting pulse volume to reach a flow rate in m3/h on an S7-1200 high-speed counter runs this same computation on hardware counting.

Wire the run input and read the 1-hour span #

Input I2 is RUN. While it is off, rung 6 holds T1 reset, C1 cleared and FLOW_H at 0. Every completed gate is then a full window — a TON always times its whole preset while enabled — so no first-gate suppression or partial-window correction is needed after power-up. What a mid-gate RUN dropout loses is the unfinished window's volume: the timer resets with the count, so those pulses reach no gate at all. If that volume must be billed, add one pulse weight to a separate running volume register at the same edge that increments C1, and let the totaliser, not the rate register, carry volume across gate boundaries.

The exercise's one-hour overall time is the rate's unit base, not a second timer: the ×3600 in the formula already restates each gate's volume per hour, and twelve windows of the gate preset fill the span exactly. If the requirement is a rolling one-hour rate instead, keep a rolling array of gate counts one hour deep — K entries at this gate length — shift it at each gate end, and rate the sum: sum × V is the volume of the most recent hour.

Test the program before it goes on the machine #

Force inputs only on a bench rig or a machine that is locked out — a forced input bypasses whatever interlock uses it. Then run six checks:

  1. Hand-trigger I1 exactly three times inside one gate, each closure released before the next. At gate end, FLOW_H must hold the worked example's figure, 36, and C1 must read 0.
  2. Hold I1 closed through a full gate. C1 must rise by exactly one; a climb of one per scan means the edge contact is missing.
  3. Leave a whole gate with zero closures. FLOW_H must fall to 0, which proves the compute rung fires on the done bit rather than on the count.
  4. Drop RUN mid-gate and restore it. T1 and C1 must reset, and the next result must cover only the window after the restore.
  5. Time two consecutive gate ends against a stopwatch. Each gate must run its preset to within a scan time or two; drift beyond that means the self-reset contact is missing and the done bit is stuck on.
  6. Make and break the input slowly, deliberately chattering the contact, and confirm one physical closure still lands as exactly one count.

Three failed results cover most first builds, and each maps to a rung above: FLOW_H stuck at 0 at every gate end is clear before compute (rungs 4 and 5), a count climbing every scan is a missing edge (rung 3), and a timer frozen at preset is a missing self-reset contact (rung 1).

Questions asked

What changes if the meter puts out more than one pulse per cubic metre?

Nothing in the logic changes, only the pulse weight. Read the weight from the meter nameplate: a meter that pulses twice per cubic metre has a weight of 0.5 m3 per pulse, and at the exercise's gate preset each pulse is then worth 0.5 × 12 = 6 m3/h on the display. The gate timer, the edge one-shot and the compute-then-clear order all stay as built.

Can I build the one-shot if my ladder has no rising-edge contact instruction?

Yes, with two rungs. Energise an internal bit through the cleaned pulse contact in series with a normally closed contact of a second internal bit, and on the rung below energise that second bit from the same pulse contact. The first bit is then on for exactly one scan per closure, and the counter takes its count input from it. Any ladder dialect can express this pair.

Does a solid-state pulse output need the debounce rung?

Usually not at full length. A mechanical meter's reed or relay contact bounces for a few milliseconds and needs the debounce timer preset to bridge it. A transistor, Hall-effect or open-collector output switches cleanly, so the debounce can be shortened to a few milliseconds or removed. Keep the rising-edge one-shot either way: it stops the counter counting a held level every scan, which no amount of debouncing fixes.

Keep reading

  • Troubleshooting

    Why water flow from pulse counts reads wrong in ladder logic

    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.

    PLC programming

  • How-to

    Program a two-pump station with flowmeter pulses on an FX3U

    Build the station in staged rungs: a master fault latch, a start/stop run latch, pump 1 on the level call, pump 2 after 30 s (T1 K300 on the 100 ms base) or on a pump 1 overload, and the flowmeter counted on high-speed counter C235 at up to 100 kHz, scaled to L/min as pulses per second × 60 ÷ K-factor.

    PLC programming

  • 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