The best software-based contact debouncer

I just created one of the best contact debouncer libraries, be sure to check it out on the “Button Debounce” GitHub repository.

What is a contact bounce anyway?

For those who are unfamiliar with the term, Wikipedia might contain some useful information. To explain it quickly, switches or push buttons (in your mouse for instance) are not ideal; the digital signal produced from the mechanical part of a button usually bounce back and forth, a single pressing to a button can create a handful of pulses, which is bad for the interpretation of signals.

Bouncy Switch

Says, you click the mouse, but with contact bounce, your computer might interpret it as a double-click.

There are many solutions to the issue, but if we want to fix it once and for all with no additional cost to the circuit, we have to fix it with a software-based solution.

I decided to do just that in my way.

As a consumer, how should I use the library?

I created an example project on GitHub with build instructions.

The only thing I wanted to suggest right here right now is; if you plan on download the library as a zip file and do a copy+paste to your project, please stop and take your time to learn git, it is not the best solution but definitely worth learning.

To paste some example here:
Simply include the header file

#include "button_debounce.h"

Declare what to do when detecting a button press

const ButtonDebounce_Config button_c3_debounce_config = {
    .fell = &led_toggle,

void led_toggle() {
  // toggle an LED on pin B5
  ChgBit(GPIOB->ODR, 5);

Allocating the memory

static ButtonDebounce_State button_c3_debounce_state;

Init the state once before use


and keep sampling the button signal to the library

  ValBit(GPIOC->IDR, 3)

Putting it all together and you’ll get the code in this file.

What is so special about the “Button Debounce” library?

This is why it is arguably the best:

  • I truly fix the issue once and for all.
    I did not design it to fix the issue on my specific hardware or platform. It will work as long as you can program the software on your desired platform in C. It will work on AVR based Arduino; it will work on STM32 Blue Pill; it will work on MCS-51; it will even work on your x86-64 PC on Linux, OS X, and Windows given that you have a valid use case for it.
  • And I mean it
    I place the software under the MIT License, one of the most permissive licenses out there.
  • The design is based on a solid principle
    which work great for decades for hardware-based signal cleaning.
  • It is asynchronous and non-blocking
    which plays well with the rest of your system.
  • It is independent
    and not rely on any other software or operating systems.
  • It is simple and minimal
    the code is so easy to understand, yet powerful.
  • It is a reusable library
    not just some example.
  • It is in pure C
    no add-ons or fancy language features used, and can be compiled with SDCC.
  • It was built on my experience as a software architect
    It contains the spirit of OOP without building on an OOP language; it is not just reusable; it also composable, and testable. I may be just a hobbyist of electronics stuff, and maybe also in C language, but I have my career as a software architect, and I am confident in it.

Any comparison?

HACKADAY was once gathering debounce code from people and made a massive list of software-based debouncer. None in the list operate on the same principle as the “Button Debouncer”.
The closest one is the Ultimate Debouncer (just some examples and explanations, not a library).

But, mine is better anyway IMHO. Look into the source code, and if you don’t agree, please give me some feedback at the comment section.

Chit-chatting has been canceled

Initially, I wanted to explain the history behind its design, and every principle I used. But, screw it, who cares about some history anyway, the code speaks for itself. Please have fun using the library.

Other caveats

  • Adding RC circuits or other types of hardware-based debouncing techniques would worsen the accuracy of the debouncer. I would recommend against using a hardware-based debouncing method because it hides voltage swings. This module perceives voltage swings to determine its state; without perceivable fluctuations, the software prematurely assumed safe transition.
  • If you know the exact characteristic of the bounce you are facing, you should tweak the sample rate of the module instead of mangling with the hardware (which creates more moving parts).
  • Please note that the module will works flawlessly most of the time without any tuning. However, if the software runs on a high-speed microcontroller or the pulse noise has a very low frequency, it is inevitable to controls the sample rate. You can determine the appropriate sample rate with a DAQ device (oscilloscope), or do it trial and error style.
    And if you look for a module that helps with lowering the sample rate, look no other than the Time Prescaler, designed by Slime Systems.

This is engineering; we can accomplish something cheaper and better, more cost per unit just does not always translate to more quality.