Professional Documents
Culture Documents
De Bouncing
De Bouncing
HARDWARE SOLUTION:
There are countless circuits for debouncing switches - with different methods more or less effective
under differing circumstances. The following circuit is one possible solution for switch debouncing and SWITCH DEBOUNCE CIRCUIT COMPONENTS
has been chosen for its effectiveness in switching low level logic signals used by microcontrollers (such
as the Arduino.) The circuit is very simple and uses a very small number of components (one 2N3904
transistor, one 10uF electrolytic capacitor, two 1K OHM resistors and one N/O momentary pushbutton
switch.) In this example, the output of the debounced switch is connected to DIGITAL PIN 8 on the
Arduino with and LED connected to PIN 13 (can be used for testing the switch debounce circuit.)
The following is an example of how to approach switch debounce with a software solution. You'll notice
that, though this is a rather simple function, the code can quickly become rather complicated. For this
reason, it can often be simpler to use a hardware debounce scheme to achieve a clarity in the program
and be able to dedicate memory and processing power to the functional parts of the program.
CODE:
void setup()
{
pinMode (buttonPIN, INPUT); //set pin as an input
pinMode (ledPIN, OUTPUT); //set pin as an output
}
///////////////////////////////////////////////////////////////////////
int ledToggle() //this function will toggle the state of the LED on and off
{
if (ledState = 0) //if the LED is off
{
digitalWrite (ledState, HIGH); //turn the LED on
ledState = 1; //store the new state of the output pin (ON)
}
else //if the LED is on
{
digitalWrite (ledState, LOW); //tuen the LED off
ledState = 0; //store the new state of the ouput pin (OFF))
2N3904 transistor pin layout
}
}
///////////////////////////////////////////////////////////////////////
int debounceWait() //this function will wait for the debounce time to pass
{
do
1 of 2 4/27/2022, 9:05 AM
UMass Amherst M5 https://web.archive.org/web/20141018015414/http://www.ecs.umass.edu...
{
//do nothing while this condition is true
}while (millis() - timeRead < debounceTime); //has debounce time been reached?
}
///////////////////////////////////////////////////////////////////////
void loop()
{
//read current state of button
buttonState = digitalRead(buttonPIN);
//do this if the button has been pressed
if (buttonState = 0)
{
//store the time when button was pressed
timeRead = millis();
//perform the function resulting from button press
ledToggle();
//wait for the button to come to rest in closed position
debounceWait();
do
{
buttonState = digitalRead (buttonPIN); //read the state of the button
}while (buttonState = 1); //wait for button to be released
// once the button has been released, read the time
timeRead = millis();
//wait for the button to come to rest in open position
debounceWait();
}
}
references:
switch debounce tutorial from www.arduino.cc
millis() code example from www.arduino.cc
do - while code example from www.arduino.cc
long type variable description from www.arduino.cc
2 of 2 4/27/2022, 9:05 AM