Shop OBEX P1 Docs P2 Docs Learn Events
De-bounce a contact — Parallax Forums

De-bounce a contact

velocityxlrgvelocityxlrg Posts: 18
edited 2007-01-08 16:55 in Propeller 1
I'm using a Propeller pin to monitor a signal that goes from +5V to ground at a frequency of about 5 Hz to a maximum of 23 Hz, square wave. I'm using a Negedge counter to count the number of times per second this is occuring, but I'm having some trouble. I get the correct answer and then suddenly I get nonsense - sometimes a really high count or sometimes a very low count. I'm thinking it has something to do with the way the signal is being generated. I know when I try grounding the pin by hand, I will get wild fluctuations in the reading of the Negedge counter; one time it will be 3 and the next second it will be 12645 or some other wildly fluctuating value. I think I'm experiencing some kind of bounce on contact closure. What can I do to filter this out given the 5 to 23 Hz signal that I'm trying to pick up?

Comments

  • danieldaniel Posts: 231
    edited 2007-01-04 19:15
    While not directly answering your question, I will point you to some good material on debouncing--the empirical analysis, theory, math, hardware, and software can be found at ·http://ganssle.com/debouncing.pdf.

    Daniel
  • Paul BakerPaul Baker Posts: 6,351
    edited 2007-01-04 19:35
    Given the slow nature of your signal an appropriately scaled RC debouncer should work and still permit you to use the negedge counter.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Paul Baker
    Propeller Applications Engineer

    Parallax, Inc.
  • velocityxlrgvelocityxlrg Posts: 18
    edited 2007-01-04 21:50
    Daniel, thank you very much for the link. Even I can tell that is a very good document, but it is a little bit over my ability to pick the right values for the formulas.

    Here is a picture of my current setup. The stamp pin is suppose to float to 3.3 V (sorry I had previously written 5V) and the diode is suppose to protect it from the evils of the outside world. The black box is grounding the line at a frequency of 5 to 24 Hz.

    Can someone help me modify this circuit to be well-behaved?
    746 x 479 - 12K
  • Mike GreenMike Green Posts: 23,101
    edited 2007-01-04 22:13
    The What's a Microcontroller? book has some examples of debouncing. Get it here: www.parallax.com/dl/docs/books/edu/wamv2_2.pdf
  • danieldaniel Posts: 231
    edited 2007-01-04 23:06
    Given that your desired frequency is < 25 Hz, there is at least 40 milliseconds between pulses edges.

    Formally, you should characterize your switch mechanism's bounce (you can have contact open bounce as well as closure bounce), but it would seem that a debounce filter on the order of 5-10 ms might be a good starting point.

    You will also need to take into account your mechanism's pulse width--you do not want your filter to take too long to react so as to miss the pulses.

    You should also consider using a software filter, so you can take advantage of recognizing the first edge of the bounce--the Ganssle article covers the software principles involved. They are adaptable (with sufficient effort) to any processor.

    Daniel
  • Tracy AllenTracy Allen Posts: 6,664
    edited 2007-01-04 23:37
    Start with a 0.1uf capacitor in parallel with the 750 ohm resistor that goes to +3.3 volts. If that doesn't get rid of the bounce, then increase the value of the resistor to 10kohms or even 100kohms. With 750 ohms, the time constant is only 75 microseconds, which might not suffice. With 100koms, the time constant would be 10 milliseconds, which is enough to satisfy all but the most ugly large reed relay.

    It is nice to do the debounce in hardware, so you can continue use the NEGEDGE mode of the counter.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
  • velocityxlrgvelocityxlrg Posts: 18
    edited 2007-01-08 16:55
    Tracy Allen said...
    Start with a 0.1uf capacitor in parallel with the 750 ohm resistor that goes to +3.3 volts. If that doesn't get rid of the bounce, then increase the value of the resistor to 10kohms or even 100kohms. With 750 ohms, the time constant is only 75 microseconds, which might not suffice. With 100koms, the time constant would be 10 milliseconds, which is enough to satisfy all but the most ugly large reed relay.

    It is nice to do the debounce in hardware, so you can continue use the NEGEDGE mode of the counter.

    Thanks for the suggestion. It worked well.
Sign In or Register to comment.