How To Implement Switch Debounce | Switch Bounce Explained
Start typing to search for results…
to search
esc<br>to close
All Articles
Knowledge Base →
What is switch bounce & how to implement debounce
James Mackey
on
3 October 2024
Share on LinkedIn
Share on Facebook
Email this Page
Tags: Arduino Microcontrollers PicoScope 5000 PicoScope 7
Switch bounce is a common issue in electronics, particularly when working with physical switches. If you’ve ever noticed multiple signals from a single switch press, you’ll likely need to implement debounce.
This phenomenon can happen to any switch, button or relay contact which uses mechanical force to connect two bits of metal to form a complete circuit and allow electricity to flow. That means it can happen in a keyboard, control panel, user interface or relay as they all use the same mechanical metal contacts to create the circuit.
What is switch bounce?
Switch bounce occurs when the contacts inside a switch don’t close cleanly. When you press a switch, the internal components can momentarily bounce off each other instead of making immediate and stable contact. This brief bouncing can generate multiple electrical signals, even though you pressed the switch just once.
To visualize this, imagine two wires touching to close a circuit. Ideally, they meet and stay connected but in reality, they may bounce rapidly making and breaking contact before settling. Each bounce that goes over the threshold of an input in a microcontroller or processor can register an extra input pulse.
Without a way to handle this bouncing, systems can behave unpredictably, registering one press as several. This leads to errors in counting, toggling, or any action the switch is supposed to control.
The effects of switch bounce
To demonstrate the impact of switch bounce, we need a simple switch circuit that we can test and measure. In this example circuit, we have the switch to ground and a pull-up resistor going into a microcontroller.
The microcontroller is running a program that is continuously checking the switch position and adding 1 to a counter when the switch is triggered, followed by waiting until the switch is released (to prevent counter spam when the button is held down).
int buttonPin = 2; // the number of the pushbutton pin<br>int buttonState = 0; // variable for reading the pushbutton status<br>int counter = 0;
void setup() {<br>Serial.begin(9600); // Begin serial interface<br>pinMode(buttonPin, INPUT); // Setup button input<br>void loop() {<br>buttonState = digitalRead(buttonPin); // Read the button state<br>if(buttonState == 0) { // If the button is pressed<br>if(pressed == false) { // and if the button hasn't been held down (preventing spam)<br>counter = counter + 1; // Add 1 to the counter<br>Serial.println(counter); // Output counter to Serial bus<br>pressed = true; // Stop counting till the button is let go<br>else { // When the button is let go<br>pressed = false; // Reset the 'pressed' boolean
When observing the behavior on an oscilloscope, you’ll likely see that sometimes a single press results in two or more counts. This happens because the bouncing signal crosses the microcontroller’s voltage threshold multiple times, registering each bounce as a press.
The goal of adding debounce is to eliminate these extra signals and ensure the system only registers one input per switch press.
Two approaches can be employed to eliminate bounce from a switch. The first is a hardware debounce that utilizes electronic parts to reduce the bounce beneath the threshold. The second is a firmware debounce that incorporates a delay in the firmware to disregard the bouncing switch.
You can test and verify each of these methods using an oscilloscope to view the input and output signal.
Implementing hardware debounce
One approach to solving switch bounce is through hardware debounce, where components like resistors and capacitors smooth out the electrical signal. By adding a capacitor and resistor to the circuit, you can filter out the high-frequency bouncing, leaving just the low-frequency button press. This configuration of capacitor and resistor functions as a low-pass filter, so you can use all the calculations of an RC filter circuit to calculate the debounce of the circuit.
Below we’re using the same circuit example we used earlier. This features a switch to ground and a pull-up resistor to 5 V.
To implement a hardware debounce you first need to test how long the initial bouncing is. Using an oscilloscope allows you to measure the length of the bouncing to be able to calculate the resistor and capacitor values to add to the circuit. In this example we measured a bouncing time of around 300 μs.
To determine the appropriate resistor and capacitor values, you’ll need a combination of values that fully discharge the capacitor longer than the maximum bouncing of the switch.
Working out the discharge time is simple, first you need to calculate the time constant (τ) of the RC circuit – this will give you...