Simple push button debounce - Raspberry Pi

Thread Starter

Xavier Pacheco Paulino

Joined Oct 21, 2015
728
I commonly see the following algorithm to read a button in a Pi:

Code:
import  RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)
GPIO.setup(18, GPIO.IN,pull_up_down=GPIO.PUD_UP)

while True:
    inputValue = GPIO.input(18)
    if (inputValue == False):
       do something
    time.sleep(0.3)
But, does this handle the bouncing with the time.sleep function? I'm looking for the simplest way to debounce a button, as my program is not complex I don't need extensive debouncing routines.
 

jpanhalt

Joined Jan 18, 2008
11,087
Can't read your code, but is the 0.3 in seconds or milliseconds? The usual delay values are in the range of 10 ms to 40 ms for mechanical switch debouncing. Dedicated debounce chips that I have seen are about 40 ms. Some code waits a set period, reads again and if no change moves on. If the readings change, they stay in the loop. Others just detect a change, wait, and move on.

I would not agree that a simple program doesn't need good debouncing. In fact, that might be more true of a lengthy program in which a non-debounced switch would be unlikely to trigger it more than once (making assumptions about how the switch is read, polling, interrupts, etc). An example in which switch bounce caused the most problems for me was a simple program to enter a binary code. In that instance 10 ms was too short, but 40 ms worked well.
 

AlbertHall

Joined Jun 4, 2014
12,344
Debouncing a switch is easily achieved by having a time between reads of the switch that is greater than the maximum expected bounce time.
That is all you need to do. Take each read of the switch as the fully debounced state of the switch.

The only circumstances in which this will not work is if you need the exact time of the switch opening or closing.
 

Thread Starter

Xavier Pacheco Paulino

Joined Oct 21, 2015
728
My bad when comparing project complexity and debounce complexity. Once I came across a very long debounce routine which made me feel stressed. As I'm a Raspberry beginner, I feel the necessity to start with the simplest way to debounce a push button. That doesn't mean that I'm not interested in more complex debounce code. I'll take into account all you've stated above.
 

jpanhalt

Joined Jan 18, 2008
11,087
My comment about complexity and debounce was based on the fact that, say, the switch bounces for 20 ms (20,000 us) and the processor is a 16 MHz. With a PIC, 16 MHz gives an instruction cycle of 250 ns = 80,000 instruction cycles . Thus, unless the program initiated by the switch is very long or has delays, it is possible the processor will complete the routine, loop back, and repeat the routine while the switch is still bouncing.

If/when you get into interrupts, looking at the interrupt flag (even if interrupts are not enabled) can be helpful (i.e., the flag can be set on switch action and remains set until you clear it), but it is not a cure for debouncing.
 

danadak

Joined Mar 10, 2018
4,057
Debouncing is a f() of what behavior you want the switch to present.

Press followed by release to act on (ie bounce in and out) or press and hold and act require separate treatment.


Regards, Dana.
 

Travm

Joined Aug 16, 2016
363
Simplest way to debounce a switch is a resistor and capacitor. 0 lines of code. Sometimes the resistor is optional.
In my opinion anyway. There are so many different ways to do it it's crazy.

In software I use interrupts and a variable counter to read when the switch changes then ignore the switch for a short period of time until the bouncing stops. Still prefer capacitors.
 
Top