I have a working project on my Arduino which tells me how much water has flowed through the sensor (http://www.swissflow.com/sf800.html). It works pretty well (give or take few ml.) The Arduino is too limited for my needs and wanted to port it to the Rasberry Pi 4.
The readings I am getting from the Pi are not random but they are way too low and vary wildly from pin to pin. I'm very much a software dev so the Arduino was perfect, being plug and play. I assumed the same about the Pi but seem to be very wrong.
Is this just a limitation on the board or am I seriously getting something wrong? Has anyone actually managed to reliably read from a Hall Sensor on a Pi with minimum variance (+- 1%)?
I used the code here as a test program and the results are just unusable.
#!/usr/bin/python
import RPi.GPIO as GPIO
import time, sys
#import paho.mqtt.publish as publish
FLOW_SENSOR_GPIO = 13
#MQTT_SERVER = "192.168.1.220"
GPIO.setmode(GPIO.BCM)
GPIO.setup(FLOW_SENSOR_GPIO, GPIO.IN, pull_up_down = GPIO.PUD_UP)
global count
count = 0
def countPulse(channel):
global count
if start_counter == 1:
count = count+1
GPIO.add_event_detect(FLOW_SENSOR_GPIO, GPIO.FALLING, callback=countPulse)
while True:
try:
start_counter = 1
time.sleep(1)
start_counter = 0
flow = (count / 7.5) # Pulse frequency (Hz) = 7.5Q, Q is flow rate in L/min.
print("The flow is: %.3f Liter/min" % (flow))
#publish.single("/Garden.Pi/WaterFlow", flow, hostname=MQTT_SERVER)
count = 0
time.sleep(5)
except KeyboardInterrupt:
print('\nkeyboard interrupt!')
GPIO.cleanup()
sys.exit()
Any guidance would be great. At the moment I'm thinking of just resorting to reading serial output from the Arduino on the Pi which isn't something I want to do but seems like the only option.