In many products, the user may provide input through a push button or a switch. And often, a switch acts as a control device. Examples are:
- a limit switch on a sliding or rolling machine element. A familiar application is in a garage door opener.
- an event indicator. In a bicycle computer each wheel rotation or chain ring rotation produces a pulse (a switch closes then opens). In a weather station, the anemometer produces a switch pulse per revolution and the rain gauge produces one for each bucket tip.
- an on-off element. Typical examples are a household thermostat and a water heater thermostat. These switches turn a heater on when the temperature drops to a low set point and turns a heater off when the temperature rises to a high set point.
In the ideal case, when a switch closes or opens the observed electrical behavior should be a perfect step function. In the real world, a mechanical switch’s contacts have a non-ideal behavior called bounce.
This behavior can be readily observed on an oscilloscope. In the image below, a switch closes, connecting a circuit point to ground. Notice that the voltage bounces down then up several times before settling at the ground level.
Image courtesy of Dr. Andrew Greensted.
There are many ways to deal with this behavior. In every case, the goal is to debounce the switch.
For a microcontroller-based product, it is usual to debounce a switch without using additional external components: cost conscious design. Again, we want to treat the button press as a one-shot event. In other words, we must catch the first “make” (switch closing) or “break” (switch opening) as a single, isolated event. To accomplish this, we add a fixed time delay after the initial detection and ignore the switch until such delay is complete. The length of the delay must be longer than the worst case bouncing behavior of the switch.