8

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.

10

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)

  • 1
    Hi joan, the code is working now and the pulses are been counted. Thanks!! – Thiago Scodeler Aug 29 '15 at 13:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.