G1/2 water flow sensor with arduino

hello everyone.

I have been trying to hook up a G1/2 water flow sensor with arduino. I have connected the red wire to 5V output, the black wire to GROUND, and the yellow wire to analog pin 0.
I have been searchin the web for info, but I can’t find out whether i need to put a resistor in somewhere?

I would also like to ask if any of you have a sketch that measures either L/min or L. I feel somewhat lost at the moment, as i am quite new to arduino.

I would imagine that you need to use, analogRead(0) in my case, but i can’t seem to get any useable numbers.

best regards, Jens.

This sensor uses a hall device to output pulses, the rate of which are proportional to flow rate. (found by simply googling G1/2 flow sensor and then selecting the seedunio site)
I suspect therefore that you need to look at the input using a digin rather than analogue configuration and measure either timing between pulses or count number of pulses over a fixed period of time - say 1 minute

Ah, so I have to measure / count how many times the sensor turns on and off in fx. a minute. What commands can I use to do that?
Im sorry about the “dumb” questions, I am quite new to arduino.

best regards, Jens.

What commands can I use to do that?

use interrupts

Other device, same principle - ==> - Arduino Playground - HomePage -

Should provide enough to get something working, please let us know if it works and how you callibrated things...

I just got today a 3/4 inch version of the same thing I suspect. As the previous replies mention this is best read with an interrupt routine that counts pulses and measures frequency. If all you want is rate, and don't care about count. Then the pulseIn() function (which I just learned about from reading another post) would work well and be very easy.

Passing the pin you have hooked up to the flow sensor to it, try calling pulseIn() twice. The first time it returns, throw away the value, and immediately call it again so you can be sure of measuring an entire pulse period, and there you go. Now just convert the pulse period to the flow rate (which requires some sort of constant specific for your sensor).
My sensor data says pulse Freq = 5.5Q, where Q is the flow in L/min. Period is the inverse of frequency so after a little algebra ...
Q = 1/(period
5.5)
That works for the sensor I have anyway.

I saw some example code using interrupts somewhere, but I don't remember where. It's not hard, and best if you want volume and the flow rate may vary a lot. I'll probably write some code for it this weekend.

Thanks for the reply that helped alot :slight_smile:

I'll try and write some code, using the function you mentioned. I will let you know if i succed :slight_smile:

If you do write some code to your water flow sensor, i would really appreciate it, if you would show it

I think, however, I am starting to get a bigger understanding, but I am still new to arduino

Best regards, Jens

I've been searching around the web for quite some time now, but i can't seem to find any information on how to use the "attachInterrups" function. I have tried to use the "pulseIn()" function too, but i can't convert the data recieved to L/min.

Do any of you know of a guide on how to use Interrups to count pulse frequence?

Best regards, Jens
edit
I think I have attached an interrupt function now, but is there a way I can make the interrupt count pulses for a few seconds, before running the void loop()?

So far my code looks like this:
It is "nooby" but right now I just need to make it work. I'm also in doubt whether I need to attach the interrupt to "0" or "2", since the wire is connected to digital pin 2.

int sensorPin = 2;
int flow = 0;
int flow1 = 0;
void setup()
{
pinMode(sensorPin, INPUT);
Serial.begin(9600);
attachInterrupt(0, flow3, HIGH);
}

void flow3()
{
int flow1 = flow + 1;
}

void loop()
{
flow = flow1 / 7.5;
Serial.println(flow);
}

You're close. Attach the interrupt to pin 2, better, use the sensorPin variable.

Global variable flow1 should be declared volatile, as in the example here: http://arduino.cc/en/Reference/AttachInterrupt

Change the mode to RISING - you want the interrupt to trigger once on a pulse.

Your interrupt routine declares a local variable flow1, you want to refer to the global one - delete the 'int'.

Use delay(5000) or similar in setup after the interrupt is attached to let a few pulses get read before loop starts.

I've made a few changes to the sketch now, and I have tested it by measuring the flow of a water tab with a "measuring container"( I dont know the english word)

It is pretty accurate.

Is there a way to show the first decimal of the flow?

My sketch looks like this now: (I am sure it contains something it shouldn't, but it works)

int sensorPin = 2;
volatile int flow = 0;
volatile int flow1 = 0;
volatile int flowTime;
int flowattempt = 0;
byte second = 0;
void setup()
{
pinMode(sensorPin, INPUT);
Serial.begin(9600);
attachInterrupt(0, flow3, RISING);
}

void flow3()
{
flow1++;
}

void loop()
{
if (millis() % 1000)
{
if (second == 60)
second = 0;
else
second++;
}
if (second % 2)
{
flow = flow1 / 7.5;
}
delay(1000);
flowattempt = (flow1 /2) / 7.5;
flow1 = 0;
Serial.print("L/Min ");
Serial.println(flowattempt);
}

Best regards, Jens

Is there a way to show the first decimal of the flow?

float flowattempt = flow1 *0.066666667; // faster than dividing by 15
Serial.print("L/Min ");
Serial.println(flowattempt, 1); // the 1 is nr of decimals

Thanks :slight_smile:

Best regards, Jens

To make it even more precise use

attachInterrupt(0, flow3, CHANGE); // get twice as many pulses !!

and

float flowattempt = flow1 * 0.033333333; // adjust this one too

If you change the pin of the input do you still get good result? Because in my software I tried to vary the pin to pin 3, pin 4 and pin 7 but it does not read anything. Have anyone an idea why? On pin 2 it is working perfectly but I need to use 2 sensors for my project

Check out this library (and read the explanation): Arduino Playground - PinChangeInt

Thanks to everyone who put up such valuable info here.

Quick question
I am intending to attach 7 G1/2 water flow sensors to my arduino.
I only want a cumulative reading e.g. like a municipal meter on my raspberry pi web interface.

I don't require l/min just the volume of water used.

If I could get the leonardo to light a led every time 5 litres passed that would be perfect for the Rpi

I am led to understand that these Hall flow meters seem to have a high resolution and I can't find a pulses/litre specification for most of them.

Could a leonardo count 7 different pulses on 7 different pins , or would the leonardo processor explode?.
Max flow would be around 10 litres/minute