My understanding:

Regarding A/D conversion of a signal: The Nyquist-Shannon sampling theorem states, that an analog signal can be fully reconstructed (D/A converted back) if the sampling rate is >= two times the max. frequency component of that analog signal. Otherwise, aliasing errors occur. To avoid aliasing errors, an analog low pass filter must be applied to the analog signal before sampling. I only consider simple voltage signals, not image processing or whatever more advanced things, to keep it simple.

What I am trying to achieve:

I want to debounce a pushbutton signal, that is connected to a GPIO of a microcontroller.

I check the GPIO value on an interval, say 20 ms. To debounce that, I simply count "HI" values up to 10, and count down if the signal is "LOW". If my counter is over a threshold of let's say 7, I consider the signal high. If it is below 2, I consider the signal LOW. (In between, the signal maintains it's current state.)

This is how I debounce my push button.

For completeness, the button needs to be immune to electromagnetic disturbances.

My question:

Do I have to consider the Nyquist-Shannon sampling theorem here?

In other words: Do I have to make an analog filter (for example an RC lowpass), to be able to debounce the button, or will my approach work?

In other, other words: Is button debouncing related to the Shannon sampling theorem and has to be considered, or is this not applicable regarding button debounce?

Any hint, advice, opinion or comment would be appreciated.

  • \$\begingroup\$ is the pushbutton used for a manual interface? \$\endgroup\$
    – jsotola
    Nov 16, 2022 at 20:43
  • \$\begingroup\$ What is the nature, particularly the bandwidth, of these "electromagnetic disturbances" that you want the button to be immune to? Is this something beyond simple contact bounce? \$\endgroup\$ Nov 16, 2022 at 21:16
  • \$\begingroup\$ Normally I’d expect a RC filter to clean things up a little as well as giving a degree of ESD and EMI protection. Aliasing is the very least of your problems! I’d usually sample at 100Hz, apply a threshold then count the number of consecutive results. Eg: 5 highs (50ms) means the button is pressed. For things like limit switches on mechanical things, consider that vibration may cause false indications, so a longer ‘debounce’ time may be advisable. PLCs usually have options of 10s to 100s of milliseconds for this reason. Even if the switch/sensor doesn’t bounce (eg Hall effect) you \$\endgroup\$
    – Kartman
    Nov 16, 2022 at 21:50
  • \$\begingroup\$ Continued. Still want to filter the input as a 5ms pulse might be due to vibration but 100ms is a valid input. \$\endgroup\$
    – Kartman
    Nov 16, 2022 at 21:51
  • \$\begingroup\$ Oliver? I'd consider 200 ms (10 samples at 20 ms) to be completely unacceptable. My tactile feedback to brain and back again is a lot faster. (It has to be to play these fancy high speed games I've been playing!) I have determined from experience that I need to have a decision from the software within (roughly) 20 ms or else I start to notice. I spent a lot of time testing this, years ago. Below that, I can't really tell. (My son, on the other hand, would expect even better. He's "insanely fast" and I can't compete with him.) 200 ms, though, would seriously annoy me. \$\endgroup\$
    – jonk
    Nov 17, 2022 at 1:57

1 Answer 1


The input filter - be it analog or digital - needs to accommodate contact bounce for as long as the switch… bounces. This can vary widely depending on the switch.

We know that functionally the switch is conveying much less information than is in its bounce: we care if it’s closed or open. From a Nyquist standpoint your sampling rate could be much lower, or even adaptive. But for that to work we need to precondition the input to reject the bounce.

This bounce will tend to integrate over time as a ramp toward closed-state as the bounces trail off. So a sliding-window averaging filter can work, which is what you’re proposing. Effectively you're creating a low-pass filter to reject the bounce.

There's other ways. You can trigger on the initial close / open, then blank out the rest of the switch bounce time, since we know those bounce pulses don’t carry information. So if the switch bounces for no more than 10ms, you could safely ignore its state for, say, 15ms.

This may be of interest: http://www.ganssle.com/debouncing.htm

Here too: https://www.eejournal.com/article/ultimate-guide-to-switch-debounce-part-1/

  • \$\begingroup\$ The button stands as an example for an industrial HMI for machine control. The EM Disturbances are according to IEC/EN 61000-4-3 Radiated RF em fields, 80MHz - 6GHz Hardware Filter is too expensive... I fully agree, 50Hz/20ms is "human inertia". However, in my application, a notable dalay of some 100 ms is acceptable. \$\endgroup\$ Nov 17, 2022 at 12:10
  • \$\begingroup\$ Thanks a lot, very kind, this really helps. \$\endgroup\$ Nov 17, 2022 at 12:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.