Tutorial: Reading Water Flow rate with Water Flow Sensor

This is part of a project I have been working on and I thought I would share it here since there have been a few threads on how to read water flow rate in liters per hour using the Water Flow Sensor found in the Seeed Studio Depo. It uses a simple rotating wheel that pulses a hall effect sensor. By reading these pulses and implementing a little math, we can read the liquids flow rate accurate to within 3%. The threads are simple G1/2 so finding barbed ends will not be that hard. I found some at a hardware store for $1.89 each.







You will need
[list]

  • Seeeduino / Arduino

    Water Flow Sensor

    10K resistor
  • [/list]

    Wiring up the Water Flow Sensor is pretty simple. There are 3 wires: Black, Red, and Yellow.
    [list]

  • Black to the Seeeduino’s ground pin

    Red to Seeeduino’s 5v pin

    The yellow wire will need to be connected to a 10k pull up resistor.and then to pin 2 on the Seeeduino.
  • [/list]

    Here is a fritzing diagram I made to show you how to wire it all up.





    Once you have it wired up you will need to upload the following code to your Seeeduino. Once it is uploaded and you have some fluid flowing through the Water Flow Sensor, you can open the serial monitor and it will display the flow rate, refreshing every second.

    [code]
    // reading liquid flow rate using Seeeduino and Water Flow Sensor from Seeedstudio.com
    // Code adapted by Charles Gantt from PC Fan RPM code written by Crenn @thebestcasescenario.com
    // http:/themakersworkbench.com http://thebestcasescenario.com http://seeedstudio.com

    volatile int NbTopsFan; //measuring the rising edges of the signal
    int Calc;
    int hallsensor = 2; //The pin location of the sensor

    void rpm () //This is the function that the interupt calls
    {
    NbTopsFan++; //This function measures the rising and falling edge of the

    hall effect sensors signal
    }
    // The setup() method runs once, when the sketch starts
    void setup() //
    {
    pinMode(hallsensor, INPUT); //initializes digital pin 2 as an input
    Serial.begin(9600); //This is the setup function where the serial port is

    initialised,
    attachInterrupt(0, rpm, RISING); //and the interrupt is attached
    }
    // the loop() method runs over and over again,
    // as long as the Arduino has power
    void loop ()
    {
    NbTopsFan = 0; //Set NbTops to 0 ready for calculations
    sei(); //Enables interrupts
    delay (1000); //Wait 1 second
    cli(); //Disable interrupts
    Calc = (NbTopsFan * 60 / 7.5); //(Pulse frequency x 60) / 7.5Q, = flow rate

    in L/hour
    Serial.print (Calc, DEC); //Prints the number calculated above
    Serial.print (" L/hour\r\n"); //Prints “L/hour” and returns a new line
    }
    [/code]




    I hope this helps someone out! Thank’s to Seeed Studio for hooking me up with a sensor so I could develop this tutorial.

    Can’t wait to post it onto our product page… Thank you so much pal!

    No problem Eric. I enjoyed writing it. I am working on the next write up, where I will show the user how to display that flow rate on an LCD. Eventually I want to set it up so the user can log the data using the Seeeduino Stalker.

    This is very interesting! Thanks for posting. I have a question around a practical example I’m thinking about putting together around this. I see the the “accurate within 3%” note and I get a bit nervous…



    How appropriate would this sensor be for measuring small amounts of liquid, like cups of beverage from a beverage dispenser? I would ideally like to achieve centiliter accuracy, if I pour a 50 centiliter cup of liquid (half a liter), is this sensor going to be resolute enough? …Do I risk being off by 1.5 cl or so per beverage? Or better yet, can it be calibrated?



    Thanks!

    I was think the same thing. I am wanting to dispense different liquids in the same fashion but I am wondering what this 3% means too. What would be the lowest accurate amount it could safely measure?



    Kevin

    Hello,



    Thank you very much for the write up. I am a newbie to microcontrollers and particularly in regards to fluid dynamics and flow rate.



    Can anyone explain in detail the formula used:



    NbTopsFan * 60 / 7.5 used in the code above

    Hi everyone,



    Sorry for my english but i’m french… In first thank you for your post it’ very interesting… I have just one qustion about your code, the flow is in L/min no? In the data sheet 7.5Q is for L/min…



    Bye

    eltoddo: Yes, it will be off 1.5cl in the worst case. If, and only if, the accuracy is constant for a certain device, it can be configured. Lets say you have constant -1% error in measured vs. real value, (aka. 50cl measured, 50.5cl real) you pull 1.01 as multiplier into the code. In +1%, you use 0.99 as multiplier. (Yes, it is a bit wrong, but I’m just making it simple enough.) Experiment and change the multiplier value to compensate. If the error was constant, that is.



    Nashblackcat: If the sensor works with 1 to 30 liters per minute(LPM) range, 50cl is gained with 6LPM in 5 seconds. Take that as 10cl in 1 sec, and if going down to 2LPM speed, it is ~3.33cl in one sec. Hard to say what the safe margin is, but I would NOT try filling 4cl vodka shot. 8cl maybe.



    LeeLinkoff: In the formula NbTopsFan * 60 / 7.5 , the NbTopsFan is the amount of rising pulse edges detected by the controller in a set delay time. Here it was 1 second. So we have here our pulses per second and the *60 multiplies this value by 60, so we get pulses per minute. The 7.5 is a constant set in the datasheet of the sensor that tells us the relation between flowrate and pulses per minute the sensor is sending. I would add here a correction value *0.97 to *1.03 if the measurement error is constant to a certain device.



    nikoumouk: I agree, the comment and code is wrong and real output is in LPM. Add *60 or change original *60 to *3600 to get liters per hour.

    What happens to pulses that arrives while the interrupt is disabled? Don’t you need a buffer here?

    dnordenberg: No, you don’t care about them. We need to look only the pulses during the 1 second period for our calculation of flow. The problems will come up only if the flow will change a lot in a short period of time, instead of being (almost) constant.

    Just to be clear (sorry for asking a dumb question)



    The breadboard diagram looks to have the power and ground connected together and the power connected to signal at the left of the breadboard… This isn’t the case, correct?



    Thanks.



    -Rob

    I just got two sensors and I am kind of disappointed.

    I measured the repeatibility of the measurement and it was terrible.

    I wanted to use those to measure how much beer I have left in my keg but I am not sure ig those sensors can be used for this.

    When the flow is slow (which sometimes happens when you want to avoid foaming) the number of pulses generated by the sensor is about half of what it is when liquid flowing with high velocity. For 0.5l water (i measured the amount passed by puring to the measured device) the difference in number of “ticks” is big I got about 121 tick with slow flow and 240 with water tap fully open…

    I think this sensor is not good to use in the systems where you often stop/start the flow.

    This is a great book that really helped me use this sensor. It also uses an LCD (I used one I bought from seeedstudio, the brick one that they don’t have anymore in the shop, I see)



    <LINK_TEXT text=“http://www.practicalarduino.com/project … flow-gauge”>http://www.practicalarduino.com/projects/water-flow-gauge</LINK_TEXT>



    I see the author posted the schematics for this project. Unfortunately I don’t see arduino sketch and I don’t think I’m allowed to post it here. Too bad because the only thing I changed in his sketch was the constant (7.5 instead of 4.5).



    But I really believe the book is really worth checking.


    From the book I mentioned "Practical Arduino" :

    "There is a slight complication though: most flow-rate sensors do not have a consistent scaling factor across their entire operational range.
    At low flow rates the sensor might be impeded more by friction in the bearings, so its output frequency could actually be lower per liter than at higher flow rates. That variation could be corrected in software by applying a different scaling factor depending on the measured pulse rate. However, because the accuracy of inexpensive flow sensors is typically only +/– 10% anyway it doesn't really matter much in practice that the scaling factor deviates slightly at low flow rates."

    PS: you can also find his sketch on the link I posted

    The best possible solution for filling glasses or other “reasonably” sized containers, is to use a dummy measuring container in between the storage container and practical container.



    Arrangement goes like this: You have flow sensor and automated valve on bottom of the measuring container. The measuring container is sizes so, that when full, it has the same volume as your practical container (eg a glass or tankard). Another automated valve and another flow sensor is placed on top of the measuring container. The measuring container also needs a hose or tube for replacement air to get liquid flow smoothly in and out of the container. The tube or hose end should be placed higher than the liquid level in storage container, so that it won’t overflow during filling. Now you can use your uC of choice to read flow sensors and control the valves.

    Procedure is like this: Close valves, open top valve, poll for flow, when no flow, measuring container should be full. Close top valve, open bottom valve to fill the practical container, poll for flow, when no flow, measuring container should be empty. Close bottom valve. Repeat.



    With this, one could calculate the remaining liquid in storage container too. As line pressure changes according to the amount of liquid in storage container, tracking the time needed to fill the measuring container would be relative to the amount of liquid still left. Measure time for filling when container is full, maybe some midpoints, and when nearly empty. Or collect a list of times every time the measuring container is filled and create a chart of time/number of fills.



    One could also use line pressure, monitor it’s change and use time and line size to fill containers accurately.

    Hi, I have used your code with the water flow sensor from seedstudio.

    When I run the code and slowly pour water into the sensor, I obtain value that exceed the range of the sensor (32 to 500LPM).



    I copypasted the code in arduino and the electrical circuit is exactly like the one on the picture. I bought 2 sensor and they both display the same problem.



    I would like to know if there anyone else experiencing the same problem? and is it possible that the sensor are broken?

    HEY

    I have bought a G1/2 Water Flow sensor . But it gives an output for only upto 3 lpm .

    What changes do i have to make to this sensor so that it can measure upto 6 or 7 lpm.

    Do i use a more sensitive hall effect sensors so that it is able to capture the faster rotations of the rotor. Could you please suggest a hall effect sensor .

    REPLY ASAP PLEASE.

    Thanks for posting.



    I’m newbie of seeeduino/arduino.



    In this case what the relation between 10k resistor and measuring max LPM?

    And is it correct that 10k resistor connect between 5V and 2nd pin of Seeeduino?

    I’m trying to connect the G1/2 flow sensor to an Atmel attiny85 chip. I’m having trouble having the chip read the sensor. Are there any tricks i.e. pins I should use or a special way to callout the sensor?



    thanks.

    Is the amplitude voltage level of pulse signal dependent on the input voltage level? For example if the input voltage is 24 V then will the amplitude level of the pulse be 24 V ?