In this tutorial we will be hooking up a Flow Sensor to an Arduino Uno to measure liquid flow. This type of flow sensor is designed to measure the volume of liquid traveling past a given point, a great way to keep tabs on how much water your drip irrigation system is using, or any other project were the flow of liquid needs to be tracked. We like this basic flow sensors because of its relatively low cost and ease of use.
A Few Considerations:
Before we jump into getting this sensor hooked up there are a few points to consider when using it in a project.
• These are not able to monitor a flow of less than 1 liter per minute or in excess of 30 liters per minute.
• The sensor is rated to a Maximum of 2.0MPa (290 PSI)
How It Works:
The sensor itself is very simple inside; there is a small flapper wheel that spins as water flows past. A magnet on the flapper wheel triggers a hall effect sensor which sends a momentary pulse down the output wire with each revolution. Knowing that there are 450 pulses per liter, we can then determine the flow rate over time or the total volume that has passed… or both!
We are going to jump right in and set up the Arduino Uno and the breadboard right next to one another. The sensor works with anywhere between 5-18VDC but since we are working with 5VDC logic on the Arduino, we will just use the Arduino’s own 5V power by way of the USB port at this time.
Start by connecting one of the jumper wires from the 5V pin on the Arduino and running it over to the positive rail on the side of the solderless breadboard. Next, run a wire from the Ground pin on the Arduino over to the negative rail on the solderless breadboard.
We now have power on the breadboard!
This particular sensor has a nice long wire harness complete with a connector. We do not happen to have the connector laying around but luckily we can use our Male/Male Prototyping Wires to connect this sensor to the breadboard. Alternatively, any long 0.100″ pitch breadboard compatible header pins could also be used. We chose this method as it is much easier to visually follow the wires.
The harness itself has a Red, Yellow, and Black wire. We know from the product page that the red wire is a power wire, the yellow wire is the output wire for the sensor, and the black wire is a ground.
Next we are going to use a 10K Ohm resistor (Brown, Black, Orange) as a pull up resistor. The pull up resistor prevents a situation where the Arduino digital input pin ends up floating (think of this as the input not definitively being on or off). When an input is floating it may hold the last value, it may flip between off and on, quite random – generally not a good thing when we are trying to tell if it is on or off!
Bend the legs of the resistor and placed it between the positive 5V rail on the breadboard and a row of pins. We are also going to go ahead and extend the sensor’s wire harness by plugging jumper wires into the connector.
In the last step we extended the sensor’s wire harness – we can now plug these jumper wires into the breadboard. The Black wire is the sensor ground and should be connected to the negative (ground) rail on the breadboard. The Red wire should be connected to the positive 5V rail to give the sensor power. The yellow wire should be plugged into the same row as the pull up resistor we added last step – this is the output from the sensor.
We are just about there!
We have one last wire to add – this connects the pull up resistor and the sensor output to the Arduino’s digital input. In this example we will be using an interrupt pin so we will need to use pin 2 on the Arduino Uno. If you are using a different Arduino please consult this table to see what pins are available!
Before we give the Arduino power it is always a good idea to go over all of the connections to make sure there are no wires in the wrong spot – sometimes that can make for a very expensive mistake!
One way to avoid this problem is good wire color discipline. In other words, decide on a purpose for each color of wire and stick to them! In this example all 5V power are red wires, all grounds are black wires, and yellow are signal wires. This way, if you ever see a red wire going to a black wire you will know right away that something isn’t quite right!
void setup() {
// put your setup code here, to run once:
}
void loop() {
// put your main code here, to run repeatedly:
}
Now that we have finished with the hookup we need to start writing some code. We will be using the Arduino IDE, this is available from https://www.arduino.cc/en/Main/Software
We will start with the “BareMinimum” sketch found by clicking “File” and selecting Examples / Basic / BareMinimum. This sketch is a great starting point as it includes the Setup and Loop functions – we will write the rest!
We know that this sensor will output a pulse to Arduino pin 2 every time the flapper rotates and every and that every rotation means 2.25mL of fluid has passed the sensor so calculating the flow rate is really just a matter of counting the revolutions per minute and multiplying by 2.25mL .
So, what is the best way to count RPM with an Arduino? In this case the interrupt pin is going to be very useful. Interrupts have a very appropriate name – they allow you to perform a task (run a segment of code) the moment a signal is received, meaning they are great when you are trying to count pulses from a sensor.
Using interrupts is easy – lets get to it!
We are starting with the BareMinimum Sketch found in the IDE, it should look something like this:
void setup() {
// put your setup code here, to run once:
}
void loop() {
// put your main code here, to run repeatedly:
}
So first we will need some variables to hold values:
int flowPin = 2; //This is the input pin on the Arduino
double flowRate; //This is the value we intend to calculate.
volatile int count; //This integer needs to be set as volatile to ensure it updates correctly during the interrupt process.
void setup() {
// put your setup code here, to run once:
}
void loop() {
// put your main code here, to run repeatedly:
}
The volatile integer “count” is important as it will be where we store the number of pulses during each second we test. The volatile part may be new to many of you, and it is there to ensure the variable updates correctly during the Interrupt Service Routine. Aside from that, it acts just like any other integer. We will want it to increment every single time there is a pulse received which means we need to create a new function that the interrupt will run when a pulse is received:
int flowPin = 2; //This is the input pin on the Arduino
double flowRate; //This is the value we intend to calculate.
volatile int count; //This integer needs to be set as volatile to ensure it updates correctly during the interrupt process.
void setup() {
// put your setup code here, to run once:
}
void loop() {
// put your main code here, to run repeatedly:
}
void Flow()
{
count++; //Every time this function is called, increment "count" by 1
}
The “++” following the variable means every time the program runs this line it will add 1 to that variable – great for counting!
The interrupt function will be calling the “Flow” function so lets go ahead and add the interrupt to the setup section of code:
int flowPin = 2; //This is the input pin on the Arduino
double flowRate; //This is the value we intend to calculate.
volatile int count; //This integer needs to be set as volatile to ensure it updates correctly during the interrupt process.
void setup() {
// put your setup code here, to run once:
pinMode(flowPin, INPUT); //Sets the pin as an input
attachInterrupt(0, Flow, RISING); //Configures interrupt 0 (pin 2 on the Arduino Uno) to run the function "Flow"
}
void loop() {
// put your main code here, to run repeatedly:
}
void Flow()
{
count++; //Every time this function is called, increment "count" by 1
}
So that last little bit may require a little more explanation: The first thing we should be clear about is on the Arduino Uno pin 2 is Interrupt 0, so both lines of code we have added are referring to the same physical pin on the Arduino. Basically the pin needs to be set as an input before setting up the interrupt.
On the next line we are configuring the interrupt by using “attachInterrupt”. To better illustrate how this line works, think of it as this:
attachInterrupt(interrupt number, the function you would like to run when triggered, what you would like to set as the trigger).
So we are using interrupt 0 to trigger “Flow” when the pin changes from low to high (when a pulse from the sensor arrives). The complete list of triggers are available on the Arduino Learning Center
Time to start writing the main code that will run continuously in the loop:
int flowPin = 2; //This is the input pin on the Arduino
double flowRate; //This is the value we intend to calculate.
volatile int count; //This integer needs to be set as volatile to ensure it updates correctly during the interrupt process.
void setup() {
// put your setup code here, to run once:
pinMode(flowPin, INPUT); //Sets the pin as an input
attachInterrupt(0, Flow, RISING); //Configures interrupt 0 (pin 2 on the Arduino Uno) to run the function "Flow"
}
void loop() {
// put your main code here, to run repeatedly:
count = 0; // Reset the counter so we start counting from 0 again
interrupts(); //Enables interrupts on the Arduino
delay (1000); //Wait 1 second
noInterrupts(); //Disable the interrupts on the Arduino
}
void Flow()
{
count++; //Every time this function is called, increment "count" by 1
}
OK, half way there. Since the loop runs over and over again we need reset our variable “count” to 0 at the beginning, we do not want the number of pulses from the last loop carrying forward. The next line enables the interrupts, meaning we now start counting how many pulses the sensor sends out. On the following line we delay the code for 1000ms (1 Second) to give us time to count pulses and on the last line we disable the interrupts to stop counting. Any pulses from the sensor are ignored before the interrupt is enabled and after it is disabled so with this code we will take a very accurate count of pulses over a period of 1 second.
Let’s do some math to turn the number of pulses per second to a more useful unit of measure:
int flowPin = 2; //This is the input pin on the Arduino
double flowRate; //This is the value we intend to calculate.
volatile int count; //This integer needs to be set as volatile to ensure it updates correctly during the interrupt process.
void setup() {
// put your setup code here, to run once:
pinMode(flowPin, INPUT); //Sets the pin as an input
attachInterrupt(0, Flow, RISING); //Configures interrupt 0 (pin 2 on the Arduino Uno) to run the function "Flow"
}
void loop() {
// put your main code here, to run repeatedly:
count = 0; // Reset the counter so we start counting from 0 again
interrupts(); //Enables interrupts on the Arduino
delay (1000); //Wait 1 second
noInterrupts(); //Disable the interrupts on the Arduino
//Start the math
flowRate = (count * 2.25); //Take counted pulses in the last second and multiply by 2.25mL
flowRate = flowRate * 60; //Convert seconds to minutes, giving you mL / Minute
flowRate = flowRate / 1000; //Convert mL to Liters, giving you Liters / Minute
}
void Flow()
{
count++; //Every time this function is called, increment "count" by 1
}
Those last 3 lines were just a unit conversion.
Pulses per second * 2.25 milliliters per Pulse = milliliters/Second
mL/Second * 60 Seconds = mL/Minute
mL/Minute / 1000 = Liters/Minute
So flowRate = Liters/Minute … Perfect! Now we just need to write it to Serial so we can actually see the data:
int flowPin = 2; //This is the input pin on the Arduino
double flowRate; //This is the value we intend to calculate.
volatile int count; //This integer needs to be set as volatile to ensure it updates correctly during the interrupt process.
void setup() {
// put your setup code here, to run once:
pinMode(flowPin, INPUT); //Sets the pin as an input
attachInterrupt(0, Flow, RISING); //Configures interrupt 0 (pin 2 on the Arduino Uno) to run the function "Flow"
Serial.begin(9600); //Start Serial
}
void loop() {
// put your main code here, to run repeatedly:
count = 0; // Reset the counter so we start counting from 0 again
interrupts(); //Enables interrupts on the Arduino
delay (1000); //Wait 1 second
noInterrupts(); //Disable the interrupts on the Arduino
//Start the math
flowRate = (count * 2.25); //Take counted pulses in the last second and multiply by 2.25mL
flowRate = flowRate * 60; //Convert seconds to minutes, giving you mL / Minute
flowRate = flowRate / 1000; //Convert mL to Liters, giving you Liters / Minute
Serial.println(flowRate); //Print the variable flowRate to Serial
}
void Flow()
{
count++; //Every time this function is called, increment "count" by 1
}
In the Setup we are starting the Serial connection at a rate of 9600 Baud – the default speed. At the bottom of the loop we are printing a line on the Serial connection containing the result of our math. All Done!
Now that all of the code has been written it can be uploaded to your Arduino! Click “Upload” button in the top left corner of the Arduino IDE and it should upload without any issues. Next, click the “Serial Monitor” button in the top right corner (it looks like a magnifying glass). After a few seconds you should start to see a stream of data appear in the window – that is your flow in Liters/Minute.
84 thoughts on “Using A Flow Sensor With Arduino”
Bagus
//Start the math
flowRate = (count * 2.25); //Take counted pulses in the last second and multiply by 2.25mL
flowRate = flowRate * 60; //Convert seconds to minutes, giving you mL / Minute
flowRate = flowRate / 1000; //Convert mL to Liters, giving you Liters / Minute
the value 2.25 obtained from where ?
Chris @ BCR
The 2.25 is derived from the datasheet – each pulse from the Flow Sensor is roughly 2.25mL of water
Drani
What if you want to print the output on an LCD? How do you do that?
mike87
use the LiquidCrystal library in the IDE
daniel
i want to measure number of liters and not liters/min and also calculate the price based on price/liter.what should the code look like?
Chris @ BCR
The number of liters would actually be simpler
flowRate = (count * 2.25); // gives you the total during the period of time measured
remove the next two lines and just divide flowRate by 1000. You will now have the number of liters. You can then multiply by the price per liter. You may want to adjust the duration the measurement is done.
Jason
Why not use pinMode(flowPin, INPUT_PULLUP) instead of a 10 ohm resistor on the signal wire?
Chris @ BCR
You most certainly can use an internal pullup on the Arduino to simplify the wiring but we use a physical resistor as it is much easier to explain the basics of how a pull-up resistor works when you have to connect the wires yourself.
Stefan
Hi,
I used ur setup and sometimes interrupts are lost
how does that come?
Any explanation?
The datasheet says 435 counts per liter water
It is sometimes more if gas is in the pipe, but how can it be less than the counts?
Chris @ BCR
If the flow of liquid is too slow, this type of sensor will not correctly measure what is passing by.
Vasekdvor
Is it possible to use Pull Down 10K ohm resistor ?
What if i use Pull down resitor 10K ohm and set attachInterrupt
to trigger when input pin is FALLING ?
rishii
Hi. I need to use water flow sensors for science fair and I was wondering if its possible to connect multiple sensors?
Chris @ BCR
Yes, you most certainly can but you will be limited by the number of interrupt pins on your microcontroller.
José Lobo
Hi, great explanation about using interrupts. I was looking for such a project for my hydroponic system. Just two doubts: Can i use this sensor to measure 100ml of the nutrient solution?
– I would like to put a set point to stop water flow at a certain value. For example, valve opens, water flows and passes sensor. After 100ml, valve closes. Is it possible??? Sorry… , Arduino newbie 🙂
Thanks, José.
Theo S.
You could definitely do it that way, but I would use peristaltic pumps for dosing accurate volumes of nutrient solution.
Mehdi
As Jose said, this is one of the best tutorials about getting flow sensor pulses. I study interrupt function in arduino learning center.
I am going to use ESP8266 instead of arduino’s microcontrollers, but there is no mention about ESP’s pin to use in attachInterrupt() function.
Should I use ESP8266 digital pin (GPIO2) as input pin and in attachInterrupt function? That is:
flowPin = 2;
…
attachInterrupt (2, Flow, RISING);
Is it correct?
Theo S.
I believe that you can attach an interrupt to any digital pin on the ESP8266. I’ve definitely seen it done with GPIO2, so you should be good.
Gilbert Hersschens
Hi,
I’m planning to use a similar flow sensor with ESP. But ESP runs on 3.3V while pretty much all sensors are rated for 5V or higher.
Did you try the sensor with 3.3V?
Erik
I don’t understand something here, you say that the flow sensor is 450 Pulses-per-liter (or 450p/1000ml), and then you say the “sensor will output a pulse every time the flapper rotates and every and that every rotation means 2.25mL” — this doesn’t add up. 450p/1000ml = 0.45ml/p. Right? Where do you get 2.25mL that would be 5 rotations according to your data.
Erik
I flipped the terms and realized my error. 1000/450= 2.25mL
KaisarGreat
Hey There 😀 I’m working on my system i’m using water flow and solenoid valve .. is it possible to put limit in the water that flows in the pipe Example i wanna set a 400ml set point or limit .. if water flow sensor reads that its already 400ml of water has been pass it will trigger the solenoid to close the pipes .. sorry for my bad english .
Rohan
Hey, i am working on a similar project. How did yours go?
Larry Fostano
I am really new to this, tried to follow the above example .I wanted to do something useful so as to learn how to apply this to everyday living.Compiled code, no problem but in serial with nothing (no liquid running through sensor) I still get a reading. My project is to use this to monitor amount of water that is being used in my well. Can you tell me where I went wrong.thanks just a newbie here.Still don’t have a concept on how or how to apply a code to every day living.
void setup() {
// put your setup code here, to run once:
pinMode(flowPin, INPUT); //Sets the pin as an input
attachInterrupt(0, Flow, RISING); //Configures interrupt 0 (pin 2 on the Arduino Uno) to run the function “Flow”
Serial.begin(9600); //Start Serial
}
void loop() {
// put your main code here, to run repeatedly:
count = 0; // Reset the counter so we start counting from 0 again
interrupts(); //Enables interrupts on the Arduino
delay (1000); //Wait 1 second
noInterrupts(); //Disable the interrupts on the Arduino
//Start the math
flowRate = (count * 2.25); //Take counted pulses in the last second and multiply by 2.25mL
flowRate = flowRate * 60; //Convert seconds to minutes, giving you mL / Minute
flowRate = flowRate / 1000; //Convert mL to Liters, giving you Liters / Minute
Serial.println(flowRate); //Print the variable flowRate to Serial
}
void Flow()
{
count++; //Every time this function is called, increment “count” by 1
}
Jaeson
Great sketch. Thanks. Ive been trying to incorporate a data logger to this sketch without much luck. Any chance you could point me in the right direction? I have a sketch that does both but measure total volume rather than flowrate. I can only bash my face on the keyboard for so long.
Many thanks
Theo S.
Where are you trying to log the data? SD card?
quangckd0703
Great! Thank you very very much..
hohm
Hello I shall like knowing how to use the water flow sensor to command(order) a pump which would stop in the quantity of deliberate water
For example (10 ml) and this to water(spray).
Beforehand thank you
kuldeep singh
i am working on project something like this but i am using water meter which is connected with IZAR PULSE may be for this i need pulsein function but i didn’t get the result what i want can you please help me regarding this
thank you
Matías
Hello! Thank you very much. I managed to walk the sensor and write on an LCD the instantaneous flow.
Now I would like to add the measurement of the VOLUME of water that was circulating and I can not think of what line of code to add.
If someone can help me, welcome. Thank you very much.
AJAY
how can i print how many liters passed in the sensor and how to print it on LCD?
R.Arun
I wanted to measure volume of fluid (like grease or peanut butter) which is having the viscosity of 2,90,000 cps flowing through a pipe.
Theo S.
Ouff, I’m not aware of any cheap sensor that could measure such a thing. Maybe try measuring the initial and final level of fluid (if you have access to it) and get your result from the difference.
sangeetha
I have problem in uploading the code.The code is compiling but not uploading.Could u please tell me what should I do inorder for the code to upload.
her0
Hi there, i was wondering if we could make the connection that goes to pin 2 on another pin, since the LCD display has to be connected in the same pin. Thanks!
Theo S.
On an Arduino Uno, you can attach an interrupt to digital pins 2 and 3. So your other choice is pin 3. But you should be able to use any pin you want for the LCD — so my suggestion would be to change the connection for your LCD.
Sanjukta Bagchi
Can I add another one sensor in this project? If yes then how?
Mattie Mckenzie
Not terribly useful having a 1s delay if the sketch needs to do anything else, this is blocking code. Designed this way, we now need another Arduino SPI’d or I2C’s in to “work” and use this Arduino solely as a counter with 1s refresh ?
Chris @ BCR
Hi Mattie,
You are correct about blocking code, nothing else will happen during that delay… but I think you are missing the point of the tutorial. This is not meant to be a best practice in code – this tutorial is focused on how to get a flow sensor hooked up to the Arduino and then some extremely simplistic code to show that it works. There are much better ways to handle waiting for time to pass (such as simple counter)… but that isn’t the point here. Interrupts are complicated enough for beginners as is… so we don’t really need to be explaining the millis() function and conditional statements, all at the same time.
Ivan
If I wanted to have a set volume that I wanted to send through the flowmeter, would I have to put the counter in a for loop to reach the desired pulses then perform another action once that for loop was finished? I am new to arduino and this will be my first project.
Thanks
Theo S.
I’d use a while loop.
while volume < threshold
{keep counting pulses}
john
How can I calculate total flow ? Could you help me about code ? And I want to reset total flow by using a button. How can I implement it into code ? Have a great day
Kathryn
Hi, I’m working on developing a project where I use a water sensor meter such as this to then trigger something like an LED light or a simple buzzer – basically, we’re trying to do an experiment where we push bubbles through a water tube and use the change in pressure, i.e. the bubble, to trigger an output. Would that be possible with this setup? How would I go about that, and do you have any output recommendations?
Theo S.
I don’t think that would be possible with this sensor. There might be a rapid change in flow rate when a bubble passes through but that will be hard to detect in my opinion. Definitely not doable with the current code.
Maybe try using a pressure sensor instead.
Saman
Hi
How can I measure the water velocity?
Chris @ BCR
Good question – you could try measuring the circumference of the pin wheel and counting the time it takes to make a full revolution. Time (revolution time) and Distance (circumference) will give you Speed.
mike87
Haha i always wanted a hose speedometer “Your Water Speed is: “
Theo S.
In general, you can use the following formula:
Q = A*v where
– Q is the flow rate (e.g. L/min)
– A is the cross-sectional area of the pipe (e.g. mm^2)
– v is the average velocity of the fluid (e.g. m/s)
You’ll need to match your units but you should be able to calculate the speed of your water using this formula.
WEXLER
Hi
I want to build this device, and I would like to be ensure that the count is not lost if the power is lost.
I want to write to EEPROM when the Arduino detects power down – I’m not sure it is ok… because EEPROM cycle use is limited.
Could you help me?
Chris @ BCR
We don’t provide help with custom projects, only questions directly relating to the tutorial.
Theo S.
It can certainly be done but you’d need a backup power source to write to the EEPROM only when it detects a power down (unless the power down is planned, like pushing a button — then you can write a shutdown routine that writes to the EEPROM before it powers down the chip; you’ll probably need to use additional electronic components too).
Like you say, there is a limited number of writes that each EEPROM address can take so if this “power down” would happen often, I’d suggest using data logging instead (saving data to and SD card for example, and then retrieving it from there).
Elpiniki Marouli
Hi ,
Trying to compile the code i get errors such as PARSE ERROR: ‘Flow’: that variable or name has not been previously declared.If i set the flow iam getting error for attachinterrupt etc
iam testing using uno ardusim
int flowPin = 2; //This is the input pin on the Arduino
double flowRate; //This is the value we intend to calculate.
volatile int count; //This integer needs to be set as volatile to ensure it updates correctly during the interrupt process.
void setup() {
// put your setup code here, to run once:
pinMode(flowPin, INPUT); //Sets the pin as an input
attachInterrupt(0, Flow, RISING); //Configures interrupt 0 (pin 2 on the Arduino Uno) to run the function “Flow”
Serial.begin(9600); //Start Serial
}
void loop() {
// put your main code here, to run repeatedly:
count = 0; // Reset the counter so we start counting from 0 again
interrupts(); //Enables interrupts on the Arduino
delay (1000); //Wait 1 second
noInterrupts(); //Disable the interrupts on the Arduino
//Start the math
flowRate = (count * 2.25); //Take counted pulses in the last second and multiply by 2.25mL
flowRate = flowRate * 60; //Convert seconds to minutes, giving you mL / Minute
flowRate = flowRate / 1000; //Convert mL to Liters, giving you Liters / Minute
Serial.println(flowRate); //Print the variable flowRate to Serial
}
void Flow()
{
count++; //Every time this function is called, increment “count” by 1
}
liv_nicole01
What program did you use to count the pulses from the flow sensor?
Theo S.
Not sure what you mean by this. The microcontroller (Arduino) counts the pulses. The microcontroller is programmed using the Arduino IDE. Then if you want to see the values, you can open the Serial Monitor in the Arduino IDE. There you will the the value of flowRate printed once per second (that’s what the “Serial.println(flowRate)” line does).
Shashi Kiran
Thanks for a wonderful explanation. I have done the same with Wemos and have used the 3.3v pin ( it is similar to ESP8266 ) . My problem is I have connected this to a wall socket ( we have 240v) with 4 switches, and the microcontroller is connected to the wall socket with a USB adapter that provides 5v.
If there is no activity on other switches then the water flow sensor accounts the pulse properly.
However, if I turn on or off one of the switches on the wall panel ( like my laundry room light) then it triggers false pulses to the USB adapter that is connected to the Microcontroller.
I can replicate it and then suddenly the ( False ) pulses are generated and sounds like the water is flowing – this is incorrect.
Trying to seek resolution. If you know how I can catch this – would appreciate a reply.
Shashi Kiran
I added the Pull down resistor 10k between the Vcc and the signal as you suggested, Still I am seeing some pulses when I turn on off switch next door to where the microcontroller is connected via the USB adapter.
Alim Huseynov
The pull-up resistor keeps the Pin HIGH and for changing the state it should go to ground or FALL … why we set the interrupt to RISING? should not it be FALLING?
Front Desk @ BCR
Each revolution you will see the voltage on the sensor output transition from HIGH to LOW and back to HIGH again – so you can monitor the falling or rising event – you will still get the same count
surya
Helo sir can i get a code that helps the flow sensor to read the flow of pressure passing through the valve ( i mean like if the valve is completely open the sensor will detect full pressure and give out a reading of 100% likewise if its half open the sensor will detect from the pressure and give out a reading for 50% also respectively for 75% and 25%.
Barah
Could I use this code for my research purpose? This actually is a great tutorial. Appreciate it.
Chris @ BCR
Absolutely – hopefully it helps!
Random Dud
Do I need to use a interrupt in the code?
Kyle O'Callaghan
It’s great to find such a well explained tutorial!
Just curious, regarding the interrupt configuration “attachInterrupt(0, Flow, RISING)”
Should the instead be
“attachInterrupt(0, Flow, FALLING)”
as the flow pin is pulled up to a ‘high’ state with the resistor?
Eric L
I thought the delay() won’t work while interrupts are enabled?
Ted Thisius
I have concerns about using delays with interrupts also. The Millis() function may be a better choice rather than Delay.
Ted Thisius
I also have concerns about using Delay with interrupts.
Some tutorials advise never using Delay but using Millis() in a loop of its own to “waste” time. The loop compares the current time with the starting time and exits when it reaches a set value.
Chris @ BCR
Ted, You are correct in a sense. However, this is a simple tutorial giving a basic overview of how a sensor can be connected and used. Delay is simple and it does exactly what it says it does – writing better non-blocking code is a concept that can be learned later and isn’t terribly relevant to getting the basic sensor working 🙂
Kasper Martensen
Is there a way to mesuare how long the sensor has run, i don’t need to know how much it has run, but i need to know it has run over 5 min, and then it need to send a signal to a relay, so close a valve.
I have looked, but i can’t really find anything about it, or is there any other way to do this?
it needs to be in a pipe, where the always i water, but i need to know if the water is flowing.
Chris @ BCR
There are a lot of ways to do that. Using the tutorial code just check to see if the flow rate is above 0. Since it is running at a set interval, just add up the seconds until you get to your run time limit. May want to include a buffer value as there can be a little drift in the sensor. Rough idea:
[cpp]
if (flowRate > (0 + flowBuffer))
{
//System Running
if (flowTime > flowTimeLimt)
{
//Ran the full time, so trigger your relay here
//Reset the clock
flowTime = 0;
}
else {
//Hasnt reached the time limit so increase your timer count
flowTime++;
}
}
else {
//System Not Flowing Water
if (flowTime > 0)
{
//Your system stopped flowing water and didn’t run a full duration
}
else
{
//System Idle
}
}
[/cpp]
Kasper Martensen
You just solved a big problem for me, with a very simple way, thanks man, i think i may have looked at it like it was way more complicated than it really is, again thanks 😀
Jonathan Simpson
Hi, great info here, I want to connect 2 flow sensors to my board. When I enable my interrupts only the last interrupt get the pulse ?
Noee
Hey! I was wondering if we could do this with air flow? Could we juste reajuste the *2.25?
Orlando E
Hello,
First of all I want to say thank you for your thorough explanation, and it was easier to understand thanks to the steps you split it. Just one observation, it is a little risk to assume 2.25 ml per rotation, it will depend on some other variables such as pressure, hose length and so on. What I’d recommend is to set your own conditions and measure the flow vs pulses (rotations), just taking a graduated flask and a chronometer; This way you will have your own ml/pulse.
Chris @ BCR
Thanks for the great feedback! Moving from an exercise in Arduino to a reliable control system would definitely involve some level of calibration – thanks for the suggestions on how to accomplish this!
Chris R Kilgus
Excellent article on how to use the flow sensor and some Arduino pointers.
Jon Cherba
I just purchased 2 Gredia G3/4″ flow sensors to use on my reef tank to monitor the return pumps. The info on the sensor says F=(5.5*Q) Q=L/min how do I modify your code to allow me to use this sensor properly? I’m trying to measure Gallons / min as well, is there an easy way to fix it to output that in the code? Your example was the only one I could find that actually worked. Thanks!
Dimuthu Madusanka
Hello friends, i want to anable a digital output high when flow rate is higher than some level. What is the code for that. I am new for arduino and i couldnt find any code like that.
Martin Larsen
How do you get the 2.25 ml per rotation?
1000 / 450 = 2.22.
I know that this is not a precision instrument, but why not use the correct factor? Perhaps 2.25 is prettier for humans to read, but the Arduino doesn’t care 😉
Chris @ BCR
Hi Martin,
Thanks for the note! We have seen the same sensor with both 450 pulses per liter and 2.25mL / pulse numbers thrown around. Ultimately, they are not going to be accurate to two decimal places just by their construction/design – so we haven’t worried too much about it. For higher accuracy using this sensor, the best bet would be to fill a known volume container at the flow rate your system normally operates at and count the pulses.
Sophia
This is an amazing tutorial! Very concise and easy to understand.
Do you have any tips for using multiple flowmeters? Can I simply have several sensors each sending signals to a different interrupt pin? What happens when several signals come in at once?
Thanks so much!
William @ BC Robotics
Thank you for the kind feedback! Using this method, you would need to use a different interrupt for each sensor. With the Uno there are only two interrupts, so that does present some challenges if you need more than two sensors.
To get around that you could go to a different controller with more interrupts or use a multiplexer to connect multiple sensors to one interrupt.
Jamie Bradbury
Hello,
I am designing a Point of Use sensor for rain water harvesting tanks in Tanzania. My team has decided to use an Arduino to monitor flow rate but is looking for a way to store the data as well. As a result, I was wondering if you could tell me how much data was generated for each minute of testing? That way I can understand what type of storage I should be looking into. I am not sure if this is something you know or not but I thought I would ask.
Geoffrey
Thanks for the article.
I am interested in measuring the flow through a heat exchanger attached to an engine and want to add an alarm function for low water flow.
Is there an addition to this code which would achieve this or am I better using more advanced code?
I would also like to display the flow rate on an LCD.
Chris @ BCR
You would want to add some sort of conditional statement in the loop
if(flowRate < someArbitraryValue) { //Do your alert here }
As for printing to a screen, figure out the screen you want to use and work with the library for the screen. Use that specific library's print to screen function in place of the serial.println in the existing code.
Etete
how can i use this for measuring air flow in vacuum system?