You are on page 1of 13

PICkit™ 1 starter kit / CoreChart Exercise:

The Science of Switch Debouncing


Switches (two contacting surfaces) are electromechanical devices that play an
important and extensive role in practically every computer, microprocessor,
microcontroller application and many other different types of functions.
Mechanical switches are inexpensive, simple and reliable, though they are much
slower than an electronic circuit. Switches are used to make or break an electric
circuit by a manual or mechanical operation, which creates the switch’s
connection.

Open switch the break Closed switch the make

Switch Selection Criteria: Some of the more important criteria are as follows:

Switching Speed. Duration of that the contact travels during the make or break
function. It is generally desirable to have high speed during the make function to
minimise the duration of an arc or flashover. If the duration is excessive, the
contact surface will deteriorate and welding of the contacts may occur. Arc-
suppression techniques should be used if higher currents are anticipated. During
the break, it is generally desirable to have a slower speed to minimise both state
and time transients, particularly for DC (Direct Current) and inductive currents.

Electrical Noise. Electromagnetic radiations may occur during the make or


break of switches, which causes interference in sensitive circuits or high-gain
amplifiers. Suppression of arc may be necessary to reduce such noise to
acceptable levels.

Contact Snap-over and Bounce Time. Snap-over time is the time a contact
separates from a normally closed position and travels to a normally open position
and makes contact with the circuit. Bounce time is the interval between the initial
contact and steady (stable) contact during which contact bounces as a result of
impact of a moving contact onto a stationary contact. These times should be
kept to a minimum because the time intervals and/or the contacting instability
may influence critical or sensitive circuits.

1 / 13
Figure 1: (Left) Generalised digital signal switch bouncing and (Right) Two
switches connected in parallel.

SWITCH INSTABILITY

THE PROBLEM OF SWITCH/Relay BOUNCING

When a switch is closed (pressed, make) or opened (released, break) the


mechanical contacts do not make or break a clean (not a step function) electrical
signal connection instantaneously on the time scales of digital systems. They
can bounce or oscillate (make noise) between its closed circuit state and its open
circuit state, often making several or hundreds of transitions during the tens of
milliseconds (10 ms) that are required to close or open a switch, until it has
settled into its new state. Bounce is due to the effects of switch age, dirty switch
contacts, switches that are switching a heavy load, operating inertia, mechanical
design and the microscopic condition of the switch-contact surfaces. This
problem of switch bounce, occurs whether the two contacting surfaces are made
out of the same materials or of different materials (decreased switching speed,
increased noise and increased bounce time). During switch bouncing there is
some intermittent activity that is due to the switch contacts actually bouncing off
each other.

Imagine slapping two billiard balls together…

2 / 13
Table 1: Two same material switch contacting surfaces.

Two billiard balls slapping together: Same contacting surfaces

Mechanical switch movements

Digital signal pulses

Switch stages
Initially making contact Stabilising contact Switch makes the circuit
Unstable state Changing state Stable state
Transient state Settled state
Steadying state One single state
Transient signals send Transient signals send Switch completes circuit
Switch mechanically Stabilising, Settling
jittering/bouncy

3 / 13
Special Case: 2 DIFFERENT SWITCH SUR FACES

A hard non-resilient material does not absorb the kinetic energy of motion.
Instead, the energy dissipates over time and friction in the bouncing action acts
against the forces pushing the billiard balls together. Hard metal switch contacts
react in much the same way. Also, switch contacts are not perfectly smooth. As
the contacts move against each other, the imperfections and impurities on the
surfaces cause the electrical connection to be interrupted. The result is switch
bounce.

Two hard metal contacts or Two uneven surface contacts

Figure 2: (Left) Two switches connected in parallel and (Right) A close-up of


two different contacting switch surfaces.

4 / 13
Table 2: Two different materials switch contacting surfaces.

Two billiard balls slapping together: Different contacting surfaces

Mechanical switch movements

Digital signal pulses

Switch stages
Initially making contact Stabilising contact Switch makes the circuit
Unstable state Changing state Stable state
Transient state Settled state
Steadying state One single state
Transient signals send Transient signals send Switch completes circuit
Switch mechanically Stabilising, Settling
jittering/bouncy

5 / 13
The Consequences of switch bouncy contact

The consequences of incorrect switch bounce can range from being just
annoying to catastrophic. For example, imagine advancing the TV channel, but
instead of getting the next channel, the channel selector (switch) skips one or two
channels. This is a situation that a designer should strive to avoid. Switch
bounce has been a problem even before the earliest computers. Similarly with a
microcontroller, if debouncing is not implemented, the microcontroller may read
these rapid transitions, as rapidly switching on/off signals and malfunction will
result. Bearing in mind that all bouncing is an erratic action that can wreak
havoc on data, because the exact number of bounces does not necessarily
repeat in the long term. Switch bounce is not consistent from unit to unit, lot to
lot, or even over the life of an individual switch. Membrane switches and some
other types do not appear to bounce when new, but all mechanical switches
bounce at sometime. Though, nothing can ensure that another switch of the
same type will act in the same way, or that a particular switch will remain bounce
free as it ages.

The Correct switch debounce

The term Debounce applies to a technique for overcoming a common problem


that, mechanical switches do not open and close as cleanly as we might like.
They tend to bounce or open and close before settling into a state. A bounce
might last from 5 to 30ms (5 to 30 milliseconds (10-3)). This can cause problems
for software that is looking for switch closures.
The proper way to debounce a switch is to:
• Scan (detect) for a switch state change;
• Delay (wait: the generation of a regular interrupts which are either
mechanically or software operated) of 10 ms to ensure that the signals of
rows and columns have reached a stable state;
• Test to detect if there has been a change in state and
• Act on the input from the switch.
If a switch state is detected, it is assumed to be bouncing, so we start over. If the
Switch State has not changed, it is assumed that the switch is stable.

6 / 13
Debouncing for more than one switch

If more than one switch needs to be debounced, repeat this process


independently for each switch, even though with only two switches things get
complicated, fortunately there is a way that to debounce all of the switches as
one, by using some Boolean arithmetic:
• Read the switches, and then store them in memory as current states.
• Compare the current state against the previous state by using an
exclusive-or (XOR) of the current state and previous state, the result is
what has changed, which is saved in memory.
The exclusive-or indicates changes, it does not tell if the change was an opening-
of or closing-of the switch. To determine if the switch is closed we perform a
logical AND on the data saved in memory and the previous state. This calculates
that a switch that was closed has been debounced and has been released, thus
resulting in a qualified switch closure. Finally, save the current state in memory.
This process is repeated at time intervals of approximately 30ms.

Design

Switch bounce is the intermittent contact and release of switch contacts. Two
parameters characterise switch bounce: bounce period (T or τ [Greek letter Torr])
and bounce duration (t = time). The bounce period is the random length of time
that the contacts remain open, or closed, while the bounce is occurring. Where,
bounce periods can vary anywhere from a few nanoseconds to a few
milliseconds. The difficulty is that bounce duration is indeterminate. Thus,
bounce abatement design starts by making assumptions based on empirical
data. Where, the first assumption is that the bounce period will be absolutely
less than 10 milliseconds. The second assumption is the total bounce duration is
indeterminate. A good design should work regardless of how long the switch
bounces, provided that it eventually does stop bouncing.

Hardware solution

Where, the debounced switch subsystem provides a switch that closes when
pressed. Whenever a mechanical switch is pressed, the switch contacts will
bounce, which produces several very quick on and off signals. Each of these
signals would be counted by a counter subsystem. To overcome this, a
debouncing circuit is used to produce a clean output signal from the switch.
Such as, by using a mechanical debouncer (latch or sample/hold), which is
needed to cancel out the switch’s bouncing effect. In effect a debouncer
registers the first connection and disregards any bouncing. This will allow for the
counter to increment correctly with each toggle of the switch. Some times switch
bouncing can be removed with an analogue filtering and a comparator with
hysteresis output. Though, unfortunately this will not work for all cases; even if it
did, the extra cost in hardware may not be justifiable, especially since most of the
work can be done in software.

7 / 13
SOFTWARE SOLUTION

BRUTE FORCE DEBOUNCE

Consider a simple push button application routine. In this case, some event
should occur when a button is pushed. Ideally, the invoked event will occur
immediately and only once for each button push. The system should also be
ready to respond to a repeat button push as soon as possible after the button is
released. This presents an apparent dilemma. How is the difference between
switch bounce and repeated button pushes determined? Recall the assumption
that the bounce period is less than 10 milliseconds. If the switch-input level is
stable for longer than 10 milliseconds, then bouncing has stopped and the input
level represents the pushed or released Switch State. The Brute Force method
only cares about a button-push event because this is what invokes the action. It
recognises the switch Release State as the stable state and everything else is
considered unstable. When the switch becomes unstable, the action is invoked
permitting nothing to happen until the switch returns to the released stable state.
The flowchart in Figure 3 outlines the software actions that are necessary to
implement the Brute Force debounce method. Notice that the debounce loop is
looking for a stable released state for 10 milliseconds before returning to the top
of the main loop. Any instability, including a stable button pushed state, resets
the debounce function 10 millisecond timer.

This program is a debouncing routine that samples the switch's state at a high
rate. When a change in state is detected, the routine will count anywhere from 1
to 4 to 8 to whatever samples to make sure the transition was not a glitch and
that the switch has settled into its new state. If there are many switches that
need to be debounced simultaneously, it gets rather unwieldy keeping track of
the counters and operations. This is empirically demonstrated by the brute-force
debounce routine.

8 / 13
Figure 3: Outline of the necessary software actions that implement the Brute
Force debounce method.
Notes:
1. NOISE - Unwanted Random Signal in the electronic circuits.
2. KINETIC ENGERY - Force that makes things moves.

9 / 13
Figure 4: CoreChart Main Program for the Switch Debouncing.

10 / 13
Figure 5: CoreChart Initialisation Subroutine for the Switch Debouncing program.

11 / 13
Figure 6: CoreChart ToggleLED Subroutine for the Switch Debouncing program.

12 / 13
Figure 7: CoreChart Debounce Subroutine for the Switch Debouncing program.

13 / 13

You might also like