Switch debounce library

Summary of Switch debounce library


Contact bounce causes mechanical switches to register multiple presses. This article proposes a software debouncing library for ATmega microcontrollers as an alternative to hardware filters. The solution reads button status repeatedly over time (e.g., 8 times every 10ms) to confirm stability before updating the state. It supports debouncing up to 16 buttons simultaneously using byte-based logic and includes functions to detect press, release, and current status events.

Parts used in the Switch Debounce Library:

  • ATmega microcontroller
  • Mechanical switch
  • Software debounce library based on Trent Cleghorn code
  • Jack Ganssle's debouncing algorithm
  • Low pass filter (Hardware alternative)
  • Schmitt-trigger (Hardware alternative)

Contact bounce (ref. https://en.wikipedia.org/wiki/Switch#Contact_bounce) is a common problem with mechanical switches and relays. Switch and relay contacts are usually made of 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.

You can find below a sample of a bouncy button press. You can see the typical ripples of the bounce button press.

Switch debounce library (1)

If you use a microcontroller to read the button status, depending on the speed you read the button it may seem that it is pressed many times, even if it is pressed once, and that’s should become a problem.

To solve this issue there is two way:

  • Hardware
  • Software
The usual hardware way is to build a low pass filter. Sometimes you will find a Schmitt-trigger after the RC filter, to avoid hysteresis.
The limitation of the hardware way is that you have to implement it on your board, to select the correct component values, and to use more hardware.
A software solution may help us, providing a malleable way to solve this issue.

An interesting reading about this topic is Jack Ganssle‘s article “A Guide to Debouncing” http://www.ganssle.com/debouncing.htm

Also, I suggest you take a read at the two articles series by Elliott Williams, he explains this issue very well and also proposes a few hardware and software solutions:

The library I propose here is, of course, a software solution developed on ATmega microcontroller but portable to many other micros.

It is based on the Trent Cleghorn code you can find it here: https://github.com/tcleg that is an implementation of the Jack Ganssle article posted above.

What it practically does is to read the button status a bounch of time, and set the final status button only if all the times the reading was done is the same.
Ganssle by experiments analyzes that a value from 10ms to 20ms is enough to consider stable a button output. In my example, I read 8 times the button each 10ms, but one can also use less time without a problem.

The library works by byte status, it means it can debounce multiples of 8 buttons at the time, one for each bit. Therefore a 2-byte implementation can debounce 16 buttons.

There is a function to get the press and release button, and the current status. Press and release functions also are implemented in such a way that one can read that function and arm back that function status only after it is read.

Code

Read More Detail:   Switch debounce library

Quick Solutions to Questions related to Switch Debounce Library:

  • What is contact bounce?
    Contact bounce occurs when springy metal contacts strike together, causing them to bounce apart one or more times before making steady contact.
  • How can you solve the contact bounce issue?
    You can solve this issue using either a hardware method like a low pass filter or a software solution.
  • What are the limitations of the hardware way?
    The limitation is that you must implement it on your board, select correct component values, and use more hardware.
  • Which articles provide good reading on this topic?
    Jack Ganssles Guide to Debouncing and Elliott Williams Embed with Elliot series are suggested readings.
  • How does the proposed library determine a stable button status?
    The library reads the button status multiple times and sets the final status only if all readings are the same.
  • What time interval is recommended for stable output?
    Experiments suggest a value from 10ms to 20ms is enough to consider a button output stable.
  • How many buttons can the library debounce at once?
    The library works by byte status, meaning it can debounce multiples of 8 buttons at a time.
  • Does the library work on other microcontrollers?
    The library was developed on ATmega but is portable to many other micros.

About The Author

Ibrar Ayyub

I am an experienced technical writer holding a Master's degree in computer science from BZU Multan, Pakistan University. With a background spanning various industries, particularly in home automation and engineering, I have honed my skills in crafting clear and concise content. Proficient in leveraging infographics and diagrams, I strive to simplify complex concepts for readers. My strength lies in thorough research and presenting information in a structured and logical format.

Follow Us:
LinkedinTwitter
Scroll to Top