Shop OBEX P1 Docs P2 Docs Learn Events
That notorious debouncing solution.. — Parallax Forums

That notorious debouncing solution..

LoopyBytelooseLoopyByteloose Posts: 12,537
edited 2014-11-26 23:57 in General Discussion
After years of reading all sorts of software debouncing solutions or special debouncing chips, I am back to a simple SPDT push button and a S-R flip-flop. (NO, not the J-K flip-flop)

Why? Simply no time delay.

http://www.electronics-tutorials.ws/sequential/seq_1.html

All the other debouncing options delay the response time of the microcontroller. This was never made clear to me before (Not even in 'The Art of Electronics' by Horowitz, et al.).

So if you must debounce, just as a 20 pin DIP for 4 switches and 8 pullup resistors. Skip all the other stuff as it under-performs (unless you find some really nice Hall-effect switches).

Comments

  • LeonLeon Posts: 7,620
    edited 2014-11-22 09:48
    That solution requires a changeover switch and an FF, which increases cost, compared to a simple push button and software.
  • LoopyBytelooseLoopyByteloose Posts: 12,537
    edited 2014-11-22 10:20
    Obviously a bit more cost, but no delay in response. I am looking to trigger my camera shutter remotely... a delay is not useful.
  • JDatJDat Posts: 103
    edited 2014-11-22 11:20
    If I need zero delay respone from button, than I am using following idea: Wait while pin becomes active (if we are using pull-up, tad active signal ir Logic 0). So, prepeller will wait for active signal (logic 0). As prepeller get logic 0, it start doing nacessary stuff (your code) and will not read button state another 20 mSec or so. After 20 mSec, propeller will wait for next button press.
    How to wait for button state?
    1) Use WAITPEQ. Do your stuff. Delay for 20 mSec. Repeat.
    2) Regularly check pin state and do other stuff. If button pressed do your stuff and don't read button for next 20 mSec. Repeat.

    That's a theoretical idea.

    Sorry, I am going to party and example code snippets will follow later.
  • LoopyBytelooseLoopyByteloose Posts: 12,537
    edited 2014-11-22 11:31
    Ummm...
    Well, i have nothing to program --- no Propeller involved inside the camera (a Canon SX-150).

    I just need a bounceless push button that will plug into a mini USB interface. The examples I have seen for DIY devices (instead of the Ricoh CA-1 or CA-2), debounce with a capacitor and a pull-up. And many examples don't even add debounce.

    http://hackaday.com/2012/04/30/adding-a-remote-shutter-to-a-cheap-digital-camera/

    Software debouncing has been covered in depth with the Propeller. It is worth mentioning, but won't work here.
  • tonyp12tonyp12 Posts: 1,951
    edited 2014-11-22 12:59
    There don't have to be a delay with a software debounce.
    As soon you get a signal-change do what you want to do, but also disable any hardware IRQ/software check of button for the next 20ms as to ignore multiple "fake" button presses.
    .
  • Hal AlbachHal Albach Posts: 747
    edited 2014-11-22 13:05
    I have tinkered on and off about how to debounce switches, especially the SPST push buttons and have come up with a simple solution probably used by a lot of others. It involves using an N-Chan MOSFET with the button between +5 and the Gate along with a 0.1uF cap and a 47K resistor in parallel from the Gate to ground. Ground the Source and pull up the drain. Pressing the button will turn on the MOSFET and almost instantly charge the 0.1 uF cap, which will keep the MOSFET on while the switch contacts bounce. Watching the Drain on my scope I see it snapping to ground without any bounces with a variety of switches. The small four pin pushbuttons you find on circuit boards show very little bounce without the cap and none with it. A Cherry Microswitch, on the other hand, bounces like crazy but with this circuit it comes out solid.
    I can't quote the exact amount of time it takes for the MOSFET to turn on, guess it really depends on which one you use. It appears the turn on is instant but the turn off does show a slight curve as the resistor discharges the cap, somewhere around half a millisecond or so. If the switch you have has poor contacts this may not work because of added resistance to charging the cap.


    debounce.gif
    433 x 372 - 3K
  • RDL2004RDL2004 Posts: 2,554
    edited 2014-11-22 13:14
    I also prefer a hardware solution. It works every time with no problems and requires just a few pennies for the components. If it was for a major commercial application there might be reason to reconsider, but for small personal projects a gate, a cap and a resistor are often the best choice... and as Loopy indicated, there is not always any software in the first place.
  • jmgjmg Posts: 15,173
    edited 2014-11-22 20:07
    After years of reading all sorts of software debouncing solutions or special debouncing chips, I am back to a simple SPDT push button and a S-R flip-flop. (NO, not the J-K flip-flop)

    Why? Simply no time delay.
    it is correct there is no leading or trailing edge edge time delays, but the SPDT can be a restriction in many designs.
    Micro-switch come in SPDT, so for those this is a valid solution.

    Besides S-R FF designs using SPDT, there is also a Pin-keep design, which has no static power drain and needs simpler logic ( 1G17 / 2G17 / 3G17 )
    The feedback resistor gives the current threshold, and once driven past that, the output locks in that state, until driven the other way.

    If your application lacks a SPDT, then you can get 'no time delay' on the leading edge with a simple re-triggerable Monostable (1G123 etc) - this allows use of a simpler SPNO switch, and any bounce extends the ON time.
    The trailing edge does have delays, but that is less important in most applications.

    Then. a purist may decide that their SPDT switch also needs a fail-detect check system, which moves a little beyond logic to a software solution.

    If the cables are long, some designs may chose to add a RC filter to the trigger, and trade of a little delay (sub 1ms) for better noise immunity.
  • xanaduxanadu Posts: 3,347
    edited 2014-11-22 20:11
    Kinda makes you wonder where the quadrature encoder i2c push buttons are. I think Jameco has an i2c pushbutton. Hall effect push buttons are expensive but from what I've read don't need debouncing.

    On small lcd clocks, when you set the time you hold the button down it will increment slowly at first, then it increases exponentially. That is a really good example of controlling debounce externally. It also leaves a lot to be desired. There are pros and cons to both but the thread started out as uC and turned into non-uC so I'm not sure this is relevant.
  • LoopyBytelooseLoopyByteloose Posts: 12,537
    edited 2014-11-23 04:02
    tonyp12 wrote: »
    There don't have to be a delay with a software debounce.
    As soon you get a signal-change do what you want to do, but also disable any hardware IRQ/software check of button for the next 20ms as to ignore multiple "fake" button presses.
    .

    You seem to be thinking of only the first edge of the pulse (this would be rising edge in my situation). AND< I have nothing programable! I require a hardwire solution.

    I need both the rising edge and the falling edge of the pulse to be under my control.

    The rising edge has the camera seek focus, the falling edge activates the shutter.
  • jonesjones Posts: 281
    edited 2014-11-23 12:31
    I need both the rising edge and the falling edge of the pulse to be under my control. The rising edge has the camera seek focus, the falling edge activates the shutter.
    I think what you are describing is a retriggerable one-shot. A short press on the button triggers the one shot to give the camera time to focus, then it times out and the shutter fires. If you are in low light or some other circumstance where the camera is taking longer than normal to focus, you hold the button down and the one shot stays triggered. Once it reaches focus, you let off the button. The one shot then runs it's normal period and times out, firing the shutter.
  • GordonMcCombGordonMcComb Posts: 3,366
    edited 2014-11-23 15:15
    jones wrote: »
    I think what you are describing is a retriggerable one-shot.

    Aye, ye olde 74123, among others. A few more components than a basic FF, but a good range of flexibility in these things. I've done projects with these for 30-odd years, including motor control PIDs, system master reset switches, and a few other things. Loopy is right that for basic pulse cleanup and stretch the simple hardware approaches are easier and cheaper if no MCU is part of the mix.
  • jmgjmg Posts: 15,173
    edited 2014-11-23 16:00
    jones wrote: »
    I think what you are describing is a retriggerable one-shot. A short press on the button triggers the one shot to give the camera time to focus, then it times out and the shutter fires. If you are in low light or some other circumstance where the camera is taking longer than normal to focus, you hold the button down and the one shot stays triggered. Once it reaches focus, you let off the button. The one shot then runs it's normal period and times out, firing the shutter.

    That still has delays, and normal retriggerable one-shot's are edge triggered, not level-holding.

    The OP wants no delays, on either edge, but smarter bounce suppression would allow SPST contacts to be used.

    This can be done with 2 tiny logic parts an EXOR + D-FF ( 1G58 + 1G80), and a RC
    Leading edge _/= loads the FF, then after the settle delay, the XOR flips the clock, and the first release-edge =\_ clears the DD. ( D connects to the RC )
    This gives SPST de-bounce on both edges, with no edge delays.
      SPST            1G58          1G80
                        __
      -/---o----------\\  \        .----.
           |  ___      ||  |-------|>  Q|-
           +-|___|--o-//__/        |    |
                    |              |   _|
                    o--------------|D  Q|o--->
                    |              |    |
                   ---             `----`
                   ---
                    |
                   GND
    
     PB ___///=================\\\\_____________
     RC _____________/===================\______
     /Q ___/====================\_______________
    
    
    
  • LoopyBytelooseLoopyByteloose Posts: 12,537
    edited 2014-11-24 05:56
    The re-triggerable one-shot is indeed another reasonable alternative.

    Canon has software called CHDK that modifies any Canon digital to use a remote shutter control and it offers three choices of configuration.

    A. One pulse - button down focus, button up shutter
    B. Two pulse - first pulse focus, second pulse shutter
    C. Ricoh CA-1 format

    http://chdk.wikia.com/wiki/CHDK

    I am still working on getting a 4043 Quad RS flip-flop for option A, but it should work with option B as well.
    The Ricoh CA-1 has been replaced by the Ricoh CA-2

    +++++++++
    These days, Hall Effect push buttons are not that easy to find. Digikey provides some in a price range from $14 to $50 USB, but nothing in stock.

    The Ricoh CA-2 is available for $30 USD on Amazon. It is an interesting solution with 1.5v AAA for power stepped up to 5V to a microcontroller that provides precise pulses -- a bit of overkill (I suspect that Ricoh wanted to make their camera's dependent on a particular pulse format.)

    =+==++===++
    I might be wrong, but the SPDT push buttons are not that hard to find.
  • LoopyBytelooseLoopyByteloose Posts: 12,537
    edited 2014-11-25 12:21
    Much as I would prefer to just use a Hall Effect push button, the prices of available units are too much.

    I went shopping locally today.

    A SPDT push button was 75 cents and a 4043 RS flip flop was 30 cents.
  • localrogerlocalroger Posts: 3,451
    edited 2014-11-26 17:38
    This is easily done in software with no perceptible time delay on either rise or fall.

    On any state change, WAITPEQ for the new state. When the WAITPEQ falls out take 3-5 more measurements to make sure the state change holds. This will be imperceptible but will weed out microsecond noise pulses (I've seen this in the field). Once the new state is established run code for the state change, WAITCNT a few milliseconds for switch bounce to subside (it's not physically possible to press and release a switch in say 5 milliseconds), and repeat with a wait for the new state.

    This will give you sub-millisecond level control of both the press and release with no hardware and no bounce.
  • LoopyBytelooseLoopyByteloose Posts: 12,537
    edited 2014-11-26 23:57
    Thanks to all..

    Obvious there are situations where the hardware or software solutions prove more optimal.

    In any event, managing to completely avoid a delay is an important criteria. After all, many of these switches are intended for robotic solutions or processes that can ill afford a latency.
Sign In or Register to comment.