python code for water flow sensor on raspberry pi

2,758 views
Skip to first unread message

Vikas Reddy

unread,
Feb 29, 2016, 4:43:22 PM2/29/16
to pi3d
I am new in using raspberry pi. I am trying to assemble a water flow sensor to raspberry pi, for which I am unable to find proper working code. Please, can anyone help me for find the code and make it run? Here is the link for the sensor https://www.adafruit.com/product/828
Thanks in advance,
Vikas

Paddy

unread,
Feb 29, 2016, 5:06:00 PM2/29/16
to pi3d
Vikas, I've not used that sensor but either the sensor gives a pulse, or more commonly I think, it works like a switch i.e. normally open but closed when sufficient magnetic flux passes. (I would have thought Adafruit would include the actual spec sheet for the sensor but I can't see it. The arduino sketch looks to set the pin high but then watches for rising edges). I used one of the latter in my exercise bike add-on, code here https://github.com/paddywwoof/pi3d_scenery/blob/master/Scenery.py#L36 You obviously only need the hall_pulse bit not the two button press counters. I found that the pulse was pretty clean and didn't give any double edges. I think people will have posted details on using this sensor if you search the raspberrypi.org forum. The worst option would be if the sensor give 5V pulses, maybe you ought to check for that first with a multimeter.

pulse_count = 0

# Setup GPIO input
try:
 
import RPi.GPIO as GPIO
  GPIO
.setmode(GPIO.BCM)
  GPIO
.setup(4, GPIO.IN, pull_up_down = GPIO.PUD_UP) # pin 7

 
def hall_pulse(channel):
   
global pulse_count
    pulse_count
+= 1

  GPIO
.add_event_detect(4, GPIO.FALLING, callback=hall_pulse, bouncetime=50)

except Exception as e:
 
print('RPi.GPIO not here you can simulate pulses with the w key. Ex={}'.format(e))



Vikas Reddy

unread,
Mar 2, 2016, 1:09:44 PM3/2/16
to Paddy, pi3d
Hello paddy,
Thanks for your code, I tried the following code for the flow sensor

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);
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()

while executing above code it showed the error in line 7 and also Run time error. please find the image for the clear picture of the error. 
And I have one more question. What is the maximum number of temperature sensors(ds18b20) can i mount on raspberry pi? Right now I need to mount 14 temperature sensors. Is it possible to mount 14 sensors on that? Whenever I try to pull up all sensors on the screen by using cd /sys/bus/w1/devices it only shows 10 or sometimes 11, and the 11 sensor will show up very rarely. What should I do in such a case.

Thanks in advance'
Vikas


--
You received this message because you are subscribed to a topic in the Google Groups "pi3d" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/pi3d/dxdTrkr6GvE/unsubscribe.
To unsubscribe from this group and all its topics, send an email to pi3d+uns...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Paddy

unread,
Mar 2, 2016, 6:26:00 PM3/2/16
to pi3d, pat...@eldwick.org.uk
@Vikas, that error looks to say that you need to run your code as root. I thought that had been modified in the most recent versions of raspbian/RPi.GPIO

However I strongly advise you not to use IDLE3 to execute programs, and certainly not for pi3d as it doesn't cope with using certain functions of some modules such as curses. Better to get into the habit of opening a terminal window, cd to the directory where your program is and type "sudo python3 new.py" at the command prompt.

The number of temperature sensors depends on what type they are. The RPi doesn't have any analogue inputs so you either need an AD converter (I would use $3 arduino on the USB, but that only has 8 analogue inputs so you would need two, which is messy) Or you could use different addressed sensors all communicating on the I2C. Or you could get an add-on temperature sensor board that has everything built in. I think it would be tricky to communicate with individual sensors using each GPIO input (but probably possible). You will find more expertise on this kind of thing on ttps://www.raspberrypi.org/forums/

Paddy

unread,
Mar 2, 2016, 6:28:45 PM3/2/16
to pi3d, pat...@eldwick.org.uk
PS put your print(count) in your main while loop to see that it's being picked up globally.
Reply all
Reply to author
Forward
0 new messages