1
\$\begingroup\$

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.

\$\endgroup\$
3
  • 1
    \$\begingroup\$ On the linked page the maximum frequency is listed as 10 L/MIN = 82 Hz. The Swissflow meter runs between 100 Hz and 2 kHz. Your Pi isn't running a real-time operating system so there will be variation between sampling points and you may be missing too many. It may be possible to use an interrupt to ensure that no pulses are missed. \$\endgroup\$
    – Transistor
    Commented Nov 11, 2021 at 8:08
  • \$\begingroup\$ Thanks for the comment! I assume I'd have to delve into the world of C to get the interrupt on the metal? I read that the Pi was capable of getting 20kHz using the Rpio python lib before missing samples so should be well within range... \$\endgroup\$
    – pma07pg
    Commented Nov 11, 2021 at 9:27
  • \$\begingroup\$ Use the Arduino to filter and count the pulses then send it via USB to the Pi. There are plenty of libraries and Python bindings to do this. Something like a ESP32 would do both the networking and pulse counting with ease and still use the Arduino tools. \$\endgroup\$
    – Kartman
    Commented Nov 11, 2021 at 11:03

2 Answers 2

0
\$\begingroup\$

The MCu is targeted for a real time process control, meanwhile the SoC, PC,...are made for non-real time processes.

You would need a special RT Kernel for your board, but even then it is very questionable.

\$\endgroup\$
1
  • \$\begingroup\$ Thanks for the reply. I did think that it was a bit too optimistic but I mentioned in the above comment that the most basic python lib can allegedly achieve 20KHz before starting to struggle. Might give it a go with PIGPIO but if it's really an issue with the board then I'll hook it up to the arduino. \$\endgroup\$
    – pma07pg
    Commented Nov 11, 2021 at 9:30
0
\$\begingroup\$

I think the pi just isn't going to cut the mustard. I'll use the Arduino to read the sensor reliably and then send the data to the Pi.

Thanks everyone!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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