Arduino switch debouncing – 2 easy ways with Pros and Cons
All my content is free.
If you feel that my videos are helping and you would like to contribute, you can toss some coins in the Tip Jar via PayPal.
Select amount then click the “Donate” button.
In the previous tutorial we saw how to connect and use tact switches with an Arduino.
Now we will look at how to make sure that when we press a switch it doesn’t register more than one press.
This is know as “Debouncing”.
What exactly is “Debouncing a switch”?
Most switches are simple mechanical devices that make a connection between two contacts.
When you press a switch the contact is made, and like we saw in the previous tutorial the Arduino detects the state (LOW or HIGH), and as long as the switch is pressed the state stays the same.
But not all switches are created equal, and some will fluctuate between both state (this is called “bouncing” from LOW to HIGH) during the pressing of the switch and this can introduce multiple detected presses on the Arduino.
There are many ways to work around this problem, here are 2 simple ones, with their pros and cons:
- Using a simple “delay()” function.
- Using the “millis” function.
Using a simple “Delay()” Function
The way this works is when you start pressing the switch the first time the Arduino detects the desired state (LOW in this case) the delay() is started and waits between 50 and 200 milliseconds, this pauses the entire code on the Arduino until the delay is expired.
The reasoning is that the delay will be long enough to not let the Arduino read the bouncing and only register one switch press.
This method works fairly well but there are pros and cons:
- The delay() function is a ‘blocking’ function, freezing the execution of the Arduino code, so that when the delay is started nothing else can be done, such as reading a sensor or displaying values on an LCD, until the delay is expired.
- It can’t be used inside Interrupts.
- Requires less coding than the millis() function and doesn’t need variables.
- Easy to use and adequate for simple projects.
Using the “millis()” Function
The millis() function, when called, returns a time value (in milliseconds) that represent the time passed since the Arduino was powered up.
This timer is started automatically at power up without the need of any coding, so you can just call the function “millis()” to get the current value.
So by using some variables we can know the amount of time passed and use this instead of the delay() function.
The big advantage is that the millis() function is also a “non-blocking function” which means that the Arduino is not paused or frozen, so you can do other things unlike the delay() function which pauses the code completely.
Pros and cons:
- The millis() function requires a bit more coding since it requires variables compare to know how much time as passed.
- The millis() timer will overflow (reset to zero) after approximately 49 days, no really a problem in most cases but something to keep in mind depending on how you use it.
- Since it doesn’t block or stops the code, you can do other things like check sensors or update display.
- Can be used inside interrupts unlike the delay() function.
- Useful for accurate timing of actions inside projects.
In this tutorial we will be connecting 4 tact switches.
The black one will demonstrate switch bouncing.
The red and blue will show the delay() and millis() functions.
The yellow one will reset the LED Matrix counter.
All the switches are using the Arduino INPUT_PULLUP resistors to set their un-pressed state to HIGH.
We are also using some button state variables to make sure that when a switch is pressed and held down, the counter does not keep increasing.
Both the delay() and millis() function have a value of 100ms for debounce.
As always please watch the tutorial video for more information.
Copy and Paste the above code/sketch in your Arduino IDE software.
Download the Parola library here: