I have a water flow sensor with Arduino Megs 2560 board. I am trying to conceptualize the concept to calculate amount of water getting dispensed esp. in 1,2,3,4 and 5 liters quantity.

Water flow sensor specification:


I would connect the sensor input to any pin from 22-53 pin.

Suppose I connect to ping 23 so in my setup() function I would add:

attachInterrupt(23, pulseCounter, FALLING);

where pulseCounter is an interrupt function triggered on each signal generated from sensor and increments a variable

void pulseCounter()
  // Increment the pulse counter

Now how do I calculate the flowrate and water dispensed in milliliters using this information? How do I know the freqz of the MCU (which is required as water dispensed per pulse is different for different MCUs)?

  • If your sensor really gives pulses/second, the water amount per second must be constant, i.e. the water pressure must be constant, because the pipe diameter is surely constant. Otherwise there's no way to calculate the volume. I'm quite sure this is not the case, but your sensor gives a certain number of pulses per volume. Am I right? Would you share the sensor datasheet with us?
    – mic
    Commented Nov 14, 2014 at 8:23
  • Sorry it would be pulses per volume of water,. I have updated the question accordingly
    – Programmer
    Commented Nov 14, 2014 at 8:27

1 Answer 1


the pulse rate can be derived without knowledge of the CPU clock speed. To achieve the pulse rate from your sensor input you can do two things:

  1. count the pulses dn within a certain (i.e. fixed) interval T of time) or
  2. measure the time dt between two pulses.

I'll come back later on this concerning accuracy.

To measure time, you can use the function millis(). This function returns you the milliseconds since bootup as an integer upon calling it. Method 2 is rather simple to implement.

  • Setup three global integers a, b and c.
  • At the end of setup set a to current millis();
  • In your ISR set b to current millis();
  • Also in ISR: set a=b;
  • calculate c=b-a (i.e. your dt);

c holds your current full wave pulse length in milliseconds, which is updated every pulse. You can derive your flow then from it.

The first method uses a counter as demonstrated in your suggestion. You then need a timer outside your ISR, two variables d and e. Either you poll millis() in your main "loop()" or you install a timer interrupt, both will work here.

  • At the end of setup set d to current millis();
  • If your predefined time T is passed (by comparing d with millis(),
  • save your counted pulses to e=pulseCount
  • reset: pulseCount=0

e holds your pulse count during interval T, from which you can derive your pulse rate.


Method 1's accuracy increases with higher T and higher flow speeds. Therefore the update rate of your measurement is low. By changing T you can easily increase accuracy on cost of responsiveness. This is perhaps a good idea, if you have pulsating flows in your tubings, or the sensor is a bit rattling.

Method 2's accuracy increases with lower flow speeds. It is best to measure very low flow speeds. Update frequency is minimal (1/dt).

Regarding this sensor you should calibrate it yourself. It is a cheap and rugged device (I'm using it, too), but its output depends on several parameters like mounting position, fluid viscosity and so on. Make sure, there are no bubbles, foam or contaminants in your fluid systems as they garble the results effectively.

n.b.: make sure to take care of an overflow of millis().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.