Microchips exist at a different time scale from humans, their electronics “hearts” can beat millions of times a second – think of them as extremely hyperactive brains. Microchip pins can either “listen” (take input) or send voltage signals (output).When in input mode, a microchip pin can detect the state of a button (button pushed, or button unpushed) and in output mode it can turn on or off an LED.When a human pushes down on a mechanical button and lifts their finger away, the spring inside the button pushes the button back up to its initial position. During this process there may be micro-bounces of the metal contacts inside the button. A microchip listening to the input at this time can interpret these micro bounces as changes of button states, and if the number of these bounces is odd, the microchip can conclude that the button has settled into a state other than the one that it is from the perspective of a human being.To resolve this issue, known as bouncing, there are two approaches: software or hardware. In software, the microchip can effectively check the state of the button twice, after a delay which exeeds the period of time that a button can bounce for. This code essentially detects a change in button state, then waits a few milliseconds and checks to see that the button is in the same state. This effectively rules out the possibility of having detected a button debounce.The second possibility is to do the debouncing with hardware, that is, electronically. In this approach, we stop the erroneous button state signals from arriving at the microchip in the first place. This typically involves using a small capacitor, the hardware equivalent of a delay in our code.Top and bottom rows of the button are connected when the switch is activated (pushed on).We want the microchip to read two distinct states: HIGH or LOW. When the button is unpressed, the 10K pull-down resistor connects PB0 to GND. When the button is pressed, the pin is connected to 5V directly, a more appealing option for the electrons than the large resistor towards ground. This circuit is NOT debounced.
Bouncing button gallery
Debounced circuit
The solution to this bouncing button with hardware is a combination of an RC (resistor capacitor) circuit along with an inverter. The product of R*C (known as tau) should be greater than the debounce time. In the circuit below, 100K * 0.1uF = 0.01 seconds, which is the time it will take to charge 63.2% of the capacitor.
Debounce circuit from Learning the Art of Electronics, p.874 by Thomas C. Hayes. Ignore the stuff after the digital input.
This article: https://hackaday.com/2015/12/09/embed-with-elliot-debounce-your-noisy-buttons-part-i/ goes in to greater depth explaining how this process works but check out the results:
Debounced circuit oscilloscope trace
Yellow is inverter output, green is RC output.
To confirm our calculation of RC (time until 63% charged) = 0.01 seconds.
Deboucing in action
Software debouncing
int main(void)
{
DDRD = 0b11111111; // Port D output
DDRB = 0b00000000; // Port B input
PORTB = 0b11111111; // pull-up resistors enabled
while (1)
{
if (PINB & (1<<0))
{
_delay_ms(50); //
if (PINB & (1<<0))
{
PORTD = 0b00000000; // turn off LED
}
}
else
{
PORTD = 0b11111111; // turn on LED
}
}
return(1);
}