Ask MAKE: Debouncing a switch

Ask MAKE: Debouncing a switch




Ask MAKE is a weekly column where we answer reader questions, like yours. Write them in to mattm@makezine.comor drop us a line on Twitter. We can’t wait to tackle your conundrums!

debounce_switches.jpg

Jen writes in:

I’m making a circuit to blink an LED at different speeds, that I can control using a button switch. It wouldn’t work until I added added some ‘debounce’ code. What does that mean, and why did I have to add it?

Good Question! Switch bounce is one of those rare electronic effects that is actually caused by a mechanical shortcoming. The issue is in the way the switch works. When you flip a switch (or press down on a button), you’re really pushing one piece of metal against another. If this happens with sufficient force, one or both of the pieces will deform slightly, and then bounce back in the other direction. Depending on how well the switch was designed, this could go on for a number of times before both pieces stop moving. Now, all of this bouncing means that from an electrical perspective, the switch looks like it is opening and closing rapidly until the bouncing dies down. It happens very quickly, however digital electronics are fast enough to see this as a bunch of switch presses!

There are three ways to deal with this: mechanically (building a better switch), electrically (building a filter out of discrete electric components), or digitally (with software).

In your question, you mentioned that you used some debounce code, which is probably the best solution for you. For this solution, you write a software routine that runs when someone presses a button, then waits for a short time (long enough for the bouncing to have stopped) before reading the switch state. This effectively ignores any spurious signals from the switch contacts bouncing without any extra hardware. If you happen to be using the Arduino platform, try this tutorial.

If you aren’t using a microcontroller, though, software isn’t going to help, and you will have to try one of the other solutions. The traditional way to handle switch bounce on a breadboard is to use a resistor-capacitor (RC) filter in a low pass configuration. What this does is prevent the output of the switch from going high too quickly, which effectively filters out any high-speed signals. Check out this tutorial if you’d like to try out this method.

So, how could you re-design the switch to prevent bounce? It turns out that the most common thing to do is to wet the contacts with mercury. Because mercury is a liquid at room temperature, its surface tension keeps the contacts connected even when they bounce. The only issue with this is that mercury is pretty toxic, so you should only use this if you absolutely need to, such as when you’re controlling high-power machines at fast speeds.

[photo by Flickr user russ_j_taylor]

14 thoughts on “Ask MAKE: Debouncing a switch

  1. Murray says:

    When using a micro controller,I usually just sample buttons every millisecond. With most switches, bouncing happens at a much higher frequency so no double contacts are recorded.

    1. Matt Mets says:

      Ha, I suppose that should work. I’m doing something similar by default in a MIDI keyboard project that I am working on.

  2. tth says:

    Maxim IC make a nice debouncing chip :
    http://www.maxim-ic.com/quick_view2.cfm/qv_pk/1896/t/al

    There is a 8 bits part, with a chip-select input.

    1. Matt Mets says:

      Oh cool, I didn’t know such things existed. I guess a debouncing chip is probably overkill for simple circuits, but I could imagine it coming in handy when you need something that just works, especially if you are working with some safety-critical equipment.

  3. Mike5 says:

    “There are three ways to deal with this: mechanically (building a better switch), electrically (building a filter out of discrete electric components), or digitally (with software).”

    A fourth way is to use a flip flop…

    1. Saeid Al-Wazzan says:

      For SPST switches, an RS latch can be used to debounce the switch safely. We used that when building logic circuits. In the case where a microcontroller is involved, I think the software debouncing solution is best.

    2. Matt Mets says:

      Hehe, so it is using discrete electronic components to build a digital filter, you got me there!

Comments are closed.

Discuss this article with the rest of the community on our Discord server!
Tagged

ADVERTISEMENT

Maker Faire Bay Area 2023 - Mare Island, CA

Escape to an island of imagination + innovation as Maker Faire Bay Area returns for its 15th iteration!

Buy Tickets today! SAVE 15% and lock-in your preferred date(s).

FEEDBACK