View: 1249|Reply: 0

Using Flow Sensor with WiringOP

[Copy link]

1

threads

1

posts

5

credits

Novice

Rank: 1

credits
5
Published in 2023-5-31 19:51:47 | Show all floors |Read mode
This post was finally edited by OARB at 2023-5-31 20:03

Hi, I am new to Linux based systems and Orange Pi 5. For a project, I need to use a flow sensor and display the flow rate.


I am using a flow sensor with sends pulses. The Orange Pi creates an interrupt to read the pulses and increase a counter, and the flow rate is caluclated based on that. I am facing an error while running the file that: "wiringPiISR: unable to open /sys/class/gpio/gpio138/value: No such file or directory


The code that I am using:


import time
import ctypes

# Load the WiringPi library
wiringpi = ctypes.CDLL('libwiringPi.so')

# Constants
FLOW_SENSOR_PIN = 5

# Set up WiringPi
wiringpi.wiringPiSetup()

# Set pin mode to INPUT
wiringpi.pinMode(FLOW_SENSOR_PIN, 0)

# Variables
pulse_count = 0
flow_rate = 0.0

# Function to handle interrupt
def handle_interrupt():
    global pulse_count
    pulse_count += 1

# Configure interrupt handler
INT_EDGE_FALLING = 1
INT_EDGE_SETUP = 0
wiringpi.wiringPiISR(FLOW_SENSOR_PIN, INT_EDGE_FALLING, ctypes.CFUNCTYPE(None)(handle_interrupt))

# Start the program
print("Flow Sensor Reading Started")

try:
    # Main loop
    while True:
        # Reset pulse count
        pulse_count = 0

        # Delay for 1 second
        time.sleep(1)

        # Calculate flow rate
        calibration_factor = 4.5  # Calibration factor for the flow sensor
        flow_rate = (pulse_count / 7.5) * calibration_factor  # Calculate flow rate in liters per minute

        # Print flow rate
        print("Flow rate: {:.2f} L/min".format(flow_rate))

except KeyboardInterrupt:
    # Cleanup GPIO pins
    wiringpi.pinMode(FLOW_SENSOR_PIN, 0)

    print("\nFlow Sensor Reading Stopped")


What am I doing wrong or missing out?
You need to log in before you can reply login | Register

Points Rule

Quick reply Top Return list