Skip to content

Debounce Algorithm

Dlloydev edited this page Jan 11, 2023 · 2 revisions

Debounce Algorithm

The debounce algorithm always looks at the current input (dat) reading and the previous 2 readings (pDat and ppDat). The sample period (default 5000 μs) is the update period for startUs.

  • When the input readings are high level stable (111), the debounced output is Set (1).

  • When the input readings are low level stable (000), the debounced output is Reset (0).

  • For all other combinations, the debounced output doesn't change state (Latched).

    image

ppDat pDat dat R S Q
0 0 0 1 0 Reset (0)
0 0 1 0 0 Latched
0 1 0 0 0 Latched
0 1 1 0 0 Latched
1 0 0 0 0 Latched
1 0 1 0 0 Latched
1 1 0 0 0 Latched
1 1 1 0 1 Set (1)

Sampling

The sample period defaults to 5000 μs. With this setting, only 15ms is required for detecting a button switch being pressed or released. This may seem low when thinking of regular debouncig, but in order for this method to falsely detect a transition, it would require that there be a gap of greater than 15ms between bounces. From A Guide to Debouncing, (Anatomy of a Bounce):

Consider switch E again, that one with the pretty face that hides a vicious 157 msec bouncing heart. One test showed the switch going to a solid one for 81 msec, after which it dropped to a perfect zero for 42 msec before finally assuming its correct high state. Think what that would do to pretty much any debounce code!

Using the Toggle library, this switch could be debounced with a 15ms sample period and ignoring dropouts of up to 45ms.

Clone this wiki locally