Button debounce with Arduino, ESP8266 o STM32

If you’ve ever used buttons with the Arduino, you may have noticed that the keystrokes were sometimes doubled, and that’s where the button debounce comes in. This is because a button works by joining two mechanical elements (contacts), to close the flow of current, which can cause false contacts. Arduino being very fast, it detects these duplicate presses and cause your program to execute the button press several times.

How do we solve this problem?

The solution to the problem is very easy: when we detect a pulse, we wait a while before considering it valid. For this I bring you this button debounce library for Arduino and ESP8266.

How does the anti rebound work?

The library basically generates an object linked to a button, which performs a check every loop. This check verifies if the button has been pressed and, if it has been pressed, it marks it as pressed. After a configurable waiting time, if the button is still in the same state, it returns as having been pressed. This process is repeated in the same way for when the button is released, to also avoid false pulses.

What does this bookstore offer me?

At first this library basically offers what you see, a function that makes the button presses not duplicated. There are several libraries that do it and you can write a small code to supplement it, but I also wanted to add functionalities such as long press detection, or burst mode. In addition, some of those libraries did not convince me because of the functionalities or because they blocked the program while waiting to confirm the keystroke.

What the long press detection does is that after a preset time (1 second by default), the library launches an event indicating that this long press has been made, so you can capture it and act accordingly. This is very useful for, for example, contextual menus, or to put a function for a short press and another for a long press … In my case, I added this functionality to display a menu of options when you press and hold a button.

What the burst mode does is that when the button is left pressed for a preset time (1 second by default), it triggers an event indicating it, and then repeats that event every 100ms (also configurable), until the button is released. This is useful for when you want to make buttons to go up a number, for example, in which we want to avoid the user having to press the button 100 times and the counter goes up just by pressing it.

Of course, this library is not limited to just buttons, and offers a debounce for switches. It also works for any event based on the LOW and HIGH states of a digital pin.

Also add that it has been tested on STM32 and ESP8266, working perfectly.

Download and install

You can download this library through Bitbucket, so you will always have the latest version:


Once downloaded, you can install it by copying the content to a folder in the Arduino\libraríes folder of My Documents, which is located at C:\Users\<user>\Documents\Arduino\libraries.

How is the debounce used?

I could fill several paragraphs here about how the button debounce library is used, but based on the principle of laziness, I won’t. Nah seriously, in the code downloaded from the Bitbucket link above, there is a folder called examples. This folder has examples of how to use the library, and it has the advantage that it will be up-to-date, so we avoid that the code put here does not work because it is outdated.

Before saying goodbye, I have to thank the original creator of the library, which helped me greatly to make mine possible.


In my library, I have only removed the block that was performed with a delay to detect false positives and that in many cases it does not matter, and I have added the functionalities of the long press and the burst mode.

As always, I hope you liked it and found it useful. Greetings!

Leave a Reply

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