header
Back to Tutorials Page
  

 
 
SWITCH DEBOUNCE TUTORIAL::

When a normally-open momentary switch is closed (pressed in), the contacts connect and close a circuit. When the switch is released, the contacts separate and open the circuit. The contacts are designed to be springy, so that the switch will return to its open state immediately upon being released. The same springy characteristic of the contacts that enables the switch to function in this way creates a new problem - when the switch is released, the contacts have a tendency to bounce and make a series of very short open / closed pulses called switch bounce or “chatter”. This problem can be very pronounced when using the momentary pushbutton switch as a digital logic input in a circuit, because there is one intentional signal made when the button is closed, followed by the series of signals due to the switch bounce. The result can be a very erratic or seemingly random functioning of the switch. “Switch debouncing” is the term for the technique to correct and compensate for mechanical switch bounce. There are both software and hardware methods to accomplish this. In this tutorial, we will first look at a hardware solution and then will look at a corresponding software solution.

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 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:

int buttonPIN = 9; //input pin for button
int ledPIN = 13; //LED output pin

int buttonState = 1; //rest state of switch is pulled high
int ledState = 0; //initial LED state is off

//need to use long variables to store large values from millis()
unsigned long timeRead = 0;
unsigned long debounceTime = 300; //adjust this variable for suitable debounce

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))
 }
}

///////////////////////////////////////////////////////////////////////
int debounceWait() //this function will wait for the debounce time to pass
{
 do
 {
 //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

 

SWITCH DEBOUNCE TUTORIAL

switch debounce breadboard circuit

 

SWITCH DEBOUNCE CIRCUIT COMPONENTS

switch debounce circuit components

 

SWITCH DEBOUNCE SCHEMATIC

switch debounce schematic

 

2N3904 transistor pin layout

2N3904 pin layout