1 Background
When physically rotating an encoder or pressing a button, two pieces of metal come into contact with each other. The two pieces of metal can make and break contact intermittently before they are in full contact. When the input signal crosses the digitization threshold, it could result in multiple pulses of varying duration in the digitized signal, as illustrated in Figure 1-1. Because of the high sample rate of the input signal line for the microcontroller to process, multiple pulses on the input signal line can result in one press/release to be registered as multiple triggers. Ideally, short period pulses of the digitized input signal are just noise, and these noisy pulses should be filtered out. Only a stable pulse with a long enough period, which means the input signal is stable for a certain time, should trigger debounce. This method is called “debouncing”.
- Scenario 1 refers to the case when the encoder is rotated to ON
- Scenario 2 refers to the case when the encoder is rotated to OFF
- Scenario 3 refers to the case when the encoder is mechanically loose or halfway rotated, making the input signal very unstable
Debouncing is a type of low-pass filter. The debounce time is the delay between the occurrence of the trigger and the registration of the trigger. This debounce time influences the accuracy of the timestamps. For scenarios 1 and 2, the edge of a trigger indicating a stable pulse should be registered after the debounce time, named debounce signal. For scenario 3, many short period pulses can occur in a very short period on the digitized signal. The user should, in advance, define if these short period pulses are noisy pulses to be ignored, or if it could be considered as a valid and stable pulse in a certain condition.
In this document, a rotary encoder has been used as an example for demonstration purposes. However, the same technique applies to also buttons, switches, keypads, and knobs.