1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

[Python] GPIO - Sensor Miscounting

Discussão em 'Python' iniciado por Stack, Setembro 8, 2025.

  1. Stack

    Stack Membro Participativo

    I'm having a problem with the GPIO library on python. When i use a flow sensor with a Raspberry Pi 3/3b with RaspberryOS 32b lite.

    I calibrate the sensor constant (responsible for converting pulses to ml) to extract a specific amount, such as 100 ml. After calibration, i change the volume to obtain a different value. When the volume is greater, the error percentage decreases, becoming negative (releasing less ml than desired). If the volume is smaller, the error percentage increases (releasing more). I think that it start to happen when i start to use thread on the code. But the strange fact it that when i comment everything else and keep only this code it still doesn't work.

    import RPi.GPIO as GPIO

    class FlowSensor:
    def __init__(self, options: dict) -> None:
    self.count = COUNTPIN
    self.control = CONTROLPIN
    self.FLOWFACTOR = FLOWFACTOR
    self.count = 0

    GPIO.setup(self.count, GPIO.IN, pull_up_down=GPIO.PUD_UP)
    GPIO.setup(self.control, GPIO.IN, pull_up_down=GPIO.PUD_UP)
    GPIO.add_event_detect(self.count, GPIO.FALLING, callback=self.count_pulses)

    def count_pulses(self, channel):
    self.count += 1

    def volume(self) -> float:
    return self.count * self.FLOWFACTOR


    I've tried to format the system, change sensor or valve or rasp tried to use pigpiod:

    import RPi.GPIO as GPIO
    import pigpio
    import threading

    class FlowSensor:
    def __init__(self, options: dict) -> None:
    self.count_pin = options['countPin']
    self.control_pin = options['controlPin']
    self.current_count = 0.0
    self.constant = 0
    self.lock = threading.Lock()
    self.pi = pigpio.pi()
    self.callbacks = []

    GPIO.setup(self.control_pin, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
    self.pi.set_mode(self.count_pin, pigpio.INPUT)
    self.pi.set_pull_up_down(self.count_pin, pigpio.PUD_DOWN)
    self.cb = self.pi.callback(self.count_pin, pigpio.RISING_EDGE, self._handle_increment)
    def volume(self) -> float:
    return self.current_count * self.constant

    def _handle_increment(self, gpio, level, tick):
    if level in (0, 1):
    with self.lock:
    self.current_count += 1.0
    for callback in self.callbacks:
    callback()

    def add_callback(self, callback):
    self.callbacks.append(callback)

    def remove_callback(self, callback):
    self.callbacks.remove(callback)


    But still doesn't work.

    Continue reading...

Compartilhe esta Página