docwisdom
Posts: 4
Joined: Thu Mar 21, 2013 6:14 pm

Water Flow Sensors

Thu Mar 21, 2013 6:35 pm

Hello,

I am planning on using my Pi to automate my hydroponics setup a bit. I plan on having 4 water flow meters (http://www.seeedstudio.com/wiki/G3/4_Water_Flow_sensor) hooked up. One of the flow sensors measures the incoming flow on the main line to all 3 grow beds. This is mostly just for monitoring the health of the pump.
The system uses a fill and drain method that is controlled by a bell siphon. The other 3 sensors will be used at the drain of each bed to measure the time between cycles and the rate of the drain.

How should I go about doing the pulse counting? I plan on polling the input sensor every 30 seconds. I want the drain sensors to act in an interrupt fashion and start recording the pulses as soon as the first one is detected and stop counting when it reaches 0 again. All of this is going to be written to a mysql database onboard so I can graph the flow in jquery.

I have done a bit of searching around to see if people have had success doing flow sensors yet and I find bits and pieces, but nothing to really get me started. I found a Pi project for a kegerator, but it implemented the flow sensing yet.
I am new to Pi and mostly have experience in arduino. I was able to get the flow sensors working using the supplied code on the Wiki page linked above using my arduino.

http://www.raspberrypi.org/phpBB3/viewt ... 72&t=12453
https://kegbot.org/kegbb/topic/514/

User avatar
gordon@drogon.net
Posts: 1979
Joined: Tue Feb 07, 2012 2:14 pm
Location: Devon, UK
Contact: Website

Re: Water Flow Sensors

Thu Mar 21, 2013 9:01 pm

docwisdom wrote:Hello,

I am planning on using my Pi to automate my hydroponics setup a bit. I plan on having 4 water flow meters (http://www.seeedstudio.com/wiki/G3/4_Water_Flow_sensor) hooked up. One of the flow sensors measures the incoming flow on the main line to all 3 grow beds. This is mostly just for monitoring the health of the pump.
The system uses a fill and drain method that is controlled by a bell siphon. The other 3 sensors will be used at the drain of each bed to measure the time between cycles and the rate of the drain.

How should I go about doing the pulse counting? I plan on polling the input sensor every 30 seconds. I want the drain sensors to act in an interrupt fashion and start recording the pulses as soon as the first one is detected and stop counting when it reaches 0 again. All of this is going to be written to a mysql database onboard so I can graph the flow in jquery.
Doesn't look too hard - wiringPi has an ISR function that you can use to take interrupts from the sensor. You just need to count pulses in the ISR in almost the same way as that Arduino one is doing.

then all you need to do is check the nmber of counts since the last time you checked - the time interval can be anything as long as you know how long it was!

So at its simplest:

start = counter ;
sleep (30) ;
howMany = counter - start ;

then do the calculation for 30 seconds with howMany counts to work out the flow rate (or the water volume) and stuff it into the database.

You can run many ISRs using wiringPi.

The thing that might trip you up is that that sensor needs 5v and the Pi is 3.3v. You can use 2 resistors per pin to drop the voltage down to 3.3v or one of the many commercial devices to do it. (a 3.3K and 2.2K reisistor pair is easy to use if you're handy with a soldering iron, or even breadboard)

-Gordon
--
Gordons projects: https://projects.drogon.net/

docwisdom
Posts: 4
Joined: Thu Mar 21, 2013 6:14 pm

Re: Water Flow Sensors

Thu Mar 21, 2013 9:13 pm

Thanks very much Gordon. I had run across your site prior to my post but didn't see anything that had directly related. I will take a look at your suggestions and see how far I can get.

Cheers

docwisdom
Posts: 4
Joined: Thu Mar 21, 2013 6:14 pm

Re: Water Flow Sensors

Thu Mar 21, 2013 10:19 pm

Gordon,

Sorry for my novice questions, but is the interrupt going to be triggered with each pulse and I increment within that function or is the interrupt triggered on the first pulse and I need to do some other polling within the function to get my count? Your count example didnt quite click with me this is why Im asking.

Cheers

User avatar
gordon@drogon.net
Posts: 1979
Joined: Tue Feb 07, 2012 2:14 pm
Location: Devon, UK
Contact: Website

Re: Water Flow Sensors

Thu Mar 21, 2013 10:24 pm

docwisdom wrote:Gordon,

Sorry for my novice questions, but is the interrupt going to be triggered with each pulse and I increment within that function or is the interrupt triggered on the first pulse and I need to do some other polling within the function to get my count? Your count example didnt quite click with me this is why Im asking.

Cheers
It would normally trigger on each pulse, but you can code it to be polled after that if you like (but I wouldn't!) So like the Arduino one, you'd just increment a global variable each pulse, then read that variable external to the interrupt code.

You could if you wanted to time the pulses and get an instantaneous flow rate too, but it would be more accurate to time pulses over a second or more.

Each pulse represents a fixed amount of water through the device (within the accuracy limit of the device) so pulses per unit of time gives the flow rate.

-Gordon
--
Gordons projects: https://projects.drogon.net/

ATSR
Posts: 3
Joined: Mon Aug 26, 2013 6:34 am

Re: Water Flow Sensors

Mon Aug 26, 2013 6:38 am

Hi
Did you get this working?
I'm looking to measure the amount of water i'm using in my garden - so will be doing pretty much the same thing. I'm fairly new to this, so will be happy if i can reference some of your code/configs.
cheers

haroon46
Posts: 4
Joined: Mon Oct 14, 2013 9:33 am

Re: Water Flow Sensors

Thu Dec 05, 2013 11:54 am

Anyone who used the G3/4 water flow sensor using python?
http://www.seeedstudio.com/depot/g34-wa ... -1083.html
How are we supposed to get 3.3V to Raspberry pi? Because the operating voltage is 5V.
Plz help me guysss
Thanks

Quixote
Posts: 2
Joined: Fri Dec 20, 2013 6:06 pm

Re: Water Flow Sensors

Mon Dec 23, 2013 6:37 pm

haroon46 wrote:Anyone who used the G3/4 water flow sensor using python?
http://www.seeedstudio.com/depot/g34-wa ... -1083.html
How are we supposed to get 3.3V to Raspberry pi? Because the operating voltage is 5V.
Plz help me guysss
Thanks
Haroon,

Where are you getting 3.3V from?
The voltage requirements for that G3/4 flow sensor is 5-24V
Mini. Working Voltage: DC 4.5V
Max. Working Current: 15mA (DC 5V)
Working Voltage: DC 5V~24V
The two things that I'm stuck on are
  • How do I connect the sensor to my Pi?
  • How do I get readings from it?
From looking at the pinouts on both the Pi and PieFace, I can see +5VDC (red -> P1-02 or P1-03) and Ground (black -> P1-03) so those seem straightforward. So if that holds true, then all I need is the GPIO pin for the yellow (Hall effect) wire, and some Python code to give me the inputs. Right?

surfparadise
Posts: 95
Joined: Fri May 10, 2013 9:01 am

Re: Water Flow Sensors

Tue Jan 21, 2014 2:39 pm

Hi all,
I'm working too on this project. I'm try to setup a flow meter on my raspberry connected to a MCP23s17 device (using wiringpi).
I have found this code on the adafruit website, but is a pde file (Java?)....
Now I must import/connect/convert to execute it in my python script, but I'm a beginner and it's really hard to me.

I don't really know how I can use it .... Is it possibile to just call it from my python script, to have available the 'Liters' for second variable? if yes, how?

Could some of you give me an help?
Thank you in advance.


Code: Select all

/**********************************************************
This is example code for using the Adafruit liquid flow meters.

Tested and works great with the Adafruit plastic and brass meters
------> http://www.adafruit.com/products/828
------> http://www.adafruit.com/products/833

Connect the red wire to +5V,
the black wire to common ground
and the yellow sensor wire to pin #2

Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing
products from Adafruit!

Written by Limor Fried/Ladyada for Adafruit Industries.
BSD license, check license.txt for more information
All text above must be included in any redistribution
**********************************************************/
#include "LiquidCrystal.h"
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);

// which pin to use for reading the sensor? can use any pin!
#define FLOWSENSORPIN 2

// count how many pulses!
volatile uint16_t pulses = 0;
// track the state of the pulse pin
volatile uint8_t lastflowpinstate;
// you can try to keep time of how long it is between pulses
volatile uint32_t lastflowratetimer = 0;
// and use that to calculate a flow rate
volatile float flowrate;
// Interrupt is called once a millisecond, looks for any pulses from the sensor!
SIGNAL(TIMER0_COMPA_vect) {
  uint8_t x = digitalRead(FLOWSENSORPIN);
  
  if (x == lastflowpinstate) {
    lastflowratetimer++;
    return; // nothing changed!
  }
  
  if (x == HIGH) {
    //low to high transition!
    pulses++;
  }
  lastflowpinstate = x;
  flowrate = 1000.0;
  flowrate /= lastflowratetimer; // in hertz
  lastflowratetimer = 0;
}

void useInterrupt(boolean v) {
  if (v) {
    // Timer0 is already used for millis() - we'll just interrupt somewhere
    // in the middle and call the "Compare A" function above
    OCR0A = 0xAF;
    TIMSK0 |= _BV(OCIE0A);
  } else {
    // do not call the interrupt function COMPA anymore
    TIMSK0 &= ~_BV(OCIE0A);
  }
}

void setup() {
   Serial.begin(9600);
   Serial.print("Flow sensor test!");
   lcd.begin(16, 2);
   
   pinMode(FLOWSENSORPIN, INPUT);
   digitalWrite(FLOWSENSORPIN, HIGH);
   lastflowpinstate = digitalRead(FLOWSENSORPIN);
   useInterrupt(true);
}

void loop() // run over and over again
{
  lcd.setCursor(0, 0);
  lcd.print("Pulses:"); lcd.print(pulses, DEC);
  lcd.print(" Hz:");
  lcd.print(flowrate);
  //lcd.print(flowrate);
  Serial.print("Freq: "); Serial.println(flowrate);
  Serial.print("Pulses: "); Serial.println(pulses, DEC);
  
  // if a plastic sensor use the following calculation
  // Sensor Frequency (Hz) = 7.5 * Q (Liters/min)
  // Liters = Q * time elapsed (seconds) / 60 (seconds/minute)
  // Liters = (Frequency (Pulses/second) / 7.5) * time elapsed (seconds) / 60
  // Liters = Pulses / (7.5 * 60)
  float liters = pulses;
  liters /= 7.5;
  liters /= 60.0;

/*
// if a brass sensor use the following calculation
float liters = pulses;
liters /= 8.1;
liters -= 6;
liters /= 60.0;
*/
  Serial.print(liters); Serial.println(" Liters");
  lcd.setCursor(0, 1);
  lcd.print(liters); lcd.print(" Liters ");
 
  delay(100);
}

Return to “General programming discussion”

Who is online

Users browsing this forum: No registered users and 3 guests