Learn to code

ARDUINO & CODES

Previous Post
Creating multiple button states with Arduino
Next Post
Reading a 4 x 4 keypad with Arduino

Reliable detecting a switch with Arduino

in Arduino Digital / Analog

In this Arduino tutorial, you will learn how to reliable detect if a pushbutton is open or closed. Pushbuttons can generate spurious open/close transitions when they are pressed due to mechanical or physical issues. These transistion may be read by the Arduino as multiple presses. To overcome this problem we can debounce the input of the button. In this tutorial we will do so. We will show you how to check the input twice in a short periode of time to make sure that the button is definitely pressed. Without debouncing the button, pressing the button may cause unpredictable results. To keep track of time this tutorial will use the millis() function to keep track of the time passed since the button was pressed.

Debounce Method

The debounce method checks to see if it gets the same reading from the switch after adelay that needs to be long enough for the switch contacts to stop bouncing. You may require longer intervals for “bouncier” switches (some switches can require as much as 50 ms or more). The function works by repeatedly checking the state of the switch for as many milliseconds as defined in the debounce time. If the switch remains stable for this time, the state of the switch will be returned (true if pressed and false if not). If the switch state changes within the debounce period, the counter is reset so that the checks start over until the switch state does not change within the debounce time.This debounce() function will work for any number of switches, but you must ensure that the pins used are in input mode.

Parts you will need

Arduino Uno Rev3 Arduino Uno Rev3 × 1
Breadboard 400 point Breadboard 400 point × 1
Dupont Wires Dupont Wires × 7
LED LED × 1
Tactile Push Button Tactile Push Button × 1
10K Resistor 10K Resistor × 1
220 ohm resistor 220 ohm resistor × 1

Arduinoplatform is a participant in several affiliate programs. This means that I will earn a commision if you buy a product from the affiliated websites through clicking on the links provided above.

Circuit Diagram

Led Button Debounce

Circuit Diagram – We will use the same diagram as explained in tutorial: Arduino Tutorial: 4.2 Make a Toggle Switch button.

The Code

/*
 Debounce sketch
 a switch connected to pin 2 lights the LED on pin 13
 debounce logic prevents misreading of the switch state
*/
const int inputPin = 2; // the number of the input pin
const int ledPin = 10; // the number of the output pin
const int debounceDelay = 10; // milliseconds to wait until stable
// debounce returns true if the switch in the given pin is closed and stable
boolean debounce(int pin)
{
 boolean state;
 boolean previousState;
 previousState = digitalRead(pin); // store switch state
 for(int counter=0; counter < debounceDelay; counter++)
 {
 delay(1); // wait for 1 millisecond
 state = digitalRead(pin); // read the pin
 if( state != previousState)
 {
 counter = 0; // reset the counter if the state changes
 previousState = state; // and save the current state
 }
 }
 // here when the switch state has been stable longer than the debounce period
 return state;
}
void setup()
{
 pinMode(inputPin, INPUT);
 pinMode(ledPin, OUTPUT);
}
void loop()
{
 if(debounce(inputPin))
 {
 digitalWrite(ledPin, HIGH);
 }
}

 

Code Explanation

Declare the variables

You will need to declare variables at the beginning of the sketch. Since we are using the debounce method we use a integer variable called debounceDelay to store the milliseconds for the delay. Furthermore, we use two other variables for the LED and for the Tactile Push Button.

const int inputPin = 2; // the number of the input pin
const int ledPin = 10; // the number of the output pin
const int debounceDelay = 10; // milliseconds to wait until stable
// debounce returns true if the switch in the given pin is closed and stable
boolean debounce(int pin)
Create Debounce Function

To be able to use the debounce method we need to make a function for it. We create a boolean funtion since we want to know whether the Push Button is pushed or not. Within the function we create two boolean variables called state and previousstate. This allows us to check in 10 milliseconds if there is a change occured.

boolean debounce(int pin)
{
 boolean state;
 boolean previousState;
 previousState = digitalRead(pin); // store switch state
For Loop for checking the state

We arrived at the for loop for checking the state of the Push Button. The for loop checks in 10 milliseconds whether the state of the Push Button is changed. In other words, was there any noise in the circuit that causes the state of the button to change while we didn’t push the button.

for(int counter=0; counter < debounceDelay; counter++)
 {
 delay(1); // wait for 1 millisecond
 state = digitalRead(pin); // read the pin
 if( state != previousState)
 {
 counter = 0; // reset the counter if the state changes
 previousState = state; // and save the current state
 }
 }

Void Setup() part

This part is easy, you we will set the LED as an output pin and the Push Button as an input pin.

void setup()
{
 pinMode(inputPin, INPUT);
 pinMode(ledPin, OUTPUT);
}
Read the pushbutton with debounce method

We arrived at the last part of our sketch. To be able to use the debounce method we need to call the funtion in the loop part. The outcome of our Debounce function is True or False. If the outcome is true we the LED will light up. If the outcome is false, the LED will turn LOW.

void loop()
{
 if(debounce(inputPin))
 {
 digitalWrite(ledPin, HIGH);
 }
}

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed

Menu