0

I run this script when the Raspberry Pi boots. When a key is pressed (GPIO 19) the interrupt function gets called.

If I remove the print("BUTTON PRESSED") statement on line 10, the interrupt function seems to be called twice in a row for a single button press.

Any idea why this is the case?

import RPi.GPIO as GPIO
import time
import subprocess

GPIO.setmode(GPIO.BCM)

GPIO.setup(19, GPIO.IN, pull_up_down=GPIO.PUD_UP)

def interrupt(channel):
    print("BUTTON PRESSED")
    p = subprocess.Popen(['node', '/btn.js', str(channel)], stdout=subprocess.PIPE)
    out = p.stdout.read()
    print(out)

GPIO.add_event_detect(19, GPIO.FALLING, callback=interrupt, bouncetime=1000)

try:
    while True:
        time.sleep(1)
except:  
    print("Exit GPIO Listener..")
finally
    GPIO.cleanup()

2 Answers 2

3

Probably switch bounce if it is a mechanical switch.

What you think of a single event may actually be multiple contacts and releases in a fraction of a second.

2
  • I would assume that the bouncetime=1000 would have taken care of that?
    – Raed
    Aug 24, 2023 at 9:23
  • 2
    So would I. However bouncetime does not really debounce. It simply ignores all signals for the bouncetime.
    – joan
    Aug 24, 2023 at 12:25
1

All mechanical switches bounce - some e.g. reed switches are more prone to this. There are mechanical solutions used for high current power switches where arcing is an issue but these are inappropriate for logic circuits.

It is possible to minimise bounce - either by suppressing extraneous signals using a capacitor to limit rise time or by introducing delays in software but using changeover switch and bistable circuitry bounce can be absolutely prevented.

1
  • Thank you for the clarification ! I'm using the buttons from this board so I don't have too much control over the hardware as it eats up all my pins waveshare.com/2.7inch-e-paper-hat.htm
    – Raed
    Aug 25, 2023 at 11:55

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.