# Are the subjects "button debounce" and "Nyquist–Shannon sampling theorem" related or not?

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.

• is the pushbutton used for a manual interface? Nov 16, 2022 at 20:43
• 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? Nov 16, 2022 at 21:16
• 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 Nov 16, 2022 at 21:50
• Continued. Still want to filter the input as a 5ms pulse might be due to vibration but 100ms is a valid input. Nov 16, 2022 at 21:51
• 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.
– jonk
Nov 17, 2022 at 1:57