Debouncing a switch?

Discussion in 'The Projects Forum' started by GRNDPNDR, Apr 22, 2012.

  1. GRNDPNDR

    Thread Starter Member

    Mar 1, 2012
    435
    7
    I have a very "bouncy" or sensitive SPST pushbutton switch that will be turned on and off repeatedly, I would like to debounce the switch such that it will still register each time it's pressed but eliminate the bounce from each press. It's only 5V.

    What would be a good, simple way to approach this with
     
  2. KMoffett

    AAC Fanatic!

    Dec 19, 2007
    2,574
    230
    What is your switch connected to that bouncing is a problem? Schematic?

    Ken
     
  3. WBahn

    Moderator

    Mar 31, 2012
    17,715
    4,788
    What resources do you have to throw at the problem? By that I mean do you already have a microcontroller in the system with an unused pin? Is putting a small 8-pin MCU in there a possibility? Is putting some digital logic chips an option? Do you need to stick with just a few passive components?

    How long does it bounce for (the bounce duration)? What is the minimum amount of time that it is pressed and also between presses (the dwell time)? You might try just putting an RC filter between the switch and what you are using the signal for and set the RC time constant so that it is long compared to the bounce duration but well short of the minimum dwell time.
     
  4. GRNDPNDR

    Thread Starter Member

    Mar 1, 2012
    435
    7
    This is the circuit. The two headers, J22 and J23 are connected to SPST switches, that are basically pushbutton switches.

    It's for a drum-set, the switches that will be connected to J22/J23 are foot pedals.

    When pressing the foot pedals, multiple presses are registered when just one was made. This is more promenant when the switch is being moved slowly and not being fully pressed, but also occurs when the switch is being pressed rapidly.


    The circuit itself is just design so both pedals can be used together such that when one pedal is held down (switch held closed) the other is still able to operate normally.

    The problem is in the switches themselves, and they could use debouncing.
     
  5. WBahn

    Moderator

    Mar 31, 2012
    17,715
    4,788
    Aside from your debouncing issues, there are a few other things to consider about your circuit.

    1) Why are you taking the signal for the top circuit (J23) directly from the header (i.e., above the LED) and the signal for the bottom circuit (J22) from below the LED?

    2) A 1Meg resistor seems a bit extreme if you want any light coming out of the LEDs since it is going to limit you to only about 3uA.

    3) The edge detector (pulse generator) circuit you have between the NOR gates is going to try to raise the input to the inverter-configured NOR gate to about 10V on a LO-HI transistion of the first gate. You should but a diode between the RC junction and the power supply node (i.e., across the resistor) to limit the gate input voltage to a diode drop above the supply.

    4) I would try putting an RC filter before the inputs from the header to the NOR gates and make it as aggressive as you can tolerate.
     
  6. GRNDPNDR

    Thread Starter Member

    Mar 1, 2012
    435
    7

    Gotta keep it simple, the overall project is fairly large but initially I need to have it working on 2x9v batteries. This isn't far fetched as nothing is constantly-on.

    Anyway, passive components are best for the debounce, to answer your question about duration? well they're drum pedals, so it's kind of random.

    either pedal may be held on or off for any period of time, but I would say the longest dwell time could be as long as a minute or more, but on the short side the dwell time may only be 50ms or less (4 or even 8 beats per second).

    Keeping propigation delay down is also ideal as lag will cause faulty notes and throw timing way off.
     
  7. GRNDPNDR

    Thread Starter Member

    Mar 1, 2012
    435
    7
    Mistake....that's why :)

    I'm going to change that. Those LEDs were added as an after thought to indicate when one of the switches/pedals was pressed. They may be removed in the end product.

    This circuit (minus the LEDs) had been built and tested as working by numerous people, I'm merely using the design, making some changes then was going to breadboard it myself.

    Why will it try and pull it to 10V? There is only 5V powering the circuit?

    I'm not exactly sure where you are telling me to put the diode though.....

    by agressive you mean? And in which way should the RC be wired?
     
    Last edited: Apr 22, 2012
  8. WBahn

    Moderator

    Mar 31, 2012
    17,715
    4,788
    I have no doubt that it does, but it is relying on the input protection diodes in the logic chips to protect the gates. While that is what they are there for, it is poor engineering to do that without a damn good reason and tends to lessen the life of the chips. Here is what is happening: The first NOR gate is output a LO for a long time which means that the resistor at the input to the second NOR gate pulls that input up to the 5V supply rail. So you have 5V across the capacitor. Now the first NOR gate transitions from LO to HI and the left side of the capacitor goes up to 5V. But you can't change the voltage across a capacitor instantaneously, so the other side is still 5V higher, making it 10V (ignoring the action of any protection diodes in the IC). BTW: this is known as a 'charge punp'. This high voltage is now bled down to 5V by the resistor, which takes about 5ms. What you want is a low resistance path from the input of the second NOR gate to the 5V supply whenver the voltage is much above 5V. You accomplish by installing a diode with the cathode connected to the supply and the annode to the gate input, which places it in parallel with the resistor. Now as soon as the right side of the capacitor moves above 5.7V, the diode becomes forward biased and very quickly dumps the charge on the capacitor to the supply and thereby clamping the input to the NOR gate at something well under 6V.
    Making the RC time constant as long as you can tolerate based on how much latency is acceptable.

    If you can't get it to work nicely without using more latency than you can tolerate, then I would recommend looking into an 8-pin microcontroller. The cost isn't much more than your 4001 chip, especially once you include the resistors, capacitors, and diodes you can eliminate at the same time. Then you can debounce it is software and you have a lot of flexibility on how you choose to do it so as to minimize latency. You do, however, have to obtain the tools and skills to work with an MCU, but those have become quite cheap for real basic level stuff.
     
  9. CDRIVE

    Senior Member

    Jul 1, 2008
    2,223
    99
    Replace the lines from the high nodes of R62 & R63 going to the inputs of U9A & U9C with 10KΩ resistors. Then connect a 1000pf cap from U9A & U9C inputs to ground. That should be a good enough starting point for you.

    BTW. What's with your Q8 transistor stage? :confused: There's no Vcc!
     
  10. GRNDPNDR

    Thread Starter Member

    Mar 1, 2012
    435
    7
    Modified, see attachment. The original design calls for 3V, so I've gotten rid of the 7805 and used a zener instead. a nice 3.3V.

    That is OUTPUT so to speak, to another module which basically just relies on whether or not the switch is open or closed. So I believe a voltage/signal is being sent to the switch. In this case the switch is that transistor.
     
  11. WBahn

    Moderator

    Mar 31, 2012
    17,715
    4,788
    I had a couple of comments about that part of the circuit, but chose to look the other way. I'm assuming that the circuit that is connected to via the J24 header has its own power and the purpose of Q8 is to pull something low. The big concern I have is if that other circuit is powered from the same source as this circuit, then he may have a ground reference problem due to the LED11 and R66
     
  12. GRNDPNDR

    Thread Starter Member

    Mar 1, 2012
    435
    7

    The circuit connected to that transistor is not powered from the same source as the rest of this circuit. That being said, you may be correct here and that LED could cause problems.

    Something to consider.
     
  13. WBahn

    Moderator

    Mar 31, 2012
    17,715
    4,788
    What is you V+ supply? A 9V battery? If so, then you will be drawing a continuous ~75mA from the battery, which will drain it pretty quickly. I would recommend using an LM317 adjustable voltage regulator instead.

    In general, FETs make better switches (you might look at the 4066 quad switch as an option, depending on how much current you are talking about). You might be able to put the switches in parallel and eliminate D10, D11 and R67. But, I suspect the BJT will probably work for you (would need to know the details of what its connected to to be sure).
     
  14. GRNDPNDR

    Thread Starter Member

    Mar 1, 2012
    435
    7
    Whats plugged into J24 is the ION Audio Xbox 360 Drum Module.

    Nobody knows quite exactly how it works inside but it's a funny little creature to some degree.

    The drum pads themselves are all piezo tranducers, and the module detects the pulses and makes them into drum signals.

    The pedal however is just an on/off switch. The module detects a hit when it's closed. connecting piezo based kick pedal does NOT work plug & play.

    The kick pedal is on or off, and is entirely passive. It is nothing more than a SPST momentary switch. It can be held closed indefinitely and does not trigger another hit when it is released/opened. it only registers a hit when it transitions from open to closed.
     
  15. GRNDPNDR

    Thread Starter Member

    Mar 1, 2012
    435
    7
    Am I getting close here?
     
  16. WBahn

    Moderator

    Mar 31, 2012
    17,715
    4,788
    So, does this sound like the behavior you would like to see:

    At any given time, the debouncer is in one of three states: ARMED, FIRED, DISARMED.

    The definition of these states is as follows:

    When ARMED, the switch has been open for sufficiently long such that it is considered to be fully open and past any bouncing that occured in going from closed to open. In this state, an time the switch is sensed as being closed it will immediately move to the FIRED state.

    In the FIRED state, the circuit asserts its output. It remains in this state for a specific amount of time regardless of whether the switch is open or closed. At the end of that specific period of time, it transitions to the DISARMED state.

    In the DISARMED state, the module is looking for the switch to be sensed in an open condition for a minimum number of consecutive pollings before proceeding to the ARMED state.

    Code ( (Unknown Language)):
    1.  
    2. // Constants
    3. FIRED_LENGTH = 50; // How many cycles to assert the output
    4. DEBOUNCE_LENTGTH = 100; // How many cycles to dwell in debounce state for
    5.  
    6. // Initialize
    7. count = DEBOUNCE_LENGTH;
    8. state = DISARMED
    9.  
    10. while (TRUE) // Loop continuously forever
    11. {
    12.     switch = GetSwitchState();
    13.     IF (state is DISARMED)
    14.     {
    15.         IF (switch is CLOSED) // Restart the debounce period
    16.             count = DEBOUNCE_LENGTH;
    17.         ELSE
    18.             count = count - 1;
    19.  
    20.         IF (count is zero) // Debounce period expired, switch is firmly open
    21.             state = ARMED;
    22.     }
    23.     ELSE IF (state = ARMED)
    24.     {
    25.         if (switch is CLOSED)
    26.         {
    27.             state = FIRED;
    28.             count = FIRED_LENGTH;
    29.         }
    30.     }
    31.     ELSE // state is FIRED
    32.     {
    33.         IF (count is zero) // Fired length as expired
    34.         {
    35.             SetOutput(DEASSERTED);
    36.             state = DISARMED;
    37.         }
    38.         ELSE
    39.         {
    40.             SetOutput(ASSERTED);
    41.             count = count - 1;
    42.         }
    43.     }
    44. }
    45.  
    I think the above pseudocode captures the behavior you are looking for. Hopefully it is clear enough for you to follow. This could be implemented in a cheap (~$1) microcontroller or the behavior could be implemented in discrete logic.
     
  17. GRNDPNDR

    Thread Starter Member

    Mar 1, 2012
    435
    7
    As much as I like the idea of a microcontroller it really isn't in the cards right now. I'f I'm going to get into that kind of stuff them I'm going to use it in more areas than one. This circuit is just a small portion of a much larger device.

    I don't have the time to start learning microcontrollers and programming at this point.
     
  18. CDRIVE

    Senior Member

    Jul 1, 2008
    2,223
    99
    Yes, did it help?
     
  19. GRNDPNDR

    Thread Starter Member

    Mar 1, 2012
    435
    7
    I won't really know until I build and test this circuit.

    What I may do however Is simply test the debounce ability of the RC components on the pedal alone and see how it fares.

    I wasn't so much aware of the bounce problem until I connected the drumset to a non-video-game brain, and started using the pedal, it was hitting notes all over the place.

    I read after the fact that the pedal switch is bouncy.


    I may just get a scope and connect it to the pedal as well and actually try and determine how long the bounce is.
     
Loading...