Vous êtes sur la page 1sur 13

How to use water flow sensor on Raspberry?

Post Reply

13 posts • Page 1 of 1

tscodeler

Posts: 5

Joined: Sat Aug 15, 2015 6:47 pm

How to use water flow sensor on Raspberry?


 Quote
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

Posts: 5155

Joined: Sat Mar 03, 2012 10:59 am

Location: Berkshire, England
Re: How to use water flow
sensor on Raspberry?
 Quote
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

B.Goode

Posts: 8241

Joined: Mon Sep 01, 2014 4:03 pm

Location: UK

Re: How to use water flow


sensor on Raspberry?
 Quote
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

joan

Posts: 14188

Joined: Thu Jul 05, 2012 5:09 pm

Location: UK

Re: How to use water flow


sensor on Raspberry?
 Quote
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?
 Quote
Mon Aug 17, 2015 2:27 am
Hi joan,
the code is working properly now 
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?
 Quote
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: 1401

Joined: Wed Jun 12, 2013 12:41 pm

Location: Stuttgart Germany

Contact: Website 

Re: How to use water flow


sensor on Raspberry?
 Quote
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

Ferdinand

Posts: 236
Joined: Sun Dec 01, 2013 2:24 pm

Location: Leiderdorp, NL

Re: How to use water flow


sensor on Raspberry?
 Quote
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?
 Quote
Sat Aug 29, 2015 2:01 pm
Hi Ferdinand,

I tested your codes in my RPi.

Thank you soo much! 

eswasar

Posts: 1

Joined: Wed Apr 19, 2017 9:39 am

Re: How to use water flow


sensor on Raspberry?
 Quote
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?
 Quote
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?
 Quote
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: 6640

Joined: Fri May 31, 2013 9:28 pm

Location: Mansfield UK

Re: How to use water flow


sensor on Raspberry?
 Quote
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.

Vous aimerez peut-être aussi