1

I have the following script I used for testing:

#!/usr/bin/env python2.7
import RPi.GPIO as GPIO, time, subprocess, threading, socket, signal
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
button=21
led=19
GPIO.setup(button, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(led, GPIO.OUT)
GPIO.output(led,True)
def buttonpressed(channel): 
 if GPIO.input(button):
 GPIO.output(led,True)
 print "rising edge detected on " + str(button)
 else: 
 GPIO.output(led,False)
 print "falling edge detected on " + str(button)
# when a falling edge is detected on button, regardless of whatever
# else is happening in the program, the function my_callback will be run
GPIO.add_event_detect(button, GPIO.BOTH, callback=buttonpressed, bouncetime=200)
print "Waiting for button to be pressed"
while True:
 try:
 time.sleep(3600)
 print "still waiting for button..."
 except KeyboardInterrupt:
 print "Exption caught after timer."
 if killer.kill_now:
 break
GPIO.cleanup() # clean up GPIO on normal exit

schematic

simulate this circuit – Schematic created using CircuitLab

Using an NO button on testing only the first button press is reliably sensed, but occasionally other presses are. Sample output:

$ ./test.py 
Waiting for button to be pressed
falling edge detected on 21
rising edge detected on 21
rising edge detected on 21
rising edge detected on 21
rising edge detected on 21
rising edge detected on 21
rising edge detected on 21
rising edge detected on 21
rising edge detected on 21
rising edge detected on 21
falling edge detected on 21
rising edge detected on 21
rising edge detected on 21
rising edge detected on 21
rising edge detected on 21
rising edge detected on 21

If I remove the bouncetime I get a lot of false positives, and if I put it to 1ms it is relatively reliable but still not great.

As it seems to be clear that I need to put a capacitor in place is there any alternative to adding this, and if not what size of capacitor should I need? Are there any other attributes I should be mindful of when shopping for a capacitor?

asked Jan 4, 2017 at 14:12
3
  • It's early morning so I may be missing something, but shorting pin 21 directly to ground through the switch doesn't seem right... Commented Jan 4, 2017 at 14:32
  • How else would you do it? Commented Jan 4, 2017 at 21:09
  • I was just misunderstanding what you were trying to achieve here. Commented Jan 4, 2017 at 21:17

4 Answers 4

1

Your problem is probably caused by the state of the switch which may change between the event triggering and your call to

GPIO.input(button)

I think that the only solution is to use hardware debouncing with a small capacitor in parallel to the switch.

Greenonline
2,9725 gold badges27 silver badges38 bronze badges
answered Jul 4, 2018 at 15:02
1

Reduce or remove bouncetime=200 entirely in GPIO.add_event_detect(button, GPIO.BOTH, callback=buttonpressed, bouncetime=200)

Then in your callback, add a static variable and a condition statement that will accept the input only if the edge is different from the previous one :

def buttonpressed(channel):
 if "previous_edge" not in buttonpressed.__dict__:
 buttonpressed.previous_edge = None 
 if GPIO.input(button) != buttonpressed.previous_edge:
 if GPIO.input(button):
 GPIO.output(led,True)
 print "rising edge detected on " + str(button)
 else: 
 GPIO.output(led,False)
 print "falling edge detected on " + str(button)
 buttonpressed.previous_edge = GPIO.input(button)

That should get rid of the bounces.

answered Mar 31, 2021 at 15:57
0

Add two resistors and a capacitor to the contact switch to form a deboucing circuit such as this:

An RC debouncer

This will correct your signal input.

I suggest that you read A Guide to Debouncing August 2004 by Jack G. Ganssle, August 2004 by Jack G. Ganssle.

answered Jul 4, 2018 at 15:49
0

My take in two parts:

  1. The edge detection is a simple algorithm, you keep track of the current state of the pin and when the next state is read you compare the two. You then decide wether it was an edge or not: https://fritzenlab.net/2024/04/20/falling-and-rising-edge-input/ .

  2. The debounce can also be made by algorithm, this way: once you detect an edge (step above) you wait for a couple of milisseconds, for the physical switch to stabilize. Once that time has passed you are good to go.

answered Apr 22, 2024 at 12:19

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.