Last Updated:15/09/2021
How To 1 - Debouncing Buttons
Lessons >> How To >> How To 1 - Debouncing Buttons
Lesson Contents
Issues without debouncing
Simple debounce example
Debouncing in a function
All examples have two buttons but can easily be explanded for more buttons as required
Circuit
2 x push buttons
2 x 10k Ohm resistors
Digital pin and resistor to Gnd are on one side of the button
5v connection to opposite side of the button
Example 1: debounceNoDebounce.ino
Simple sketch showing the issues of using buttons without debouncing
Click to Download code:debounceNoDebounce.ino
/* debounceNoDebounce
2 buttons without debounce
This script shows the impact of button bounce in different situations.
*/
//Global variables
//set up pins 8 & 9 with buttons
const int buttonPin1 = 8;
const int buttonPin2 = 9;
//built in led
const int ledPin = 13;
void setup() {
Serial.begin(9600);
Serial.println("debounceNoDebounce");
pinMode(buttonPin1, INPUT);
pinMode(buttonPin2, INPUT);
pinMode(ledPin, OUTPUT);
}
void loop() {
//local variable
int Pressed;
Pressed = digitalRead(buttonPin1);
//With an LED being turned on or off, if the button bounces the same signal to turn it on is repeated
//However to the user there are no implications
if (Pressed > 0) {//Code to do something when button 1 pressed goes in here
digitalWrite(ledPin,HIGH);
}else{
digitalWrite(ledPin,LOW);
}
Pressed = digitalRead(buttonPin2);
//With button 2 we see the impact in multiple Serial.println() taking place
if (Pressed > 0) {//Code to do something when button 2 pressed goes in here
Serial.println("Button 2 Pressed");
Serial.println(millis());
}
}
Example 2: debounceV1.ino
Debounced buttons example
Click to Download code:debounceV1.ino
/* debounceV1
How to debounce buttons
see http://www.digitaltown.co.uk/lesson7delaymillis.php for more information on millis() timers
*/
const int buttonPin1 = 8;
const int buttonPin2 = 9;
//
unsigned long buttonDebounce;
int buttonDebounceSensitivity = 220;//to high and system becomes unresponsive, too low and multiple presses recorded
void setup() {
Serial.begin(9600);
Serial.println("debounceV1");
pinMode(buttonPin1, INPUT);
pinMode(buttonPin2, INPUT);
}
void loop() {
int Pressed;
if (millis() > buttonDebounce) {
buttonDebounce = millis() + buttonDebounceSensitivity;
Pressed = digitalRead(buttonPin1);
if (Pressed > 0) {//Code to do something when button 1 pressed goes in here
Serial.println("Button 1 Pressed");
Serial.println(millis());
}
Pressed = digitalRead(buttonPin2);
if (Pressed > 0) {//Code to do something when button 2 pressed goes in here
Serial.println("Button 2 Pressed");
Serial.println(millis());
}
}
}
Example 3: debounceV2.ino
Button processing and debounce moved into a function
Click to Download code:debounceV2.ino
/* debounceV2
Buttons and debounce in a function
see http://www.digitaltown.co.uk/lesson7delaymillis.php for more information on millis() timers
*/
const int buttonPin1 = 8;
const int buttonPin2 = 9;
const int ledPin = 13;
unsigned long buttonDebounce;
int buttonDebounceSensitivity = 220;//to high and system becomes unresponsive, too low and multiple presses recorded
void buttonPress(){
int Pressed;
if (millis() > buttonDebounce) {
buttonDebounce = millis() + buttonDebounceSensitivity;
Pressed = digitalRead(buttonPin1);
if (Pressed > 0) {//Code to do something when button 1 pressed goes in here
Serial.println("Button 1 Pressed");//TX led will flash
Serial.println(millis());
digitalWrite(ledPin,HIGH);//Turn built in led on
}else{
digitalWrite(ledPin,LOW); //Turn built in led off
}
Pressed = digitalRead(buttonPin2);
if (Pressed > 0) {//Code to do something when button 2 pressed goes in here
Serial.println("Button 2 Pressed");//TX led will flash
Serial.println(millis());
}
}
}
void setup() {
Serial.begin(9600);
Serial.println("debounceV2");
pinMode(buttonPin1, INPUT);
pinMode(buttonPin2, INPUT);
pinMode(ledPin, OUTPUT);
}
void loop() {
buttonPress();
}
Additional Resource Links
Don't forget to use the Reference in you Arduino IDE and look at the block comments and single line comments for more help on this lesson.Comments
This site has been designed to be child friendly, this means that comments cannot be added to videos or directly to the site. To add a comment or ask a question please email the address in this image: and use How To 1 - Debouncing Buttons as a reference.