easily changed within the software.
Debouncing Switches
When one pushes a keypad or closes any switch for
that matter, the switch contacts tend to bounce, making
and breaking contact several times before assuming their
new state. This 'contact bounce' is highly variable between
switches, and even varies from activation to activation of
the same switch. When viewed with an oscilloscope, one
might see the switch open and close a half dozen times or
more during its activation. The duration of the individual
bounces is variable, and could be up to a millisecond or
two, although shorter pulses are common. It may take up
to 10 ms or even longer for the switch to assume a stable
state. This bouncing occurs with both opening and closing
mechanical contact switches.
When one turns a ceiling light on or off, the bounce
of the switch is imperceptible. When one is entering an
access code, however, switch bounce is very important.
If the circuitry and software do not account for switch
bounce, the microcontroller might register one keypad
press as multiple presses of the same key, making it
impossible to correctly enter the code. Entering 1, 2, 3, 4
might be read by the microcontroller as if one had
entered: 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 4, 4, 4, 4, 4.
Although one can eliminate the bounce in hardware
by placing a capacitor across the switch contacts, this
requires additional components and circuit board space.
For high voltage circuits — where each bounce is
associated with a small spark and the generation of
electromagnetic interference — this approach may be
needed. For this project, however, debouncing is done
in software.
Many techniques exist for debouncing a switch in
software. Perhaps the simplest technique — employed in
this case — is a simple timer. Here, when a key is pressed
its value is stored and the microcontroller just waits 75
ms before doing anything else. The switch may bounce
numerous times, but these will be ignored by the
microcontroller. After 75 ms, the microcontroller watches
to see when the key is released. It again waits 75 ms to
avoid detecting any switch bounce on the keypad release,
and then resumes its main loop, watching for key
presses. This may seem like a long time interval to wait to
insure that one has skipped over any keypad switch
bounces, but even taking 150 ms to read each key press
would still allow the user to enter the access code at
over six digits per second. This is much faster than the
normal individual could possibly press the keys. A more
efficient algorithm to detect key presses and eliminate the
■ FIGURE 1. The garage access keypad schematic.
The keypad connects to port B on the microcontroller via
a ribbon cable. The microcontroller interfaces with a
matrix keypad, relays, LEDs, and a piezoelectric beeper.
A generic five volt power supply rounds out the circuit.
February 2009 37