ChipCenter Questlink
SEARCH CHIPCENTER
Search Type:
Search for:




Knowledge Centers
Product Reviews
Data Sheets
Guides & Experts
News
International
Ask Us
Circuit Cellar Online
App Notes
NetSeminars
Careers
Resources
FAQ
EE Times Network
Electronics Group Sites

  Analog Avenue

    Columns

Archives | Data Sheet

Switch Bounce and Other Dirty Little Secrets
By John Wettroth,
Maxim Integrated Products, Inc.

Introduction

There is a dirty little secret that every engineer learns soon after he or she tries to connect a switch or a relay to a digital system; switches can do some really odd things . . .

Due to the microscopic surfaces of contact, mechanical design, aging, inertia and a host of other issues, switches don’t make and break cleanly on the time-scales expected by digital systems. Instead, switches make multiple transitions in the millisecond timing range when opening or closing. This behavior is commonly called switch "bounce" and it is an absolute and inescapable fact of life. If you were to connect a standard switch to a digital counting circuit, you would get several counts on opening and several counts on closing, but the exact numbers would not be repeatable in the long term and would wreak havoc with getting any useful counting data. Oscilloscope traces for two different types of switch (see Fig. 1) give a feel for what the waveforms look like.

Analog Avenue Column
Fig. 1a: Switch-Bounce Rising Edge for small push-button switch on "make". Horizontal scale at 2 ms/Div. Bounce interval is about 10 ms, 6 transitions total until settling. Note increasing frequency with time.

Analog Avenue Column
Fig. 1b: Switch-bounce rising edge for 5-A contact relay on "make". Horizontal scale at 2 ms/Div. Bounce interval is about 8 ms, 13 transitions total until settling. Note several runt pulses with higher frequencies at beginning and end.

Note that switch bounce is highly variable. It is not completely consistent from unit-to-unit, lot-to-lot, or even over an individual switch’s life. You can even test some switches that appear not to bounce. I have tested several membrane switches that didn’t bounce, but nothing says that another of these switches will act this way or that this same switch won’t bounce as it ages. All mechanical switches bounce sometimes.

In addition to bounce, switches and digital systems have other annoying habits. When cabling to switches is run in a noisy industrial environment, strange things happen. Since an open switch is, by definition, a high impedance, interfering signals have an easy load to work against. Any noise impulse that is capacitively- or inductively-coupled to the wiring can cause phantom switch closures. Imagine a programmable logic controller (PLC), a kind of specialized industrial computer, controlling a motor through a hefty relay. A limit switch is placed near the motor to give the PLC some position feedback and is interfaced to a digital input on the PLC. When the PLC tells the motor to start, a surge of current flows out to the relay and motor. This might cause ground bounce or a capacitively-coupled spike in the digital input due to the coupling in the long wiring runs. The PLC, if not properly designed, would interpret this spike as a premature switch closure shutting the operation down. When the PLC turns off the load similar things happen due to the inductive-kick of the relay, motor, and wiring capacitance/inductance. These spikes and transients can cause erroneous readings on digital inputs if the PLC and its digital inputs are not properly designed.

Finally, digital and switch inputs in industrial, office, and even home environments, are subject to over-voltage conditions, voltage transients and ESD strikes. Over-voltages can come from power supply sequencing issues, where the power to one box is on but power to another interconnected box is not, improper wiring or from fault conditions that should never occur. Voltage transients are often associated with capacitively- or inductively-coupled spikes as discussed before. ESD strikes are very common on operator consoles, terminal strips during installation, connectors and elsewhere. Any of these transient conditions can be destructive if system latch-up occurs. Even if not destructive, they can cause erratic operation like a CPU reset or a watchdog overflow.

Designers of all systems should be aware of these problems and the methods used to combat them. Maxim has recently introduced a new series of parts that were developed to address these real-world interfacing issues. They offer foolproof, software-free debouncing, ESD and over-voltage protection in several easy to use and inexpensive configurations. These parts are the MAX 6816 (single), MAX 6817 (dual), and MAX 6818 (octal) switch debouncers.

Switch Bounce

If you ask most engineers about fighting switch bounce, they will almost universally report that it is "done in software and is no problem." And software is a good place to do it, generally, if proper attention to detail is given.

Debouncing switches with resistors and capacitors are possible too, although in order to do a proper job this requires a pull-up resistor, a series resistor to a capacitor, a resistor to the input of a Schmidt trigger buffer, and often a diode: To make sure that the charge on the capacitor doesn’t force a lot of current through the buffer input-protection network at power down. This is a lot of parts and it can be seen that this technique can get a bit unwieldy for multiple inputs, and this method won’t be covered in any detail here.

Software debounce is the mainstream method used today. Good debounce routines are actually a subtle type of real-time software being, basically, a simple digital low-pass filter. Digital inputs are often sent through debounce filtering even though they might not be switch inputs. Short transient inputs caused by spikes and transients can be eliminated by using the same techniques. The object is make sure that the switch or an input is in a stable state before reporting it open or closed.

The pseudo code below is a mediocre software debounce routine for one input. Multiple inputs can be handled by generalizing this routine and using pointer-based variables, etc. Though this type of routine is often used, it has several problems and flaws that we will discuss.

Action Comments

1. Check input timer An ISR usually sets a timer bit that is polled in main routine

2. Return if no timer Go do something more useful

3. Get input bit the "bouncy" input

4. Count++ if high, clear else increment a counter if input is high

5. If count> 4, state = 1, else 0 check counter - also clamp it at 4

6. Return input state state is debounced

The routine above is flawed in some fundamental way - consider the following software issues:

  1. One of the most basic problems is that it only debounces switch closures, any openings even while the switch is bouncing is considered open. This unintended asymmetrical operation might be good for things like keypads since action is taken only on key presses, but for a general purpose input, debouncing of both edges should be implemented.
  2. The next most basic and related short coming is that it assumes that if a switch is not closed, it is open; there is actually a third state: "Unstable" or still bouncing. The proper way to report switch state is with the last non-bouncing state until a new debounced state is reached. Even this can cause problems and a third state of "changing" might sometimes be required.
  3. Many debounce routines will wait for an input to be in the same state some number of times with a delay between samples. If the switch changes state in this routine, the other state will be tested for N times until the switch is stable. This can cause large delays that eat up a lot of CPU time. In an extreme case of this, imagine a PLC with a general-purpose input and high-frequency applied to a port. This might be inadvertent, deliberate or due to a failure. For whatever cause, it would completely hang the processor. Hopefully a watchdog would bring the processor back but it would recur indefinitely: Not a robust design.
  4. Imagine a large industrial system with lots of inputs. Each input would require a closed counter, an open counter and two state bits that define its state. For a PLC or general-purpose input board with 64 inputs, this would be require a lot of RAM and code to implement.

Transient and ESD Suppression

The standard solution here is a transient suppressor or MOV device at each external input. This is common in industrial and automotive systems where engineers know, and believe, the perils of not including protection. It is straightforward and relatively inexpensive but can add a bit of clutter and real estate for more than a few inputs, although Quad and Octal devices are available that can help. It is also be wise to put a 220-W resistor in series with the Vcc line of port input devices. A common CMOS octal input device like a 74HC244 or 74HC573 draws little current and, should it latch up, this 220-W will limit the current and power dissipation to a safe level. Power-cycling might still be necessary though. Microcontroller port pins should generally not be brought out directly to "real-world" inputs: Beside these latch up issues, radiated EMI will likely be an even bigger problem!

Some designers believe that resistors put in series with CMOS digital inputs will protect against these problems. The logic is that you cannot latch up a part until you inject sufficient current into a pin - and this is true, the "SCR latch-up threshold" of modern CMOS ICs can be 50 mA or more. This is actually a somewhat effective way of protecting for over-voltages (discussed next) but is not effective for ESD, since a 15-kV ESD strike can force a significant amount of current through even a 100-kW resistor and will go around resistors using parasitic paths.

Over-voltage Protection

Over-voltage protection is the ability to withstand continuous or longer-term transient inputs outside the rails. Series resistors, acting against protection diodes tied to the rails, are effective here. A common worst case is a condition where there is no Vcc applied to the input port IC and there are 24-V inputs from an external source. An external source like this can often "backdrive" the protection networks and force voltages onto the power rail inside a system. A Zener diode across the Vcc rails of the input port IC should be considered. Calculate the power dissipation of this Zener as well as the maximum power dissipation of the input series resistors to make sure that in a worst case conditions, the protection circuits don’t fail.

The Maxim Switch Debouncers

Maxim saw a need for a simple interface device that provided debounce, ESD protection and over-voltage protection. The company had seen customers use our supervisors with a debounced manual reset input like the MAX811 just to get a single channel debouncer in a SOT-23 package. We also saw some industrial customers using our ESD-protected 232 interfaces as general-purpose digital input devices. The fact that 232 receivers transition at low voltages but can withstand high voltage and ESD were the features that customers found useful. This feedback produced a line of switch debouncers with ESD and robust input features (see block diagram Fig. 3 and application Fig. 4.)

Analog Avenue Column
Fig. 3: Block diagram of the MAX6816 family of debouncers. An ESD-protected and over-voltage input structure is followed by a digital filter that debounces the input and applies under-voltage lockout.

Analog Avenue Column
Fig. 4: Typical single debouncer application using MAX6816 in SOT-23-4. Note, no external components are required.

The MAX6816 (single) and MAX6817 (dual, in SOT-23-6) provide all the basic features of the family including the debounce logic/digital filter, input over-voltage protection (which handles ý25 V levels) and ý15 kV ESD-protection for use in harsh industrial environments. They feature single-supply operation from +2.7 V to +5.5 V and draw 6 m A of typical supply current. They also offer under-voltage lockout circuitry that ensures the output is in the correct state upon power-up. They require no external components and even include a 63-kW pull-up resistor for direct interfacing of switches. The nominal debounce delay is 40 ms with a minimum of 20 ms and a maximum of 60 ms, a range for even the ugliest of switches.

Analog Avenue Column
Fig. 5: Timing diagram for the debouncing function. Note that the output changes state a nominal 40 ms after the inputs are stable. The MAX6818 offers an additional change of state output that indicates a change in inputs to reduce polling overhead in multiple input systems.

The MAX6818 (octal) adds a switch change-of-state output (CH), and data bus, simplifying microprocessor (ýP) polling, or interrupts and interfacing. Each time the part is read out with the enable line, the CH pin is reset and will go low again if any input changes state. There is also a three-state output bus controlled by an enable (EN) pin.

Analog Avenue Column
Fig. 6: Typical applications circuit for the MAX6818. Note the outputs go to the uP data bus D0 through D7 and are three-stated until enable, EN, is brought low. The Change Output, CH, is reset high on each read and is set if any input changes state. In this circuit it is tied to an interrupt, it could also be polled.


John Wettroth is a senior engineer in Maxim's Standard Products Division. He sets direction for a small group of product definers in the area of switches, supervisors, interface, power and related technologies. Before joining Maxim, he developed portable instruments at SAIC. He lives in North Carolina and can be reached at jwet@mindspring.com.

Analog Main | Product of the Week | Columns | Editorial | Tech Notes

Click here to get your listing up.

Copyright © 2003 ChipCenter-QuestLink
About ChipCenter-Questlink  Contact Us  Privacy Statement   Advertising Information  FAQ