0

I'm working on a simple script that detects a voltage drop on a circuit. Specifically, when a doorbell is pressed. I've wired the doorbell up to GPIO pins.

The below Python script works, however, it only outputs a result the first time a voltage drop is detected. If I press the doorbell a second time, It doesn't output a result.

Please could someone point me in the right direction to update my script to continuously listen for additional voltage drop events, rather than just the first one? Essentially, looping it back to listen for additional events after the 100ms debounce.

#!/usr/bin/env python3
import signal
import sys
import os
import RPi.GPIO as GPIO
BUTTON_GPIO = 23
def signal_handler(sig, frame):
    GPIO.cleanup()
    sys.exit(0)
def button_pressed_callback(channel):
    print("button pressed")
if __name__ == '__main__':
    GPIO.setmode(GPIO.BCM)
    GPIO.setup(BUTTON_GPIO, GPIO.IN, pull_up_down=GPIO.PUD_UP)
    GPIO.add_event_detect(BUTTON_GPIO, GPIO.FALLING,callback=button_pressed_callback, bouncetime=100)
    signal.signal(signal.SIGINT, signal_handler)
    signal.pause()
2

The script works for me.

The only thing which could go wrong is that the button is not returning to a high state after being pressed and released.

joan@pi3bp:~ $ cat q.py

#!/usr/bin/env python3
import signal
import sys
import os
import RPi.GPIO as GPIO
BUTTON_GPIO = 23
def signal_handler(sig, frame):
    GPIO.cleanup()
    sys.exit(0)
def button_pressed_callback(channel):
    print("button pressed")
if __name__ == '__main__':
    GPIO.setmode(GPIO.BCM)
    GPIO.setup(BUTTON_GPIO, GPIO.IN, pull_up_down=GPIO.PUD_UP)
    GPIO.add_event_detect(BUTTON_GPIO, GPIO.FALLING,callback=button_pressed_callback, bouncetime=100)
    signal.signal(signal.SIGINT, signal_handler)
    signal.pause()

joan@pi3bp:~ $ python q.py

button pressed
button pressed
button pressed
button pressed
button pressed
1
  • Thanks for validating, you're correct, the issue was related to my circuit. – Eliot Harper Jan 2 at 10:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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