|
| 1 | +# Debouncing Approaches {#debouncing_approaches} |
| 2 | + |
| 3 | +Obtaining the state of switches and buttons generally requires to debounce the input signal because of contact bouncing. |
| 4 | +The waveform of an input may be smoothed physically using a low-pass filter. |
| 5 | +Coping with bouncing programmatically may allow a simplified circuit thus reducing complexity and material cost. |
| 6 | + |
| 7 | +Debouncing in software follows in general one of these approaches: |
| 8 | + |
| 9 | +1. steady sampling of the signals and waiting for either |
| 10 | + 1. an average value to cross a threshold |
| 11 | + 2. a sequence of continuous states to reach a minimum length |
| 12 | +2. observing processor interrupts triggered by state changes of digital inputs |
| 13 | + 1. after a state change, ignore further changes for a specified time before adopting the next change |
| 14 | + 2. after a state change, wait for the state to be stable for a specified time before adopting that change |
| 15 | + |
| 16 | +Steady sampling has disadvantages: |
| 17 | + |
| 18 | +- if the sample rate is too low, the reaction may be significantly delayed |
| 19 | +- if the number of samples is too low, a state change may be prematurely detected (state changes while still bouncing) |
| 20 | +- constantly polling the state |
| 21 | + - may bind processing time |
| 22 | + - may require context switching |
| 23 | + - may be solved (more) efficiently using the processor's hardware timers and counters (availability depends on the MPU) |
| 24 | + |
| 25 | +Interrupt based sampling only consumes processing time if actual changes occur. |
| 26 | +This is especially advantageous with buttons, which are seldomly actuated, compared to other activities. |
| 27 | + |
| 28 | +Also interrupt based sampling is not flawless. |
| 29 | +The specific disadvantages depend on the chosen approach and the concrete implementation. |
| 30 | + |
| 31 | +In any case it must be considered that detecting a steady state change of a button should probably be executed with a higher priority than the execution of the processing of that event. |
| 32 | +For example periodically sampling or reacting on an digital input should interrupt time-insensitive processing. |
| 33 | +But the actual processing of a finally adopted state change may take longer and should take place with lower priority. |
| 34 | +Thus some kind of synchronization mechanism (or interrupt masking) will usually be necessary. |
| 35 | + |
| 36 | +Hereafter two different approaches based on hardware interrupts will be discussed. |
| 37 | + |
| 38 | + |
| 39 | +## Interrupt based Debouncing |
| 40 | + |
| 41 | +Interrupts called by the processor as a reaction on a change of a digital input may be used to detect state changes of switches and buttons. |
| 42 | +In contrast to debouncing approaches which sample with fixed periods, interrupt based debouncing usually uses the time relative the the last state change to detect steady states. |
| 43 | +Either by discarding changes which occur shortly after a previously adopted state change. |
| 44 | +Or by waiting for the last change to prevail long enough. |
| 45 | + |
| 46 | + |
| 47 | +### Post-change delay |
| 48 | + |
| 49 | +Approaches which ignore changes which occur in a short period after a previously accepted change are easy to implement. |
| 50 | +One simply has to compare the timestamp of the last accepted change with the current time. |
| 51 | +If that difference is below a threshold, the state change will be ignored. |
| 52 | + |
| 53 | +\include{doc} bouncing-classic-normal.puml |
| 54 | + |
| 55 | +The timing diagram above shows how, when the button is pressed, a rising edge will be detected. |
| 56 | +But also bouncing will occur for some time. |
| 57 | +A threshold defines a window (highlighted section) beginning with the first event in which following state changes will be ignored. |
| 58 | +After that window, if the button is released the falling edge, and therefor the state change, will immediately be detected. |
| 59 | + |
| 60 | +The debounced signal will be active for the time between button pressed and released. |
| 61 | + |
| 62 | +\include{doc} bouncing-classic-too-short.puml |
| 63 | + |
| 64 | +A problem arises in case the button press (the pulse) is too short. |
| 65 | +The first state change will be detected. |
| 66 | +But if bouncing finishes within the window for ignoring changes, the final state (released) will not be detected. |
| 67 | +The debounced state will remain "active" until further state changes. |
| 68 | + |
| 69 | +As the time a button bounces may differ on each actuation, defining a threshold value totally avoiding this problem may not be possible. |
| 70 | + |
| 71 | + |
| 72 | +### Pre-change delay |
| 73 | + |
| 74 | +A different interrupt based approach waits for the input signal to be stable before adopting its change. |
| 75 | +This can be achieved with a delay which will be restarted each time a state change occurs. |
| 76 | +If the delay elapses without further changes the state is considered as stable. |
| 77 | +The current state is then read to check for a state change. |
| 78 | + |
| 79 | +The duration of the delay can be much shorter than the window in which state changes will be ignored from the previous approach. |
| 80 | +In contrast to defining the expected time after which a signal will be stable it defines the minimum time a signal may not change to be accepted as stable. |
| 81 | +Thus the duration is more a requirement then a heuristic. |
| 82 | + |
| 83 | +\include{doc} bouncing-delay-normal.puml |
| 84 | + |
| 85 | +In the timing diagram above is shown that the debouncer will wait while the digital input bounces. |
| 86 | +After bouncings stops the delay time will elapse. |
| 87 | +Only then the state of the digital input is read to detect the state change. |
| 88 | + |
| 89 | +The debounced signal will be active for a duration which depends on the bouncing while releasing the button. |
| 90 | +If bouncing takes the same time while pressing and releasing, the active duration will be the same as the pressed duration. |
| 91 | + |
| 92 | +The debounced state will be delayed by the bouncing time and the defined startup delay. |
| 93 | + |
| 94 | +\include{doc} bouncing-delay-too-short.puml |
| 95 | + |
| 96 | +An advantage to the previous approach can be seen with short pulses. |
| 97 | +If the button is pressed and then released while still bouncing (or withing the delay), no state change will be adopted. |
| 98 | +Too short button presses do not lead to adopt only one of two (or more) state changes. |
| 99 | +This is the safer approach. |
| 100 | + |
0 commit comments