Arduino flow rate sensor interfacing: The Best Tutorial

Arduino flow rate sensor tutorial

Ever wanted to measure liquid flowing through a pipe / container? Wanted to create a control system based on the water flow rate or quantity ? For example while gardening, to measure the amount of water used to water your plants , to prevent wastage , etc.  If yes, then this very easy DIY project is for you. Here, step by step instructions are given on how to measure water flow rate and quantity using an arduino flow rate sensor. Have fun with this DIY hacking tutorial!

What are the stuff required to do this project?
Hardware :

  1. Arduino / Arduino Clone or make your own custom arduino board with this tutorial.
  2. Water/Liquid flow meter like : Flow meter.
  3. Connecting wires.

Software :

  1. Arduino IDE : Arduino

So how does this work ?
The arduino flow meter mentioned in the above link works on the principle of “Hall Effect”. According to which, a voltage difference is induced in a conductor transverse to the electric current and the magnetic field perpendicular to it. Here, Hall Effect is utilized in the flow meter using a small fan/propeller shaped rotor which is placed in the path of the liquid flowing.

The liquid thus pushes against the fins of the rotor, causing it to rotate. The shaft of the rotor is connected to a hall effect sensor. It is an arrangement of a current flowing coil and a magnet connected to the shaft of the rotor. Thus a voltage/pulse is induced as this rotor rotates. In this flow meter, for every litre of liquid passing through it per minute it outputs about 4.5 pulses.This is due to the changing magnetic field caused by the magnet attached to the rotor shaft, as seen in the picture below. We measure the number of pulses using an arduino. And then calculate the flow rate in L/hr using a simple conversion formula explained in step 2.

Arduino flow rate sensor tutorial

Working of a flow rate sensor

Step 1 : Connecting the arduino flow rate sensor

Here, the connections required for this flow rate sensor with respect to arduino is very minimal. There are only three wires coming from the flow rate sensor. The 5V Vcc (Red wire) , The Gnd (Black wire) , and the signal/pulse (Usually yellow) line. Connect the Vcc and Gnd of the flow meter to the arduino’s Vcc and Gnd. The pulse line of the flow rate sensor is connected to arduino’s digital pin 2. The arduino’s digital pin 2 serves as an external interrupt pin (interrupt pin 0). Now you have hooked up your flow meter to the arduino.

Arduino flow rate sensor tutorial

Arduino flow rate sensor connections

Step 2 : Upload the flow meter code to the arduino and measure water flow rate

Upload the following code to your arduino : Flow Meter Code. Here the code uses an external interrupt on arduino’s digital pin 2. This is used to read the pulses coming from the flow meter. When arduino detects the pulse, it immediately triggers the pulseCounter() function . This function then counts the total number of pulses.

In this arduino flow rate sensor, for every litre of liquid passing through it per minute it outputs about 4.5 pulses. Thus dividing the total pulse count obtained by 4.5 will give you the total amount of liquid passing through it in litres/minute. Further dividing it by 60 will give you the flow rate in litres/hr. And hence the total amount or quantity of water/liquid that has passed through it. The sensor  is accurate to within 3%.

A video showing the arduino flow rate sensor in action :


Use this arduino flow rate sensor with a solenoid valve to monitor and control the water used. You can use it in your gardening system. Or interface it with an LCD display for other applications that require you to measure water flow rate and quantity. Have fun with your arduino flow rate sensor!

Arduino flow rate sensor tutorial DIY Hacking

Arduino flow rate sensor

Recommended Posts
Showing 93 comments
  • rinku
    Reply

    Sir. I am making a project with water flow senser (hall effect base) and 16*2 lcd
    Want to calculate total volume of water flow
    But i want use only atmega328pu ic..(programed via arduino uno) ant make new pcb with neccesory componet and thing…
    Help me in coding

  • Nathan Gant
    Reply

    Thanks, Arvind, for the excellent Arduino sketch. I tested my flow sensor using your software code, and came out with the following 11 data points:

    3.5 13158
    3.6 12118
    3.625 12204
    3.73 11626
    3.75 11492
    3.76 11554
    3.8 11188
    4 10977
    4.04 11546
    4.33 10309
    4.75 8907
    pulses Milliliters

    Regression line equation: y=22405-2830.266x

    So based on above linear equation, the best K value (pulses per liter) to use in the Arduino sketch for this particular flow sensor would be 4.012. That would give me the closest number for metering, with some built-in mechanical variance from the flow sensor.

    • Nathan Gant
      Reply

      Bivariate data from flow sensor #2:

      4.0 11824
      4.012 10920
      4.025 11345
      4.06 10411
      4.08 11939

      Data from this flow sensor appears to be randomly scattered. Also I rejected one data point, 4.0175, because I was getting over 20 liters and on a second run, over 30 liters, that’s far more than the bucket capacity of 11.053 liters. I did notice the signal pin next to the 10k resistor was loose, so next time I’ll start soldering the connections to eliminate that possibility. I don’t know if having a 4th decimal point in the K pulse value 4.0175 made a difference, there may be other factors causing the output to show excess flow.

      This demonstates a good reason to test your water flows before moving on to the next step.

      Sample size: 5
      Mean x (x̄): 4.0354
      Mean y (ȳ): 11287.8
      Intercept (a): 14420.271184913
      Slope (b): -776.24800141568
      Regression line equation: y=14420.27-776.248x

      So this particular flow sensor would apparently want a factor of about 4.33 to give me an accurate flow rate, a little higher than previous sensor (4.012).

      Also, for the stats, I used

      http://www.alcula.com/calculators/statistics/linear-regression/

      because it was very easy to cut and paste my data set into their site.

      • JuN
        Reply

        Bro, do you follow all the coding and sensor type as what the website taught?

  • Mike Petterson
    Reply

    When you say “In this arduino flow rate sensor, for every litre of liquid passing through it per minute it outputs about 4.5 pulses”
    Are you using the YF-S201 sensor as shown in the pictures?
    If yes, then shouldn’t the correct value be 7.5 as stated in the data sheet?

  • Agenda Généticien
    Reply

    Hi, i believe that i noticed you visited my site so i came to return the desire?.I’m trying
    to to find things to improve my website!I suppose its adequate
    to make use of some of your ideas!!

  • abhilash
    Reply

    im using the same sensor for my project.im getting the out when i compiled the code on aurdino uno but when i executed the same code on the intel galileo gen 2 im getting the output has 0.can anyone help me hoe to sort out this problem

  • Justine Fuentes
    Reply

    What if you wanted to measure alcohol inside of a non-alcoholic beverage? How can we program this?

  • avinashjakkuva
    Reply

    How can u connect to raspberry pi which doesn’t have a digital pin how will u connect to it

  • Emir Porcil
    Reply

    Sir, I need help! I test the water flow sensor(model: YS-S201) with your program code then I use the faucet as the source of water to fill an empty 6.6 L empty bottle. After filled the bottle I checked the serial monitor then I found out that the output liquid quantity is much higher than water filled in the bottle which is a large amount of error. What can I do to fix this? I used a PVC pipe attached with the sensor and the faucet.

  • Jeff Nyika
    Reply

    Hi, so I have 2 flow sensors similar to this and Im trying to use them at the same time, connected to an arduino.
    Im not a pro with arduino so wanted to find out if it is possible to use 2 sensors hooked to the same arduino to measure different places?
    thanks for a response in advance

    • Fong
      Reply

      Brother, did you solve your problem with two sensors? Do you mind to share with me how you complete such as coding and schematic diagram because I am doing the same project with two sensors. I would appreciate if you lend a helping hand.

  • Andre
    Reply

    Really nice-work ;)
    Have a quastion about the code though, if I wanted to change the time interval what would have to change?

    I’m guessing it’s just changing the 1000 (miliseconds) for another value, right?

  • Clay
    Reply

    Excellent writeup! I was able to get the code to work on the Uno, but it doesn’t work on my Yun. I know it’s something to do with the PIN mapping. Could you help? The interrupt routine is not getting fired. I have the wiring the exact same.

    • Julio
      Reply

      Hi Clay Did you figure it out what happens? Also have issues with yun Instead of using serial i use console to read values over the wifi It just print zero when the yf s201 is working I really dont know what to do

      • sylvain
        Reply

        Hi guys,

        I have a Yun as well. The mapping for Digital pins to Interrupt pins is different on the Yun. Mine works using Interrupt 1 instead of 0. The only bit of code to change is: byte sensorInterrupt = 1; // 0 = digital pin 2

  • Vijayanand
    Reply

    can we connect this flow sensor to allen bradley plc, which has only digital input and output. urgent

  • David B
    Reply

    Is there a device that will also show me the direction of the water (e.g. negative voltage?)

  • Aaron
    Reply

    Hi Arvind,

    I was wandering how you knew that this particular sensor creates 4.5 pulses per litre because I am using a slightly different sensor and to get true values of liquid output from your code, I had to change the calibration factor from 4.5 to 65 (sixty five) by trial and error and was wanting to know if there was a more logical process to modifying your code to suits the specs of the flow sensor I am using?

    Thanks

  • vishal
    Reply

    hey arvind i want to monitor whether water is flowing or stagnant through a pipe not the quantity of water flowing can i do that using this flow rate sensor if so where can i find the code??

  • daniel patterson
    Reply

    I am building an aquarium control unit and have most of the code, now im adding the 2 flow meters and it is plowing my mind any help would be greatfull.

  • Shubham chhatre
    Reply

    Arvind sir,
    i want to turn on or turn off the flow of water depending upon the sensor value. how we can write program for that condition?

    • Daniel patterson
      Reply

      i have 2 meters and it is killing me on getting them into my current code any help would be helpfull

  • Miguel
    Reply

    Hi Arvind,

    Thank you for the tutorial.

    I’m using a different sensor but am getting irregular readings even though I have a constant flow (I’m using air, not water).

    I can see that the sensor is giving me a constant square wave but my reading keep varying, about 30% up or down.

    This is the sensor:
    http://uk.rs-online.com/web/p/flow-sensors-switches-indicators/4468315/?origin=null|fp&cm_sp=featureproducts-_-FeaturedProductsContent-_-4468315

    Any suggestions would be greatly appreciated.

    Kind regards,

    Miguel

    • Ana
      Reply

      Hi,
      We are doing a similar project and we want to add in an LED indicator light for when a certain amount of water flows through the sensor. Do you have code to do that?

      Thanks!

      • rebin
        Reply

        also i need this led!
        did you find ? can i see your project?

    • ANDREW
      Reply

      Is it possible to correct for the inaccuracy of the meter with the code?

  • love vegetables
    Reply

    How many flow meters could theoretically be attached to a single Arduino? What considerations would I have to make?
    thank you

  • horoscopo gratis escorpio
    Reply

    Thanks for every other fantastic article. Where else maay anyone get that kind
    of info in such a perfect means of writing? I have a presentation subsequent week,
    and I am at the lopok for such information.

  • swanand
    Reply

    Hello sir,

    can you please provide me the code to interface the sensor with 8051 ucontroller.

  • chloe
    Reply

    Hello everyone, can someone give an advice about what is important/advantages to know the flow rate of water?
    instead of to gardening, why we must control the flow rate…please, give an opinion..

    • Muhammad Ali
      Reply

      my dear friend by controlling water flow you can make automatic system for watering the garden, you can fill constant quantity of liquid in bottles/cans/anything, these are some very basic examples , there are many magics happening with water flow rates

  • udin
    Reply

    Hello sir,
    Im wondering, based on the sensor datasheet, the flow-pulses for 2L/minute= 16 Hz, so, what is the output voltage for 16Hz frequency?

  • udin
    Reply

    Hello sir,
    Im wondering, based on datasheet
    it was flow- pulse for 2L/min=16hz, what is the voltage for 16hz?

  • Rahul
    Reply

    Greetings friend.
    I created a similar system with flow sensor DN32 and I have very irregular readings is very unstable.
    You know how to improve the stability of measurement readings.
    I am measuring the fall of flow and for some reason my system is very unstable.
    If you know how to improve presicion of readings.

  • ratnesh
    Reply

    thank you sir. I am using water flow sensor model YF-S201 having a different calibration factor than yours.What modification is required in the code, also how one can measure rpm of the rotor.

  • Erik
    Reply

    Is the totalMilli keeping track of the total amount of water in mL flowed through for just that one measurement cycle or for the entire time the unit is turned on? If I wanted to keep track for the total amount of water flowed through, for an entire day perhaps, how would you suggest this be done?
    Many thanks!

  • Matt
    Reply

    Hi thanks for the great tutorial. I was wondering if you could explain how one would interface this with a LCD display. And would there be any reason that you couldn’t do a conversion in code to make the displayed value gal instead of liters? Thanks.

    • Arvind Sanjeev
      Reply

      Hi Matt,
      Please check this tutorial: http://diyhacking.com/arduino-lcd-tutorial/ this will help you :)

      • Matt
        Reply

        Hi Arvind,

        Thanks – your tutorials worked perfectly. I now have a UNO running a flow sensor and displaying results on my 20×4 lcd. Couple more questions if you don’t mind. How could I convert the l/min to gallons? And could you suggest a way to keep a running total of the fluids used? I’m trying to monitor how much rain barrel water we are using annually to water our garden. Sorry for the “entry-level” questions but I am very new to this. Thanks -Matt

  • amar mehdi
    Reply

    void open_valve() {

    digitalWrite(RELAY_A, HIGH); // turn RELAY_A on
    // Serial.println(“Valve Open”);
    Serial.write(“{valve_open}”);
    }

    void close_valve() {
    digitalWrite(RELAY_A, LOW); // turn RELAY_A off
    // Serial.println(“Vavle Closed”);
    Serial.write(“{valve_close}”);
    }

    sir, this part of code I did’nt underatand ? for what purpose it has been used?

  • januar
    Reply

    can you help me if i want to display output from sensor to web (chart) by using php

    • Arvind Sanjeev
      Reply

      Read the data from the serial port of the computer and have a script which posts that to a webpage. Integrate serial communication and http modules for this, you can use python.

  • amar mehdi
    Reply

    sir,
    i,m using the same sensor as you use YF-S201. can you show your formula of converting of counts to ml/minute or hour as u did in your video.
    because rpm=rpmcoun*60/4.5 does not work correctly.

    • Arvind Sanjeev
      Reply

      Please check the source code on the final step of this tutorial.

  • amarmehdi
    Reply

    sir, you said that solenoid valves control the flow but you did not use any solenoid valve in your video.
    i,m asking about open and close valve function that you use in your code.for what purpose they are used.thanks

    • Arvind Sanjeev
      Reply

      Please highlight the code you want me to explain, I dont think I have used any open/close valve commands there.

  • Chuck Colby
    Reply

    hey Arvind. I have been reading your work and find it interesting for sure. I’m looking to measure the volume of air flow through a tube of air flow traveling at 15 LPM. The goal is to control a pump to allow “x” amount of air through the pipe and then shut off the pump. Why i can calibrate the pump and control the time that the pump runs, then infer the air volume from those numbers, the pumps are not consistent and don’t necessarily run at 15 lpm all the time. Therefore, the ability to actually measure the volume would be ideal.. Do you think your above project would measure air volume?

    best,

    Chuck

    • Arvind Sanjeev
      Reply

      Hi Chuck, Ideally it should. This sensor consists of a simple propeller like system, which turns when water/air hits its blades. Dont know about the accuracy, but the output from the sensor would be definitely proportional to the input air/water. Would suggest you go by with a trial & error approach.

  • irfan
    Reply

    sir, i read your code.plz can u elaborate the purpose of valve open and close.i didn,t understand it. my email id is irfan_385@yahoo.com

    • Arvind Sanjeev
      Reply

      Solenoid valves can control the flow of water based on the sensor data. Can you explain in detail what you want to know?

  • Ratnesh
    Reply

    Sir,
    Which Arduino you has used kindly tell me how to buy it in india. How you run the software on windows 8 to measure flowrate

    • Arvind Sanjeev
      Reply

      You can use arduino Uno. If you are new to arduino, please download this free eBook, it will help you: http://diyhacking.com

      • ratnesh
        Reply

        thank you sir. I am using water flow sensor model YF-S201 having a different calibration factor than yours.What modification is required in the code, also how one can measure rpm of the rotor.

  • yunus
    Reply

    hye..i want to ask.. i have problems with sensor flow rate.. i want to make the sensor have to dectect flow rate of fluid. if the sensor low than 2ml/min it will close the pump. did you have any idea or any link to refer

    • Arvind Sanjeev
      Reply

      You can use a relay to control the pump based on the output of the flow rate sensor (simple if/else statements).

  • dayang madsiana
    Reply

    hai, I want to ask about the program. How you program it?

    • Arvind Sanjeev
      Reply

      If you are new to arduino, you can check out this free eBook to help you on programming and getting started: http://diyhacking.com/
      The code for this project is available in this page itself :)

      • dayang madsiana
        Reply

        I am a student from Polytechnic Mukah Sarawak, Malaysia. Our project need this. Now my lecturer want me to build a circuit interface for each one of the sensor. We use 4 sensor, temperature sensor, flow rate sensor, water level sensor and pressure sensor. So, it’s all 4 circuit interface right. If you don’t mind to share any idea. This is my email dayang.madsiana@gmail.com, my wechat ID DayangM85.

        • Arvind Sanjeev
          Reply

          Hi, you can search online for arduino projects having these sensors. You can then integrate all the code together :)

  • Guillaume
    Reply

    I have a problem about the display of the flow, when i run your code there is no information printed on the serial monitor :/ do you know where is the mistake?
    I have the Water flow sensor yf-s201 and im connected on the good COM (COM17 for me)
    And sorry for my english i’m french ;)

    • Arvind Sanjeev
      Reply

      I think there might be some connection problem. Why don’t you try printing something on the serial monitor and check.

  • Viswa Devan
    Reply

    Hi Arvind,

    Can i know what is your sensor output?
    Can i try out the code with using a 4mA-20mA output from sensor?

    I’m currently doing a project where i can measure air flow so i can estimate my savings i’ll get by savings air consumed by my tool.

    Thank you very much…

  • Shaun Reed
    Reply

    Hi Arvind,

    Your code works great! While I’m measuring flow, I activate my valve, which disrupts the flow measurements. I think this is due to the interrupt of the flowmeter being disrupted. As you suggested above, how do I modify your program to use a digital pin, say pin 4 or 7, instead of pin 2.

    Thanks,
    Shaun

    • Arvind Sanjeev
      Reply

      Hi Shaun,
      I fail to understand how the activation of a valve affects the interrupt? Also, why don’t you try the other interrupt pin 1 (digital pin 3).

  • Fabio
    Reply

    Hi Arvind, I used your code on a Arduino nano 3.0 and it worked perfectly; after I used the same code on an Intel Galileo: the ide compiles the code but it isn’t able to read to measure from the sensor. Any suggestion?
    Thank you very much

    • Arvind Sanjeev
      Reply

      Hello Fabio,
      Have you used the same sensor on the galileo? And also check the wiring connections for it, and whether the pin number for the arduino program matches that of the galileo.

  • Ali
    Reply

    good day Arvind and thanks for this

    the displayed out put is ony line of squares, unreadable simples.

    what is the port setting you used?

  • Sukanya
    Reply

    Great Aravind!
    Can you please help how to use this code in ARM cortex3.This is little urgent for me.
    Thanks in advance.

    • Arvind Sanjeev
      Reply

      Hello Sukanya, you would probably have to port the corresponding functions used here. You just need to write a piece of code that reads the digital data from a pin on your controller.

  • Ali
    Reply

    Hi Arvind Sanjeev
    do I need to attach any thing to the arduino except the wire connection of the sensor and the labtop?

  • Sean
    Reply

    Hey your code worked great. I appreciate it. If i wanted to output the data to .csv file how could do that? instead of a com monitor.
    Thanks for the help

    • Arvind Sanjeev
      Reply

      Hello Sean, if you want to output your data to a csv file, I would recommend writing a python script which reads values from the serial port (Arduino) and writes it into a text file in the form of CSV.

  • JV
    Reply

    Hi Arvind Sanjeev,
    can I use multiple flow meter in one arduino? what will be the configuration of connections? what will be the configuration on the program that you used?

    • Arvind Sanjeev
      Reply

      Hello, this program uses the arduino external interrupt pin (digital pin 2) and since there are only 2 interrupts on the arduino, you can use two of these sensors for this program at the same time. However, you can also edit this program to add the desired number of sensors by simply connecting them to the digital pins.

  • Ashok
    Reply

    hi Arvind sanjeev,
    how to convert the code to Raspberry pi? Please help me

  • Donald
    Reply

    Hey, thanks for the video! I am using the adafruit sensor, but when I used your code, there is no printed information on the screen. Do I need to change some settings?
    Thanks!

    • Arvind Sanjeev
      Reply

      Hello Donald, can you please share the link to your sensor? I am using another China made sensor for measuring the flow rate.

  • Faiz Wahyudi
    Reply

    please check baudrate setting.

  • Syazwan
    Reply

    Good days sir.. I try to upload the sketch to my arduino UNO and run it. I can’t find the result as what you get sir. Im using G1/4 Flow Rate sensor.. Can you help me..

pingbacks / trackbacks

Leave a Comment

+ 63 = 66

Start typing and press Enter to search