Introduction: How to Use Water Flow Sensor - Arduino Tutorial
In this tutorial you will learn how to use one water flow sensor with an Arduino board.
The water flow sensor consists of a plastic valve body, a water rotor and a hall-effect sensor. When the water flows through the rotor, rotor rolls and the speed of it changes with a different rate of flow. The hall-effect sensor outputs the corresponding pulse signal.
This type of sensor can be found on different diameters, water pressure (MPa) and flow rate (L/m) ranges. Make sure to select one that will cover your needs. The sensor that I have it has 20mm diameter, <1.75Mpa water pressure and ~30 L/m flow rate range.
In this tutorial we will use the serial monitor for printing the water flow rate in liters per hour and the total of liters flowed since starting.
So let's get started!
Step 1: What You Will Need
For this tutorial you will need:
- Arduino uno
- Water flow sensor
- 3 breadboard cables
Step 2: The Circuit
The connections are pretty easy, see the above image with the breadboard circuit schematic.
Step 3: The Code
Here's the code, embedded using Codebender!
Try downloading the Codebender plugin and clicking on the "Run on Arduino" button to program your Arduino board with this sketch. And that's it, you've programmed your Arduino with this sketch!
You can keep playing with that by clicking the "Edit" button and start making your own modifications to the code. For example you can change in the line 58 the "1000" ms delay time.
Step 4: Serial Monitor
Press the connect button below to start the serial communication.
Connect your sensor with your water tap, or just blow on it.
Note: The back side of the sensor show with one arrow the correct flow side.
Step 5: Well Done!
You have successfully completed one more Arduino "How to" tutorial and you learned how to use the water flow sensor.
I hope you liked this, let me know in the comments.
There will be more of them, so make sure to click Follow button!
Find more useful Arduino Tutorials here
49 Comments
3 months ago
someone plz help to connect with lcd
4 months ago
Thank you for your superb tutorial. Your sensor seems to be YF-S201 but I only had a similar shaped sensor of model: YF-S402B. I changed the calibrationFactor to 35 and it gives pretty good flowMilliLitres and totalMilliLitres.
Question 8 months ago
Do you have the code listing any other way? The codebender window cuts off the entire ISR. I don't want to directly program my Arduino as I have to build this into a larger program I just want the code as example.
1 year ago
You don't need to use interrupt you can sample with a while loop using any pin.
It may be simpler to do so. Especially if you connect to a pin that doesn't support intterupts.
I sample just half a second :
pulseCount = 0; // reset pulse count
flowSensor = 0.0; // reset flow senosr value.
oldTime = millis(); // gets the current time before the sample loop
initialFlowValue = digitalRead(flowSensorPin); // gets the initial posostion of the flow sensor
sampleTime = oldTime + 500; // sets the sampling time as 0.5 second.
while (oldTime < sampleTime) { // loop to sample the flow sensors values
oldTime = millis(); // gets the current time during the sample loop
currentFlowValue = digitalRead(flowSensorPin); // sampling flow sensor
if (initialFlowValue != currentFlowValue){ // if flow sensor value has changed from say high to low or low to high.
pulseCount++; // increment pulseCount
initialFlowValue = currentFlowValue; // set the initial flow value to be the same as the current value.
}
}
flowSensor = (2 * pulseCount) / calibrationFactor; // number of pulses in half a second, so twice that for a minute divided by the calibration factor
1 year ago
Thank you for this great article--it's been helpful but my water flow project has an added dimension; I need to incorporate code that when over a period of time, e.g 100,000 ms, if there is no flow, then an analog pin on the UNO is given a HIGH (if no_flow==true).
Any suggestions?
Thanks again.
Tip 1 year ago on Step 3
Hi,
Used the code and ran some test on the pulseCount value. This might overflow when byte datatype is used. Using volatile int there is no risk to that.
Question 2 years ago on Step 2
Where is the circuit diagram?
2 years ago
Anybody can help me. I try to combine this code with other sensors code. And it all end up sayin that the pulseCounter are not declare
Reply 2 years ago
You probably missed the last lines of code where the function is defined.
Question 3 years ago
Hi, how did you calculate the 4.5 for the calibration factor? I have purchased a slightly different sensor (as it better fitted the requirements for my project) but it's showing me moving thousands of litres? I can use a measured amount (I.E. fill a 2L water bottle and count the pulses) but this seamed a little mad? Any help would be massivly appricated!
Question 3 years ago on Step 3
This code are not give the output for nodemcu .What is the reason ?
Answer 3 years ago
It's taken me a couple of weeks to rebuild this so it works with a Node MCU, but in basic you need to look into how NodeMCUs process interupts, as they are a little different to the arduino, Hopefully I will upload the code once the projects done
Question 3 years ago
Project flow:
- water or any liquid flows through the sensor and quantity of water gets recorded
- now according to quantity of water flown there is price decided
- this price needs to be deducted by swiping rfid card
- ISSUE:
- i haven't been able to control interrupts
- my program calculates the water flown
- REQUIRED:
- edit code such that it as soon as water flows through it, it calculates and ask the user to swipe the card, and as user swipes an amount deducted and process continues.
https://arduino.stackexchange.com/questions/75761/arduino-project-interrupt-errorCheck out above link
PLEASE PLEASE HELP ME
Question 3 years ago on Step 3
I was stuggeling for quite a time to get my flow sensor working. Tried several tutorials and sample code's I found online, this one was the most promising, and most well detailed and explained. Nevertheless it did not worked, values vere keep showing as "0", nothing happened. Until I tried to changed this line:
"byte sensorInterrupt = 0; // 0 = digital pin 2"
to this:
"byte sensorInterrupt = 2; // 0 = digital pin 2"
Now, numbers are coming, it seems to work. I still don't understand why (*), and I am still not sure if the values are correct, but this I'll be able to check via mesurement (pour some liters thru the sensor and see, what happens)
Was there an error in the sample code?
Thanks again for the good tutorial I learned a lot from it !
Answer 3 years ago
I did the same but it still not working though. Value never change from zero....
Reply 3 years ago
its not working aswell to me, the value still same = 0
have information how to fix that?
3 years ago
Hi
Can this be used as a simple switch to turn on an extractor fan without having to use code and a board?
Thanks Damian
3 years ago
The code in this tutorial may be confusing for some. The signal pin from your flow meter should be assigned to the pin you define for sensorInterrupt on line 12 of the sample code. I was setting my 'signal pin' and defining the variable on line 13.
Question 4 years ago
would it be possible to create this code in javascript ?
Answer 3 years ago
Running Javascript directly on an Arduino? No. But you could look into a library like "Johnny-Five" which does allow you to run an application written in Javascript on your computer and have it issue run-time instructions to an Arduino over a USB connection. Probably not what you're looking for.