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:
#!/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.RISING, callback=countPulse)
while True:
try:
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.
-
Did you see this post learn.adafruit.com/adafruit-keg-bot which is linked from the product page you linked to above?Steve Robillard– Steve Robillard2015年08月15日 20:26:08 +00:00Commented Aug 15, 2015 at 20:26
1 Answer 1
Water meter pulse outputs are typically open drain.
This means they are pulled to ground to signal a pulse and float high to an external voltage.
As a quick check change the following two lines.
GPIO.setup(FLOW_SENSOR, GPIO.IN, pull_up_down = GPIO.PUD_DOWN)
to
GPIO.setup(FLOW_SENSOR, GPIO.IN, pull_up_down = GPIO.PUD_UP)
and
GPIO.add_event_detect(FLOW_SENSOR, GPIO.RISING, callback=countPulse)
to
GPIO.add_event_detect(FLOW_SENSOR, GPIO.FALLING, callback=countPulse)
-
1Hi joan, the code is working now and the pulses are been counted. Thanks!!Thiago Scodeler– Thiago Scodeler2015年08月29日 13:42:55 +00:00Commented Aug 29, 2015 at 13:42