Dear friends welcome to this Arduino Button Debounce Tutorial! This is the second tutorial on buttons with Arduino. Please check our previous video in order to learn how to connect a button to your Arduino project. In this video, we are going to learn how to overcome the most common problem of buttons and digital switches which is bouncing.

Intro to the Arduino Button Debounce Tutorial

But first of all, let’s demonstrate how bouncing is a problem to an Arduino project. I have prepared a simple project to demonstrate that. This simple project counts the number of times the button is pressed If we press the button 10 times the LED goes on. Let’s try it…1, 2, 3, 4 5, 6, 7, 8, 9 9 okay reset and let’s count again. 1, 2, 3 4, 5, 6, 7, 8, 9, 10. Once more … 1, 2, 3, 4, 5, 6, 7, 8… 8! What’s going on? As we just saw the project is not working correctly although the code is correct. Sometimes the LED turns on after the button is pressed 8 times, sometimes after 9 times and sometimes after 10 as it is supposed to do. That’s the result of button bouncing.

What is Button Bouncing?

Contact bounce is a common problem with mechanical switches. Switch contacts are usually made off springy metals. When the contacts strike together, their momentum and elasticity act together to cause them to bounce apart, one or more times before making steady contact. The result is rapidly pulsing electric current instead of a clean transition from 0 to full current. This behavior causes problems as we saw before in our project. That’s a screenshot of switch bounce on an oscilloscope. The switch bounces between on and off several times before settling. That’s why our project counts more button presses than expected.

——————–
WHERE TO BUY
——————–

1. A pushbutton: https://educ8s.tv/part/Buttons

2. Arduino Uno: https://educ8s.tv/part/ArduinoUno

3. Resistor: https://educ8s.tv/part/Resistors

4. LED: https://educ8s.tv/part/LEDs

5. Small Breadboard: https://educ8s.tv/part/SmallBreadboard

6. Wires: https://educ8s.tv/part/Wires

Full disclosure: All of the links above are affiliate links. I get a small percentage of each sale they generate. Thank you for your support!

[adsense]

 Code of the Arduino Button Debounce Tutorial

Let’s now go to the computer to see the code of the project and how to overcome this bouncing problem. The code of the project is very simple. We have connected the button to digital pin 2 of Arduino. Then in the loop if we read HIGH from digital pin 2 and before it was LOW that means that the button was pressed and we can increase the counter that counts the number of button presses. If the counter reaches the value of 10 which means that we have pressed the button 10 times the program turns on the LED. If we take a close look at the oscilloscope snapshot we can see that because of button bounce, even though that we press the button once the code can think that it was pressed more than once. Very quickly. In order to fix that problem when we detect a change in the button state, we can wait a few milliseconds and read the state again in order to be sure it was a change, and if not, we can ignore that sudden change. So in order to implement that in the code we create a new function that is named debounceButton. It takes as an argument the state of the button and returns the new state of the button. If it detects a change in the state of the button, waits for 10 ms and reads the state of the button again. It does that in order to check if there was a change indeed or it was the button bouncing and then it returns the state it reads. So the last change we have to make in the code is to call the debounceButton function here instead of the digitalRead function we used before.

——————–
CODE
——————–

 

 

 

——————–

SUBSCRIBE ON YOUTUBE

——————–

Never miss a video: Subscribe to educ8s.tv