Arduino Button Debounce Tutorial

Nick Koumaris   info@educ8s.tv   http://educ8s.tv/  
1428
Project tested

One of the major problems encountered when using push buttons and switches in digital electronics project is the problem of bouncing. When we press a button once it may register twice and when we press it four times, in a row, for instance, it may register just twice. This occurrence is due to a property of switches known as bounciness which is as a result of the physical property of the switches.

Contact bounce (also called chatter) is a common problem with mechanical switches and relays. Switch and relay contacts are usually made of springy metals so when a switch is pressed, its essentially two metal parts coming together and even though the connection may seem already made to the user, it may not happen immediately, as a matter of fact, it may make contact on one side – then both – and then the other side –, technically bouncing between in-contact and not-in-contact until it finally settles down. This result in a rapidly pulsed electric current instead of a clean transition from zero to full current as shown in the graph below.

The effect of bouncing is usually unimportant in power circuits but causes problems in some analog and digital circuits that respond fast enough to misinterpret the on‑off pulses as a data stream and as such even though bouncing occurs within a millisecond time frame, the microcontroller often works at a faster speed and may fail or succeed to register the press of the switch due to the state of the bounce.

The effects of contact bounce can be eliminated (debounced) by use of mercury-wetted contacts, but these are now infrequently used because of the hazard of mercury release. Alternatively, contact circuits can be low-pass filtered to reduce or eliminate multiple pulses. In digital systems, multiple samples of the contact state can be taken or a time delay can be implemented in order for the contact bounce to settle before the contact input is used to control anything.

For today’s tutorial, we will be looking at how to remove the bounce effect in switches used for an Arduino project using software debounce method.

 

Required Components

The following components are required to build this project.

  1. A pushbutton
  2. Arduino Uno
  3. Resistor
  4. LED
  5. Small Breadboard
  6. Wires

As usual, each of the components can be purchased by clicking on the link attached to them.

 

Schematics

The circuit for this project is fairly easy. Connect the components as shown in the schematics below.

Schematics

Check the connection once more to be sure everything is as it should be.

Code

Code for this project is a fairly simple one, we will not be using any library and our goal is to easily handle the debounce problem such that the when the switch is pressed it is read accurately by the microcontroller.

The first thing we do in the code is to declare the pins to which the LED and the push button is connected to on the Arduino. We also declare variables which will be used to hold the state of the LED and the button.

    /////////////////////////////////////////////////////////////////
   //            Arduino Debounce Button     v1.01     //
  //         Get the latest version of the code here:            //
 //     http://educ8s.tv/arduino-button-debounce-tutorial       //
/////////////////////////////////////////////////////////////////

const int buttonPin = 2;    
const int ledPin =  13;     
int ledState = LOW;
boolean buttonState = LOW; 

int pressed=0;

Next, we then move to the void setup function where we set the pin mode of the pin to which the LED and the push button are connected to on the Arduino.

void setup() {
  pinMode(ledPin, OUTPUT);
  pinMode(buttonPin, INPUT);
}

With that done, we move into the void loop section. The actions performed within the void loop is heavily reliant on the debounce button function, which basically reads the state of a button then waits for a few ms after which it checks if the state is the same before it is registered.

boolean debounceButton(boolean state)
{
  boolean stateNow = digitalRead(buttonPin);
  if(state!=stateNow)
  {
    delay(10);
    stateNow = digitalRead(buttonPin);
  }
  return stateNow;
  
}

The void loop uses this function to determine if a button was pressed and the number of times it has been pressed. When the number of times which the button has been pressed gets to 10, the LED is turned on.

void loop() {
  
  if(debounceButton(buttonState) == HIGH && buttonState == LOW)
  {
    pressed++;
    buttonState = HIGH;
  }
  else if(debounceButton(buttonState) == LOW && buttonState == HIGH)
  {
       buttonState = LOW;
  }
   if(pressed == 10)
  {
    digitalWrite(ledPin,HIGH);
  }
}

The full code for the project is available below and can also be downloaded from the download section at the end of this tutorial.

    /////////////////////////////////////////////////////////////////
   //            Arduino Weather Station Project #2     v1.01     //
  //         Get the latest version of the code here:            //
 //     http://educ8s.tv/arduino-button-debounce-tutorial       //
/////////////////////////////////////////////////////////////////

const int buttonPin = 2;    
const int ledPin =  13;     
int ledState = LOW;
boolean buttonState = LOW; 

int pressed=0;

void setup() {
  pinMode(ledPin, OUTPUT);
  pinMode(buttonPin, INPUT);
}

void loop() {
  
  if(debounceButton(buttonState) == HIGH && buttonState == LOW)
  {
    pressed++;
    buttonState = HIGH;
  }
  else if(debounceButton(buttonState) == LOW && buttonState == HIGH)
  {
       buttonState = LOW;
  }
   if(pressed == 10)
  {
    digitalWrite(ledPin,HIGH);
  }
}

boolean debounceButton(boolean state)
{
  boolean stateNow = digitalRead(buttonPin);
  if(state!=stateNow)
  {
    delay(10);
    stateNow = digitalRead(buttonPin);
  }
  return stateNow;
  
}

 

Demo

Copy the code, paste in the Arduino IDE and upload to your Arduino board. With the circuit setup as shown in the image below, you should see the LED come on after 10 button press.

 

With this, we can now build more reliable pushbutton/switch based projects.

That’s it for this tutorial guys, As usual, let me know if you have any questions or comments via the comment section.

You can watch the video of this tutorial here.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.