Interfacing switches and relays to the real world in real time
Designing the external interface to an industrial or automotive application can be challenging for an uninitiated systems engineer. The bouncing of switch and relay contacts can produce arcs that threaten system reliability. ESD can also threaten reliability and uptime. Fortunately, there are ways to ease the task of designing an interface between the inputs of a microcontroller and a hostile industrial or automotive environment.
Phill Leyva, Maxim Integrated Products -- EDN, 6/7/2001
Textbooks depict the ideal operation of switches and relays as a single transition that occurs instantly on actuation followed by zero on-resistance. Unfortunately, this ideal operation has never existed. Real switches and relays exhibit a finite resistance called "contact resistance," which increases over time with the number of actuations. Furthermore, as a switch or relay changes state, the contacts travel through several open-close cycles called "contact bounce" before coming to rest in the final state. A digital control system could interpret these transitions as four separate contact closures. Contact bounce is unacceptable because automotive- and industrial-control systems require accurate, real-time data in the form of definite state changes (Reference 1).
Specify switches for load, ac-voltage rating
Contact resistance for new switches ranges from less than 50 mW to 100 mW , depending on the contact material, power loading, environmental conditions, and switch usage (Reference 2). In Figure 1, a pushbutton switch supplies 24V, which is a typical industrial-control voltage, to the resistor. At least four major switching transitions occur before the output comes to rest at 24V. In some cases, this rapid contact bounce produces a changing resistance as the moving contact wipes across the stationary contact.
Factors that affect the rated capacity of a switch include the power factor, ac or dc voltage, voltage amplitude, the magnitude of current through the switch, and the type of load, such as a lamp or motor. The load can be resistive, inductive, or capacitive. Manufacturers typically specify a switch's ac-voltage rating at a given current level and power factor. The operating voltage must be less than this ac-voltage rating.
The type of load on a switch drastically affects its rating. A resistive load, such as a tungsten heater, is the least stressful. Both inductive loads and motor loads place additional stress on the switch as it opens and closes. The bouncing switch contacts can encounter inrush currents three to 10 times greater than steady-state levels. Bounce occurs during both opening and closing, and the resultant arcing causes contact wear, higher resistance, and lower reliability.
Lamp and capacitive loads represent the worst case. At the instant of switch closure, both of these loads subject the switch to a short circuit. A lamp's cold resistance is close to 0W , and a discharged capacitor is a short circuit at the instant you apply voltage. This state change can produce inrush currents as many as 100 times the steady-state value. To compound the problem, switch contacts are bouncing during this short-circuit interval (Reference 3). High current levels and bouncing contacts produce severe arcing across the switch contacts, which causes contact erosion. Worse, repeated cycles of switch operation with severe arcing can create a short circuit by welding, or fusing, the contacts together.
Contact material and power level
Other items to consider when specifying a switch for automotive or industrial environments are power level and contact material. You usually specify silver contacts for power levels greater than 0.4 VA, known as high-level range or wet-contact rating. This power level allows enough arcing to remove any silver tarnish, or oxidation, which increases the contact resistance. Thus, minimal arcing is beneficial, but the excessive arcing caused by a lamp or capacitive load destroys the switch contacts.
For power levels of less than 0.4 VA, known as the low-level range or dry-contact rating, you should specify gold-plated contacts. Because these power levels lack sufficient energy to cause arcing, silver contacts become encrusted with contact oxide, which is an insulating material. Therefore, the contacts fail to close the circuit. Plating the silver contacts with gold, however, prevents tarnish and thereby extends the contact life to that of its mechanical-life rating. Also for the low-level range, you can use bifurcated contacts, which are two-pronged parallel switch contacts, in place of domed contacts. The two prongs provide a wiping action that maintains reliability by helping to remove the contact tarnish.
To avoid signal degradation due to contact wear, you should not route signals through switch contacts that also perform power-level control, such as contacts that control motors, lamps, and solenoids. Also, any contacts that provide signals to a µC or an industrial-control system must incorporate signal debouncing using hardware or software (Reference 4).
With design cycles that now last months instead of years, discovering midway through a project that your µC in not operating in real time because its internal resources—timers, RAM, interrupts, and even polling to debounce inputs—are stretched beyond capacity has become a serious problem. You need a design approach that reduces the total system cost, places minimal dependence on the µC (software and RAM), requires minimal pc-board space, and contributes to system reliability.
Debouncing for clean state changes
As Figure 1 demonstrates, a simple ulldown resistor is not enough to enable a switch to make a clean and definite state change. Figure 2 illustrates a typical textbook solution to the switch-bouncing problem. Two resistors and two NOR gates form an R-S latch (Figure 2a). The two resistors pull down the R or S input when the switch is open and limit the flow of current to ground if the switch contacts fuse. A truth table for the R-S latch illustrates its operation (Table 1).
The Q output is logic high when the switch setting is in the S position. Thus, contact bounce simply causes a logic low at the S input. This input is a holding state because the pulldown resistor at R holds this input at logic low. The converse holds when the switch is in the R position: Q is at logic 0 and also maintains a holding state in the presence of contact bounce (Reference 5). This circuit is acceptable, but it requires two extra NOR gates. There is room for improvement.
One disadvantage of this debouncer circuit is the spdt switch, which is more expensive and larger than an spst switch. The larger switch, resistors, and NOR gates require more pc-board space. Further, the truth table's "unstable state" indicates that the state of Q is uncertain when both inputs are simultaneously logic high. This condition can easily occur when troubleshooting in the field—if, for instance, a meter lead shorts the low and high inputs.
Make-before-break switch contacts allow unstable states to occur, so the contacts for this circuit must be break-before-make. Because the circuit does not provide voltage-level shifting, you must run three wires to the switch. The additional circuitry and pc-board space increase system costs.
An alternative technique uses a switch-debouncer IC to reduce the component count, the power consumption, and the pc-board space (Figure 2b). IC1 is a CMOS switch debouncer that connects directly to an spst switch. The IC's input has a 63-kW pull-up resistor for logic-high inputs, which saves pc-board space. Capacitor C1 decouples the VCC pin. Closing the switch pulls the IN pin low and provides a logic low at the OUT pin. The OUT pin does not change state until the IN pin is stable for 40 msec, which hides the effect of any contact bounce.
A not-so-obvious advantage of this circuit is resistive loading of the switch by the internal 63-kW resistor, which ensures reliability and an indefinite switch life. Disallowed states do not exist in this circuit, because the input is either low or high. Moreover, the IC's undervoltage-lockout circuitry ensures a condition crucial in automotive and industrial applications—that the OUT pin is in a known state during power-up. The circuit improves reliability and lowers system costs by requiring fewer components, a less-expensive spst switch, and only two wires connected to the switch.
Deal with relay bounce
Like switches, relay contacts have a finite contact resistance that increases over time with repeated actuations. The typical contact resistance of a new relay ranges from less than 50 mW to 200 mW (Reference 6).
The actuation of a relay is also mechanical. Unlike the switch, however, an spst relay has only one movable contact, which connects electrically by a wire to one of the external-contact terminals. The stationary contact connects to the other external contact. Relay contacts also make several open-close cycles of contact bounce before coming to rest in the final state. This contact-bounce interval is in addition to the relay's operate and release times, which can measure tens of milliseconds.
Figure 3 shows the voltage across a resistive load after actuating an industrial relay that supplies 24V to the resistor. At least 12 major switching transitions are evident before the contacts come to rest at 24V. An automotive µC or industrial control system would interpret these transitions as multiple cycles of the relay contacts.
Similar to switches, a relay's worst-case loads are lamp and capacitive loads. These loads subject the contacts to additional stress by creating a short circuit at the instant the relay contacts open or close. You should, therefore, specify the contacts of a relay for the type of load you expect in the application. Unlike switch contacts, manufacturers rate relay contacts by their ac/dc resistive-load and "contact-horsepower" capabilities. The contact-horsepower rating provides the maximum motor loading that a relay can withstand without premature failure.
Relays that you use in industrial- and automotive-control systems for the purpose of isolating one power source or ground from another are subject to the same "dry" and "low-level" ranges of power dissipation. You must specify gold-plated or bifurcated relay contacts for signal-power levels of less than 0.4 VA. If you use a power-level relay with silver contacts, you can soon expect field failures. Contact bounce with high-level current flow subjects the relay contacts to the type of contact failure you see in switches. Again, signals that power motors, solenoids, or similar components in an automotive- or industrial-control system should not route through contacts. You should use a separate set of contacts for these signals.
As signal translators, the relay and resistor are inadequate for providing a clean signal to a µC or control system. Figure 4 shows a textbook debouncer applied to relay contacts for a system µC (Reference 7). A process sensor, such as a remote pressure switch, actuates the relay mounted on the control board with 24V dc. The time-constant value of the RC lowpass filter should be large enough (20 to 200 msec) to mask any bounce time that the relay contacts introduce. As each capacitor charges and discharges, the corresponding Schmitt-trigger gate provides output changes with hysteresis and a sharp, definite switching transition. This circuit requires polling software, and its high parts count requires more pc-board space. For a typical 8-bit input module, the circuit includes two 74HC14 Schmitt-trigger ICs, eight capacitors, and 16 resistors.
Working with polling software
Listing 1 is a pseudo-port-polling subroutine that monitors the relay in Figure 4 for changes in the contact state. For each pass through the main loop, the main program jumps to the Check_Port subroutine, reads input data, and stores it in the Port1 memory location. The subroutine compares Port1 data with old port data in the Port_P memory location, and performs a bit test at line 4. If no input bits have changed since the last reading of Port1, or Port_P contains previous data, the program execution exits this subroutine and continues again at the main program. If bits have changed, the subroutine adds "1" to the Count memory location. The routine then compares the Count value with the constant "N_Pass_Value," which is the number of passes through the main program before the program assumes the relay is debounced. If the Count value is less than N_Pass_Value, execution returns to the main program. If Count equals N_Pass_Value, the subroutine sets Count to zero and copies the new Port1 data into Port_P, and execution passes from the subroutine back to the main program.
An alternative approach requires far fewer components and relies less on software to determine when a relay or switch contact has changed state (Figure 5). It, therefore, improves data-processing time in the µC or control system. A CMOS switch-debouncer IC feeds all eight inputs to the µC. A change-of-state pin,
, goes low when a state change occurs.
connects to the Interrupt Request pin,
, of the µC. The µC toggles the IC's
pin low to read the output data and resets the
signal to high. This approach does not require a polling routine, only a simple interrupt-servicing routine. The result is a significant improvement in real-time data processing by µCs in automotive and industrial applications.
Listing 2 shows a pseudo-code interrupt-service subroutine for the circuit in Figure 5. Listing 2 does not appear to have an advantage over Listing 1 in code length, but its advantage is significant for processing input data in real time, which automotive and industrial applications require. Most importantly, the interrupt-service routine runs only when new data is available, which is when the relay or switch changes state. This action allows the processor to perform other time-intensive routines and handle the data closer to real time. The Check_Port subroutine in Listing 1 must run during each loop of the main program, regardless of whether new data is available. This subroutine also requires an extra byte of RAM for Port1 data, which may be unavailable when you are using an 8-bit µC in a real-time data application. As mentioned earlier, system resources, such as RAM and real-time data processing, show signs of strain only late in the design cycle.
Figure 6 with Listing 3's pseudo-polling debouncing routine checks for changes in the state of the relay contacts with minimal use of hardware, resistors, or software. The resistors pull down the µC input pins when the relay contacts are open. R depends on the type of specified relay contacts, considering the contact material and load current. Program execution jumps to the subroutine DB_Check_Port (Listing 3) during each pass through the main program. The subroutine (lines 1 through 3) reads input data and stores it in the Port1 memory location for comparison with old port data in Port_P memory location. The code at line 4 performs a bit test. If no bits have changed since the last reading of Port1, execution exits the subroutine and continues at the main program. (Port_P contains previous data.) When bits have changed state, a timer/counter loop (lines 6 and 7) marks 50 msec by ticking away CPU clock cycles. After that interval expires, Port1 data transfers to the Port_P memory location for use by the main program.
This approach has two main problems: It wastes CPU time, and it provides no ESD or transient protection on the input lines. Slowing the data-processing time for a timer/counter loop is an unacceptable practice in automotive and industrial applications because these systems must handle sensor and system-control signals in real time. Further, this design provides no input protection for the µC pins because the pins connect directly to the relay terminals. Anyone replacing the relay can easily compromise the µC's integrity by allowing ESD directly into the µP pins (see sidebar "ESD protection").
Automotive environment
Without exception, auomotive environments challenge design engineers. Automotive systems must survive temperatures of –40 to +70°C, continuous mechanical vibration, and contaminants, such as lubricants, coolants, and other liquids. Other potential problems are static overvoltages and reverse voltages, switching spikes, transients from parallel and series loads, low-voltage noise transients, and load dumps.
You must consider the possibility of static overvoltage or reverse-polarity voltage from the vehicle's battery system. The vehicle power rail carries maximum voltages of 24V when jump-starting a dead battery. A µC's power supply must be able to withstand this voltage, as must any input that can short circuit due to a component failure or the action of a service technician. Inadvertently connecting a vehicle battery in reverse exposes all systems that connect to the vehicle's power rail to negative voltage. Referring again to Figure 2b and Figure 5, protection on the debouncer IC inputs can handle ±25V, or a logic high of 25V and a logic low of –25V.
As an alternative, you can protect each input by installing a series resistor and back-to-back zener diodes, or transient-voltage-suppression diodes in parallel with the pulldown resistor of Figure 2a. For further protection, the µC's power supply should also include a reverse-protection diode in series with its input.
When you disconnect or turn off parallel-connected inductive loads, such as fuel pumps, relays, horns, solenoids, and starter motors, they can generate negative-voltage transients as great as –100V on the power rail. Similarly, positive-voltage transients of as much as 100V can develop when you disconnect a series-connected load, such as a switch in the rear of the vehicle, from an inductive component, such as a long wiring harness that supplies 12V to the rear of the vehicle. The wiring harness in a vehicle contains distributed capacitance and inductance that can superimpose ±100V switching spikes on the vehicle's 12V power rail. ESD and transient-input protection is a must in this environment.
When the low impedance of an engine's starter motor is cranking the engine over, the resulting heavy current can produce low-voltage noise transients by momentarily depressing the 12V power rail to levels as low as 5V. This problem is especially troublesome during cold weather when the oil viscosity is higher than during warm weather. Moreover, the starter motor's rotational speed during starting is not continuous but varies due to the effect of mechanical components that attach to the crankshaft. The result is a variation of the level in the low-voltage transient. The electronic-module system controls should, therefore, have sufficient "carry-over" capacity, or power-supply input capacitance, to allow operation during these low-voltage transients. One advantage of circuit integration and fewer components is less power dissipation, which allows a smaller carry-over capacity.
Watch for load dumps
The last hazard to consider is a load dump, which occurs when an air-conditioning clutch, flashing headlamps, or other large load suddenly disconnects from the power rail. If the engine is accelerating or running at high speed, the alternator's field-excitation circuit, which has a time constant of 40 nsec to 400 msec, can subject the power rail to voltages of 10 to 120V.
Designers must consider all the above factors when designing a control system for the automotive environment. Figure 7 shows a simple view of the various loads that connect to a power rail, with their associated distributed inductances, capacitances, and resistances. Connecting a switch that simply signals an event, such as an alarm door switch or a transmission shift-point pressure switch, exposes the controller input to all the previously mentioned transient hazards.
Figure 8a illustrates input protection for a typical HCMOS device that connects to the power rail. You choose R1 and R3 to limit current through the switches when they are closed during dry- or wet-contact conditions and also to pull the inputs up to 12V. C1 and C3, which are typically small, 22- to 100-nF ceramic capacitors, decouple to ground any RF interference from sources such as the ignition system and CB radios. R2 and R4 limit currents to the HCMOS inputs during a worst-case transient voltage of 150V. Finally, C2 and C4 in conjunction with R2 and R4 form an RC debouncer for the input, providing a typical time constant of 50 to 200 msec.
Another alternative employs an IC that includes two ESD-protected debouncers in a SOT-23 surface-mount package (Figure 8b). At first glance, the transient protection of Figure 8a appears to be missing, but that protection is unnecessary for the IC inputs. R1 and R3 are unnecessary because IC1 includes a 63-kW resistor. C1 and C3 are optional and typically unnecessary because the input protection is internal to IC1. Built-in resistors in IC1 limit input currents.
Finally, the RC debouncer combination of C2 and C4 with R2 and R4 is unnecessary, because IC1 debounces signals for at least 40 msec after the contact bounce has settled. IC1's low supply current (6 µA typical) allows the system to handle low-voltage noise transients with less holdup capacity. The Figure 8b circuit also provides undervoltage lockout for protection when the vehicle battery disconnects or reconnects.
An item overlooked by electronics designers but of concern to automotive engineers is the mechanical shock waves that pressure switches and other transducers transform into electrical signals. For example, a pressure switch monitors the hydraulic fluid in an automatic transmission during acceleration, both to indicate the appropriate shift point for the transmission and to send a signal to the engine-controller µC. Because neither this fluid pressure nor the voltage on a capacitor or current through an inductor changes instantaneously, the pressure output includes overshoot and ringing at the shift point. Thus, the pressure-switch signal requires debouncing not only for contact bounce in the pressure switch, but also to debounce the mechanical shock wave. Switches that monitor coolant pressure also require debouncing because air in the engine coolant causes cavitation, and temperature switches require debouncing to mask mechanical and electrical effects. These mechanical-shock issues also arise in industrial applications.
Author info
Phill Leyva is a senior member of the technical Staff at Maxim Integrated Products (Sunnyvale, CA) where he designs and develops evaluation modules for dc-dc power converters. He has also helped develop dc-dc power converters and hot-swap controllers. He holds a BSEE from San Jose State University (San Jose, CA). His hobbies include camping, bicycling, landscaping, and woodworking.1.Wettroth, John, "Switch Bounce and Other Dirty Little Secrets," http://www.edtn.com/analog/c027.htm, 4/1/99.
2. "Technical Information," EAO World Class Switching Products Catalog 102B, November 1998, pg 104 to 105.
3. "Rerating Current," Design Guide 2000 NKK Switches, Catalog No. 9908, May 1999, pg Z3.
4. Pasahow, Edward J, "An Introduction to Interfacing and Peripheral Equipment," Microprocessor Technology and Microcomputers, McGraw-Hill, 1988, pg 234.
5. Katz, Randy H, "Practical Matters," Contemporary Logic Design.
6. "General Purpose PC Board Relays," Potter & Brumfield General Stock Catalog, Catalog No. 13C222, August 1988, pg 4 to 24.
7. Horowitz, Paul, and Winfield Hill, "Switch Bounce," The Art of Electronics, Cambridge University Press, 1994, pg 576 to 577.
8. Prestopnik, Richard J, "Basic Input and Output Interfacing," Digital Electronics, Saunders College Publishing, 1990, pg 465 to 466.
9. "Automotive Applications," High-Speed CMOS Designer's Guide, Signetics/Philips, 1988, pg 3-39 to 3-44.


















