McGourty Associates
    —
Our Company
    —
Our Products
MechaBlox for Quick Concept Design
    —
Services
Design Services we offer
    —
Customer Support
    —
Mechatronics Designer
Latest issue
    —
Design Files
Application Information
    —
Fundamentally So...
Engineering Fundamentals Review
    —
Designer's Bookshelf
Books for Mechatronics Designers
    —
Resources
Engineering Links
FAQs
The Forum
    —
Contact Us
    —
Conditions of Use
    —

Switch Sensor Interfacing.

Design File DF01, September, 2005


The simplest digital sensor is a two state, on-off or yes-no mechanical switch. It is cheap and easy to use, but caution! The the impact of it's mechanical properties on system performance need to be considered carefully.


Notes:

1.0 The Mechanical Switch

First let's define some standard switch terms. Switches are specified by:
  1. Poles, the number of switch contact sets, and
  2. Throw, which is the number of conducting positions

For example, a SPST switch is a single pole, single throw switch. It has one on position, and one set of contacts as shown in Fig.1.0)

Switches may further be specified by the number of conducting positions, or Ways, for example a Three Way switch.

If the switch is non latching Momentary Action type (typically a SPST), the switch is spring loaded, and returns by spring action to its resting state when released. The contact activation state will be specified as either Momentary Contact (ON) or Break Contact (OFF).

SPST = Single-Pole-Single-Throw
DPST = Double-Pole-Double-Throw

1.1 The Switch as a Digital Sensor

The simplest digital sensor of all is the Single-Pole-Single-Throw (SPST) mechanical switch. It can only assume two states, ON (closed) or OFF (open). It is a passive component and because it is activated by an external mechanical force, it consumes no power itself. Its only function is to complete (make) or disconnect (break) a circuit. Its cousin, the SPDT switch is not much more expensive, functions much the same, and in some applications is the preferred choice - see below Section 3.1.2.

Figure 1.1 shows a typical application of mechanical SPST switch. It is used to signal a "high" or a "low" signal by connecting a device's input pin (Pin1) to Vsupply, or Ground respectively.

Ground connections are typically made directly. Vsupply connections however, typically are made through a current limiting "Pull-Up" resistor Rp. A typical value for Rp may be in the 10k-Ohm to 20k-Ohm range.

A pull-up resistor may not be required if the input device has programmable pull-up capability, - but be sure to turn the pull-ups on in your initialization program! A floating input pin will give spurious results, and sure to cause random errors!

If Switch S1 is off (open), the input signal is "high" and the current consumed by the circuit is determined by Rp and the input impedance Ri of Pin1.

Iopen = V/(Rp + Ri)

If Pin1 is a high impedance input (a CMOS device for example), the effective input impedance Ri is typically in the mega-Ohm range, and the high state input current is approximately

I = V/Ri

and for all practical purposes negligible.

In the "ON" or closed position, S1 completes the circuit to ground, giving an effective input voltage of "0", or a "low" signal at Pin1. In the closed state, the current consumed by the signal circuit is:

Iclose = V/Rp + Ilow

where Ilow is Pin1's specified low signal input current. For the reasons above, Ilow is typically very small and the effective Iclose can be approximated by:

Iclose = V/Rp

Clearly, the low state current is determined by the selection of Rp. To minimize current, increase the value of Rp. However, if too large a value is chosen, Pin1 made be subject to noise.

In general, and especially for battery powered circuits, for lowest current consumption, choose the "high state" or OFF to be the normal, or predominant state for the circuit. If that is not practical and if lowest current is desirable, you can use an inverter such as a 74HC04 between the switch and the Pin1's input.

The NO marking on a switch means normally open. NC means normally closed

2.0 Transient Analysis

A switch is a mechanical device, typically consisting of two spring loaded contact points which close or open under pressure to make or break a circuit respectively. When the switch is closed, and the points make a circuit, a condition called "Switch Bounce" occurs.

Switch bounce is a transient condition of multiple make-break states - or if you like noise, as the switch settles into the closed position. Switch bounce is caused by the points at the moment of closure monetarily rebounding as the points come into contact with each other. Typically switch bounce will last from a few to tens of milliseconds before the signal stabilizes. Switch bounce is typically not as great a problem when the switch opens or breaks the circuit - but depending upon the activation method, it still can occur and should be anticipated.

A few milliseconds may not seem like much, but even eight bit modern microprocessors are capable of running millions of operation a second. For example, an Atmel ATmega8 CPU running at 16mHz clock frequency can execute up to 80,000 instructions in 5ms. It not only plausible, but probable that if the raw bouncing switch signal is processed it will be processed not as the "one" state change it is in reality, but a series of phantom state changes.

Before the input signal state change can be verified, the signal needs to be De-bounced. De-bouncing is the process of detecting the switch state change and removing transient noise to create a single noise free signal state transition.

Switch bounce is a transient condition of multiple make-break states as the switch settles to the closed state

De-bouncing is the process of detecting the switch state change and removing transient noise to create a single noise free signal state transition.

3.0 Switch de-bouncing strategies

There are two strategies to switch de-bouncing, Hardware De-bounce, or Software De-bounce. If the switch signal is input to a microprocessor, software de-bounce is the obvious choice since it is free - at the expense of a little software. Otherwise hardware de-bounce is required.

3.1 Hardware de-bouncing strategies

3.1.1 Input line capacitor

A simple improvement to reduce switch bounce is to place a small filter capacitor in the circuit as shown in Fig 3.1.1a.

Referring to Fir 3.1.1a, at the time of closure, the discharge path of Cf is through S1 and it's small internal resistance. When the switch opens, the charge path is through Rp and whatever current Pin1 may contribute. Since for all practical purposes, the internal resistance of S1 is zero and the Pin1 current is negligible, it is primarily the charge path through Rp which determines the charging time.

The frequency response of the circuit is:

F = 1/(2piRpCf)

To filter the transients, it is tempting to make Cf as large as possible. A Large Cf will reduce the response time of the circuit, but if very large, the state change from closed to open will assume a charge ramp rather than an impulse. This has two potentially negative ramifications.

First, excess current will be consumed charging the capacitor. Even if Cf is a small value if the switching rate is fairly high, repetitive charging/discharging cycles will negate the otherwise low-current/low-power benefit of the mechanical switch

Second, and potentially more problematic is fact that the signal as seen by the digital input Pin1, will transition through undefined Logic Levels (voltage states) as it charges from the electrically specified "low" state to the electrically specified "high" state. Depending upon the electrical characteristics of Pin1 this may an indeterminate switching time in comparison a "high" to "low" transition when Cf discharges. If the circuit needs to switch on a "low" to "high" state changes, this is a potential problem.

Worse yet - especially if the device is CMOS, the ramping signal may bias Pin1 into the linear operating region causing excessive current flow and potentially damaging the device.

This is not to say that capacitors should never be placed on switch signal inputs. In some noisy environments, it is required. If that is the case, or in any condition where a linear input signal is used to switch a digital input, use a Schmitt trigger circuit, or a device with Schmitt trigger inputs.

Figure 3.1.1b show a single-ended non-inverting Schmitt trigger circuit. A Schmitt trigger is a binary trigger circuit made up of a comparator with hysteresis. What that means, it is comparator, with part of the output signal is feed back to the input create two distinct switching levels, a high level to switch the circuit during low-to-high transitions and a lower level to switch the circuit during high-to-low transitions. The switching levels are determined by the values of Rh1 and Rh2.

Schmitt trigger circuit design is straight forward but a bit off-subject for this discussion. For more information check out the analog and op-amp references in the Designer's Bookshelf section.)


To avoid excess charge/discharge currents, use as small a capacitor Cf as is practical.

3.1.2 SR Latch

If space and budget allows, there is another hardware debounce technique using a SPDT (ON-OFF-ON) switch and an SR Latch (Set - Reset) latch made of two NAND gates. One advantage of this system is the debounce circuit generates complementary outputs

Referring to figure 3.1.2, when switch S1 is connected to Pole A, pin U1-1 is LOW, switching output pin U1-3 to a HIGH state. This forces Pin U2-1 connected to pin U1-3 to a HIGH, state. Pin U2-2 which is connected with a pull-up resistor to VCC is also HIGH, which switches pin U2-3 to a LOW state. Pin U1-2 is connected to pin U2-3, so it is forced LOW.

As S1 opens, pin U1-1 is pulled HIGH, but "momentarily" neither pole A or B is connected to ground. Because the state U1-2 continues to be held LOW by the output pin of U2, the output of U1 remains high.

When S1 makes contact with pole B, pin U2-2 is forced low, switching the output pin U2-3 to a HIGH state. In turn, pin U1-2 is now forced HIGH, U1-1 remains pulled HIGH, so the output pin U1-3 switches low, forcing pin U2-1 to a LOW state. The switching occurs at the propagation delay of the NAND gates.

Now, if S1 bounces as it closes to a stable state connected to pole B, causing pin U2-2 to momentarily bounce HIGH, it doesn't matter. As long S1 does not close on pole A, the state of U1 remains a stable low, holding pin U2-1 LOW, keeping the output state of U2 stable in the HIGH state.

If S1 does not have a middle non-conducting position,there is the disadvantage of one pull-up resistor always connected to ground. Choose as large a pull-up resistor as practical to minimize current dissipation.

SR latches can also be configured with NOR gates

3.2 Software de-bouncing strategies

If the switch is connected to a microprocessor, in general, and in particular for low cost applications, the switch is de-bounced in software.

A software de-bounce routine is very simple: upon detection of an input level state change the microprocessor samples the input level, delays a fixed period (usually the key-bounce time), and samples the input again. If the second sample input level is the same, the state change is accepted as true. If the level is not the same, the microprocessor can delay another delay period (or more), then sample again. At some point, if the levels do not match, the state change is rejected as noise.

A key de-bounce routine debounce_key() is shown below. When the routine is called, first step is to sample the pin level by a call to an I/O routine, sample(input_pin), which reads the state of input_pin.

A call is made to wait(delay). which delays 5ms. Input_pin is again sampled and if the state is the same, the wait loop breaks, and the second sample read is returned.

If the second sample does not match the first, up to two more samples are taken (15ms total delay time). The value of the second sample is returned, whatever value it is.

int debounce_key(void) {

  int input_pin;
  int delay = 5ms;
  int sample_count = 3;
  int first_sample, second sample;

  first_sample = sample(input_pin);
  while (sample_count--){
    wait (delay);
    second_sample = sample(input_pin);
    if (first_sample == second_sample)
        break;
    }
  return (second_sample);
}

The are a number of variations that can be made. For example, if the routine is arbitrarily called (nothing specific triggers the call, other than a need to see if there is a valid input), the last stable state is returned as shown below. This routine is almost the same as above, with the exception that first_sample is updated each loop to the last value read.

int return_key(void) {

  int input_pin;
  int delay = 5ms;
  int sample_count = 3;
  int first_sample, last_sample;

  first_sample = sample(input_pin);
  while (sample_count--){
    wait (delay);
    last_sample = sample(input_pin);
    if (first_sample == last_sample)
        break;
    else
        first_sample = last_sample;
    }
  return (last_sample);
}

The actual value of delay needs to be empirically determined. Usually a vendor's spec sheet will give some indication of expected key-bounce times. However, key mounting methods, application etc.. can have unexpected effects to extend the specified times.

Conclusion

Mechanical switches as input devices are simple, robust devices, but for reliable signaling operation they require careful attention to switch bounce conditions. In addition, the impact on system power drain must be considered when implementing and choosing pull-up resistor and noise filter capacitor values.