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]


Recent Entries

Comments

Oldest comments listed first.

Posted by: Murray on October 22, 2009 at 1:20 PM

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.


Posted by: Matt Mets on October 26, 2009 at 11:03 PM

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


Posted by: tth on October 23, 2009 at 1:00 AM

Maxim Chip

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.


Posted by: Matt Mets on October 26, 2009 at 11:08 PM

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.


Posted by: Mike5 on October 23, 2009 at 7:24 AM

flip flop debouncing

"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...


Posted by: Saeid Al-Wazzan on October 25, 2009 at 8:18 AM

RS latch

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.


Posted by: Matt Mets on October 26, 2009 at 11:13 PM

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


Leave a comment


Subscribe to MAKE!Subscribe to MAKE Magazine!

Subscribe today, save 42% and get web access to MAKE free. MAKE Digital Edition is available only to subscribers.

$34.95 / 1 year
(4 Quarterly Issues)

Subscribe now


Void your warranty, violate a user agreement, fry a circuit, blow a fuse, poke an eye out. Make: The risk-takers, the doers, the makers of things... Welcome to Make: Online!


CRAFT Maker Shed Maker Faire MAKE television




Check out more videos from MAKE.

MZ_Jameco-RobotBuild_RR.gif
MZ_DIYMovieMaking-RR.gif
Maker SHED

Connect with MAKE

Be a MAKE fan on Facebook MAKE on Facebook
Visit our Facebook page and become a fan of MAKE!
MAKE on Twitter MAKE on Twitter
Follow our MAKE tweets!
MAKE Flickr Pool MAKE on Flickr
Join our MAKE Flickr Pool!
    make_tips on Twitter



    MAKE Archives

    Make: Money

    Make: Science Room
    Subscribe to MAKE Magazine!

    Make: Online editors and authors!

    Gareth BranwynGareth Branwyn
    Editor-in-Chief


    Phillip TorronePhillip Torrone
    Senior Editor
    | Web | Twitter


    Becky SternBecky Stern
    Associate Editor
    | AIM | Twitter


    Marc de VinckMarc de Vinck
    Contributing Writer
    | AIM | Twitter


    John ParkJohn Park
    Contributing Writer
    | Twitter


    Sean RaganSean Ragan
    Contributing Writer
    | Twitter


    Matt MetsMatt Mets
    Contributing Writer
    | AIM | Twitter


    Dale DoughertyDale Dougherty
    Editor & Publisher
    | Twitter


    Shawn ConnallyShawn Connally
    Managing Editor
    | Twitter


    Goli MohammadiGoli Mohammadi
    Associate Managing Editor

    Kip KayKip Kay
    Weekend Projects
    | AIM | Twitter


    Collin CunninghamCollin Cunningham
    Contributing Writer
    | AIM | Twitter

    Adam FlahertyAdam Flaherty
    Contributing Writer
    | AIM | Twitter


    John BaichtalJohn Baichtal
    Contributing Writer
    | AIM | Twitter



    More contributors: Mark Frauenfelder (Editor-in-Chief, MAKE magazine), Kipp Bradford (Technical Consultant/Writer), Chris Connors (Education), Diana Eng (Guest Author), Peter Horvath (Intern), Brian Jepson (O'Reilly Media), Robert Bruce Thompson (Science Room)

    Suggest a Site!

    Advertise here with FM.

    Why advertise on MAKE?
    Read what folks are saying about us!

    Click here to advertise on MAKE!



    Current Podcast

    itunesdl.gif Weekend Project: Pinhole Panoramic Camera A sturdy, panoramic design pinhole camera that uses 120 film. Thanks go to Ross Orr for the original article in MAKE Volume 09. To download The Pinhole Panoramic Camera video video click here and subscribe in iTunes. Check out... More...

    Get the Make: Online sent via email
    Enter your email to receive Make: Online each day:



    Sign up for the Make: Newsletter

    Our Make: Newsletter covers news from maker Media, has original columns, Shed deals, and more! You can also read the archives of past issues.


     



    MAKE Fascination video series brought to you by Dow

    Make: Education
    MAKE: en EspaƱol MAKE: Japan
    Important please read


    Subscribe to MAKE Magazine!

    Recent Posts from the Craft: Blog