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.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()

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

    while True:
    print("Exit GPIO Listener..")

2 Answers 2


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.

  • 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

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.

  • 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.