Button Debouncing With Smart Interrupts

Button Debounce Teaser" data-medium-file="https://hackaday.com/wp-content/uploads/2025/01/debounce-main.jpg?w=400" data-large-file="https://hackaday.com/wp-content/uploads/2025/01/debounce-main.jpg?w=800" />Debouncing button or switch inputs on microcontrollers can be a challenging problem for those first starting to program these devices. Part of the reason for this difficulty is that real-world ...read more

featured-image

Debouncing button or switch inputs on microcontrollers can be a challenging problem for those first starting to program these devices. Part of the reason for this difficulty is that real-world buttons don’t behave like the idealized textbook components we first learn about, and therefore need special consideration to operate like one would expect. There are simple ways to debounce inputs like adding a delay after a button is pressed, but for more efficient use of computer resources as well as adding some other capabilities to inputs .

The strategy with this debounce method is not simply to use a single ISR for the button input, but to activate a second timer-based ISR at that time that runs at a certain interval which timestamps any button press and checks the amount of time the button has been active. If it’s under a certain threshold the ISR assumes it’s caused by bounce and blocks the bounce. If the timestamp ages past another longer threshold it knows the button has been released.



This method allows on-the-fly adaptation between long button presses and rapid button presses and is capable of debouncing both types. For those wanting to try this out, [stockyu] has included some example Arduino code for others to use. It’s an interesting take on a solution for a common problem, and puts very little load on the microcontroller.

There are about as many ways to debounce inputs as there are microcontroller platforms, though, which frees up 100% of the microcontroller’s CPU..