tscodeler
Posts: 5
Joined: Sat Aug 15, 2015 6:47 pm

How to use water flow sensor on Raspberry?

Sat Aug 15, 2015 8:09 pm

I'm trying to use this water flow sensor with raspberry:

https://www.adafruit.com/products/828

I'm using this python code to read the pulses:

Code: Select all

#!/usr/bin/env python

import RPi.GPIO as GPIO
import time, sys


FLOW_SENSOR = 23

GPIO.setmode(GPIO.BCM)
GPIO.setup(FLOW_SENSOR, GPIO.IN, pull_up_down = GPIO.PUD_DOWN)

global count
count = 0

def countPulse(channel):
   global count
   count = count+1
   print count

GPIO.add_event_detect(FLOW_SENSOR, GPIO.BOTH, callback=countPulse)


while True:
    try:
        print(GPIO.input(23))
	print (count)
        time.sleep(1)
	
    except KeyboardInterrupt:
        print '\ncaught keyboard interrupt!, bye'
        GPIO.cleanup()
        sys.exit()

Unfortunately, this code is not working properly and as I'm new with raspberry I don't know how to solve the problem.

I would like to know if is necessary to use another component in raspberry, like MCP3008 or another one.

If possible, send me how to wire the sensor cables is raspberry too.
Last edited by tscodeler on Wed Aug 19, 2015 2:20 am, edited 1 time in total.

texy
Forum Moderator
Forum Moderator
Posts: 5173
Joined: Sat Mar 03, 2012 10:59 am
Location: Berkshire, England

Re: How to use water flow sensor on Raspberry?

Sun Aug 16, 2015 9:24 am

When posting pythons listing you need to use the Code tabs otherwise all the formating is lost.
Texy
Various male/female 40- and 26-way GPIO header for sale here ( IDEAL FOR YOUR PiZero ):
https://www.raspberrypi.org/forums/viewtopic.php?f=93&t=147682#p971555

User avatar
B.Goode
Posts: 13791
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: How to use water flow sensor on Raspberry?

Sun Aug 16, 2015 11:41 am

tscodeler wrote:If possible, send me how to wire the sensor cables is raspberry too.
Since the working voltage of the sensor is between 5v and either 18 or 24v (depending on which bit of the webpage you read), it seems reasonable to suppose that is also the signal level returned on the yellow pulse output. The gpio pins on an RPi are only safe for input voltages up to 3.3v, so you will need to find some way of protecting your RPi from voltage levels above this.

As a workaround: there is a published sketch for an Arduino which includes output to a serial monitor. Perhaps you could exploit that and simply read the serial data with your RPi? (Again, the signal levels between the RPi and the Arduino need consideration.)

Coincidentally, there is discussion of a similar project using the same flow sensor in this currently active forum thread: viewtopic.php?f=37&t=114429

User avatar
joan
Posts: 15949
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: How to use water flow sensor on Raspberry?

Sun Aug 16, 2015 11:53 am

I'm fairly sure the sensor is an open collector output and will magically start working if the PUD_DOWN is changed to PUD_UP.

tscodeler
Posts: 5
Joined: Sat Aug 15, 2015 6:47 pm

Re: How to use water flow sensor on Raspberry?

Mon Aug 17, 2015 2:27 am

Hi joan,
the code is working properly now :D
The pulses are been counted.

I saw how connect the wires in another topic that you answered a few months ago.
Take a look on the last message in this topic.
viewtopic.php?t=38208&p=316540

I connected the wires in this way on Raspberry Pi Model2:

Code: Select all

Red ------------- 5V

           +----- 3V3
           |
          10K
           |
Yellow ----+----- gpio

Black ----------- Ground
Black ------> Ground (pin 6)
Red -------> DC Power 5v (pin 2)
Yellow ------> Resistor 10K connected to DC Power 3.3V (Pin 1) -------> GPIO23 (Pin 16)

This is the sensor data sheet:
http://img.filipeflop.com/files/downloa ... F-S201.pdf

tscodeler
Posts: 5
Joined: Sat Aug 15, 2015 6:47 pm

Re: How to use water flow sensor on Raspberry?

Wed Aug 19, 2015 2:17 am

In python code I added one more line code to calculate the measure of water:

This is the last version of the code:

Code: Select all

#!/usr/bin/env python

import RPi.GPIO as GPIO
import time, sys

FLOW_SENSOR = 23

GPIO.setmode(GPIO.BCM)
GPIO.setup(FLOW_SENSOR, GPIO.IN, pull_up_down = GPIO.PUD_UP)

global count
count = 0

def countPulse(channel):
   global count
   count = count+1
   print count
   flow = count / (60 * 7.5)
   print(flow)

GPIO.add_event_detect(FLOW_SENSOR, GPIO.FALLING, callback=countPulse)

while True:
    try:
        time.sleep(1)

    except KeyboardInterrupt:
        print '\ncaught keyboard interrupt!, bye'
        GPIO.cleanup()
        sys.exit()
This expression below is correct to calculate the measure of water according the pulses generated?
flow = count / (60 * 7.5)

In this expression , 450 pulses generated by the sensor corresponds to 1 liter of water.

ghp
Posts: 2409
Joined: Wed Jun 12, 2013 12:41 pm
Location: Stuttgart Germany

Re: How to use water flow sensor on Raspberry?

Wed Aug 19, 2015 5:31 am

Hello,

according to the adafruit website, the formula are:
  • Flow rate pulse characteristics: Frequency (Hz) = 7.5 * Flow rate (L/min)
  • Pulses per Liter: 450
Regards,
Gerhard

User avatar
Ferdinand
Posts: 236
Joined: Sun Dec 01, 2013 2:24 pm
Location: Leiderdorp, NL

Re: How to use water flow sensor on Raspberry?

Mon Aug 24, 2015 1:30 pm

Hi tscodeler,

I changed your code a bit. To simulate your flowsensor.py use my squarewave.py
Connect GPIO 17 to GPIO 23.
Make both programs excecutable with the following command:
chmod 777 flowsensor.py squarewave.py
and start both programs.
sudo ./flowsensor.py in LXTerminal#1 and sudo ./squarewave.py LXTerminal#2


Test generator

Code: Select all

#!/usr/bin/python
#squarewave.py
# Square wave generator to test flowsensor
# connect GPIO 23 to GPIO 17
# change frequency settings to simulate water flows streams

import RPi.GPIO as GPIO
import time
import random
square_wave_pin = 17

# change this variable for other flow streams
# 1.0  == 0.135 l/m
# 5.0  == 0.675 l/m
# 10.0 == 1.35  l/m
# 20.0 == 2.7   l/m
frequency = 20.0

seconds = 1 / (2 * frequency)
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(square_wave_pin,GPIO.OUT)

GPIO.output(square_wave_pin, False)
time.sleep(seconds)
try:

   while True:
      GPIO.output(square_wave_pin, False)
      time.sleep(seconds)
      GPIO.output(square_wave_pin, True)
      time.sleep(seconds)

except KeyboardInterrupt:
   GPIO.cleanup()
Your code

Code: Select all

#!/usr/bin/python
#flowsensor.py
import RPi.GPIO as GPIO
import time, sys

FLOW_SENSOR = 23

GPIO.setmode(GPIO.BCM)
GPIO.setup(FLOW_SENSOR, GPIO.IN, pull_up_down = GPIO.PUD_UP)

global count
count = 0

def countPulse(channel):
   global count
   if start_counter == 1:
      count = count+1
#      print count
#      flow = count / (60 * 7.5)
#      print(flow)

GPIO.add_event_detect(FLOW_SENSOR, GPIO.FALLING, callback=countPulse)

while True:
    try:
        start_counter = 1
        time.sleep(1)
        start_counter = 0
        flow = (count * 60 * 2.25 / 1000)
        print "The flow is: %.3f Liter/min" % (flow)
        count = 0
        time.sleep(5)
    except KeyboardInterrupt:
        print '\ncaught keyboard interrupt!, bye'
        GPIO.cleanup()
        sys.exit()
Luck with your project!
Success with your project!
Ferdinand

tscodeler
Posts: 5
Joined: Sat Aug 15, 2015 6:47 pm

Re: How to use water flow sensor on Raspberry?

Sat Aug 29, 2015 2:01 pm

Hi Ferdinand,

I tested your codes in my RPi.

Thank you soo much! :D

eswasar
Posts: 1
Joined: Wed Apr 19, 2017 9:39 am

Re: How to use water flow sensor on Raspberry?

Wed Apr 19, 2017 9:56 am

Hello Friends,

I am trying a similar project in college for a test case to measure water flow.

I am new to Raspberry Pi, so can you help me the initial programming on SLES Leap 42 required to run the RPi.GPIO scripts.

Thanks,
Swaroop

jwangyue
Posts: 1
Joined: Wed Dec 12, 2018 7:52 am

Re: How to use water flow sensor on Raspberry?

Wed Dec 12, 2018 8:08 am

Can someone kindly give me the codes to use to successfully interface YFS201 water flow sensor with Raspberry Pi ?

marataziat
Posts: 3
Joined: Wed Aug 10, 2016 11:49 pm

Re: How to use water flow sensor on Raspberry?

Wed Jun 19, 2019 8:24 am

How can I connect 20 sensors like this? If I will use this: https://www.amazon.com/HiLetgo-Expansio ... B01N47M0ZE it will work properly?

pcmanbob
Posts: 12841
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: How to use water flow sensor on Raspberry?

Wed Jun 19, 2019 12:26 pm

The add on board you have linked to is just a means of connecting devices to the gpio using easy to use terminal blocks, that can be jumpered to any gpio pin, it provides no interfacing of its own.

Yes you may be able to use it to connect your sensors , but as you have not specified which sensor or how they connect to your pi no one can say for sure.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

John_Anderson
Posts: 1
Joined: Sun Oct 18, 2020 7:38 pm

Re: How to use water flow sensor on Raspberry?

Sun Oct 18, 2020 7:40 pm

I'm using the industry's most comprehensive range of corrosion-free a flow meter which are cost effective, excellent compatibility with liquids, foam, vapor and can offer low current consumption for battery powered applications. You can visit this website: https://iconprocon.com/product-category/flow-meters/

janetb
Posts: 1
Joined: Sun Oct 25, 2020 2:00 pm

Re: How to use water flow sensor on Raspberry?

Sun Oct 25, 2020 2:23 pm

I have not seen a single code example for reading a Hall-effect sensor that actually works.
At even modest flow rates, the signal from the rising or fall edge event happens dozens of times a second.
If you count these pulses, the code routinely misses most of them.

Most of them look something like this:

Code: Select all

 
 #!/usr/bin/python
#--------------------------------------
#    ___  ___  _ ____
#   / _ \/ _ \(_) __/__  __ __
#  / , _/ ___/ /\ \/ _ \/ // /
# /_/|_/_/  /_/___/ .__/\_, /
#                /_/   /___/
#
#       Hall Effect Sensor
#
# This script tests the sensor on GPIO17.
#
# Author : Matt Hawkins
# Date   : 03/04/2017
#
# http://www.raspberrypi-spy.co.uk/
#
#--------------------------------------

# Import required libraries
import time
import datetime
import RPi.GPIO as GPIO

def sensorCallback(channel):
  # Called if sensor output changes
  timestamp = time.time()
  stamp = datetime.datetime.fromtimestamp(timestamp).strftime('%H:%M:%S')
  if GPIO.input(channel):
    # No magnet
    print("Sensor HIGH " + stamp)
  else:
    # Magnet
    print("Sensor LOW " + stamp)

def main():
  # Wrap main content in a try block so we can
  # catch the user pressing CTRL-C and run the
  # GPIO cleanup function. This will also prevent
  # the user seeing lots of unnecessary error
  # messages.

  try:
    # Loop until users quits with CTRL-C
    while True :
      time.sleep(0.1)

  except KeyboardInterrupt:
    # Reset GPIO settings
    GPIO.cleanup()

# Tell GPIO library to use GPIO references
GPIO.setmode(GPIO.BCM)

print("Setup GPIO pin as input on GPIO17")

# Set Switch GPIO as input
# Pull high by default
GPIO.setup(17 , GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.add_event_detect(17, GPIO.BOTH, callback=sensorCallback, bouncetime=200)

if __name__=="__main__":
   main()   
   
I have seen tweaks regarding the pull_up_down property, as well as emitting the signal on the rising edge, falling edge or both. None of them seem to be able to catch all the pulses - or even get close...

Because it is installed on an RPi and not an Arduino, the unit also has a resistor bridge in between the sensor and RPi to limit the voltage to 3.3v.

How are others solving this problem?

tjleon
Posts: 1
Joined: Fri Dec 11, 2020 10:29 pm

Re: How to use water flow sensor on Raspberry?

Fri Dec 11, 2020 10:36 pm

janet_b: Trying to count the pulses can be tricky, because the pulses can occur when you aren't expecting them. It's better to use an interrupt to count the pulses. See this article: https://wiki.seeedstudio.com/Water-Flow ... ls-requied for an example that used an arduino to accomplish what you are trying to do.

User avatar
joan
Posts: 15949
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: How to use water flow sensor on Raspberry?

Sun Dec 20, 2020 1:37 pm

Any of the Python GPIO modules will capture the pulses as long as you use the correct module function.

http://abyz.me.uk/rpi/pigpio/examples.h ... monitor_py

User avatar
hellbreak
Posts: 1
Joined: Thu Feb 04, 2021 9:29 am

Re: How to use water flow sensor on Raspberry?

Thu Feb 04, 2021 9:38 am

Hi guys,
could you please explain how exactly the flow L/m is calculated?

According to the common example code
flow = count / (60 * 7.5)

but also, according to the adafruit website, the formula is:

Flow rate pulse characteristics: Frequency (Hz) = 7.5 * Flow rate (L/min)
Pulses per Liter: 450

but also, as in the code posted above, Ferdinand wrote
flow = (count * 60 * 2.25 / 1000)
print "The flow is: %.3f Liter/min" % (flow)


Thanks

Return to “Python”