Larry's Circuit Blog

Tuesday, March 23, 2010

LCB #4, PART 2. SWITCHES, and high-level interfaces.

More on switches, and how to use high-level interfaces for reliable performance.                                                                                                                                                                             

In LCB#4, Part 1, I discussed "dry circuit" switches and how they can be easily and directly interfaced to  low-voltage logic.  "Dry circuit" switches are acceptable in this application because they do not require high contact voltage and current for reliable operation.

Unfortunately, there are many cases when a heavy-duty switch must be used.  Examples include oil- tight industrial switches, key switches, automotive switches, high-current relays, etc.  In this case it is necessary to switch high voltage and high current to keep the contacts clean.  In many systems there is a 24V DC supply available for driving loads and operating other devices, and this auxiliary supply can be conveniently used to provide the switching current.  Of course, the 24 Volts must be translated down to the logic voltage, which we will assume is 5V.  Please see Figure 1.



An almost OK circuit:
This circuit is acceptable for casual, non-critical applications.  The use of a relatively low-value pullup resister R1, connected to +24V, insures that the contacts will stay clean.  When the switch is open, the voltage at point A will be close to 24V, and the voltage at point B will be clamped to about 5.6V by the internal diode at the gate.  The value of R2 is not critical; high values may cause problems with leakage, and low values will not limit the internal diode current to a safe value.  20K-50K is usually OK.

The internal input diodes are usually rated at 10MA, and with the values shown the current will only be about 0.8MA.  This is OK when reliability requirements are not extreme.  Noise immunity is particularly good when the switch is open because of the low impedance at B provided by the forward-biased input diode.  When the switch is closed, the capacitor provides a low impedance at B.

If you are concerned about the internal-diode current, you may add a resistor in parallel with C.  Select the resistor to reduce the voltage at B, with the switch open, to equal the logic voltage.  The divider action will  improve, somewhat, the noise immunity when the switch is closed.

Although you can usually get by with this simple circuit, let's see what happens when when point A is hit with a static discharge.  This can easily happen because many switches are simply not provided with much insulation.  Since your circuit represents a ground, an arc can penetrate the switch and strike the contacts.  Assuming the "Human Body Model" (100 pfd charged to 5KV to 10KV or more), a discharge will raise point A to a high positive or negative voltage.   This will produce a high current (1/4A or more) through R2.  Most of the energy will be absorbed by C, but the input protection diode may still be briefly stressed with high current.

The above discussion is based on the fact that R2 will limit the current.  Unfortunately, this may not always be the case;  a high-voltage static discharge can merrily arc across R2 and produce a dangerously high current in the gate protection diode.  This can readily happen if a small SMT part is used for R2.

One solution is to use several resisters in series for R2, as shown in Figure 2A.  If you are using 1/4W resistors, use 3-4 in series; with SMT resistors, it is a good idea to use 5-6 in series.


A pretty good circuit:
                                                                                    
If this is a little clumsy, or if you are paranoid about static, you can use a transient protection element from point A to ground; this will clamp the discharge.  This could be an MOV, a transient-rated Zener, or even a neon lamp.  Just to add to your worries, remember that if you clamp the discharge you will be producing a fast, high-current transient (the spark), which can radiate RF and produce nasty transients in nearby circuits.  For this reason, you are often better off without clamping the transient and just carefully limiting the current via R2 (no spark).

There are other circuits which use additional diodes to prevent the gate's protection diodes from conducting any current at all.  An example is shown in Figure 3.


A really good circuit:



In this circuit, a large transient voltage at pt. A will be safely clamped, at pt. C, to about 0.7V above the 5Vsupply, or to 0.7V below ground.  The current will be limited by R2.  Resistor R3 is very important:  since the voltage at pt. C is likely to exceed the turn-on voltage of the internal diodes (if only by tenths of a volt), it is necessary to include R3 to provide current limiting for the internal diodes.  This is a very rugged circuit, as long as you use several resistors in series for R2.

Concluding, the circuits shown here are good for driving logic with big, ugly switches or contacts.  They also provide some noise immunity and conditioning for applications where the switches are located far away (up to tens of feet, depending on the environment; shielded cable may help).  However, they are not a substitute for opto-isolated circuits or line receivers that can reject high levels of noise.  More on this sometime in the future.

Don't forget contact bounce!!!!!!!

Friday, February 26, 2010

LCB #4, PART 1. SWITCHES, and how to interface them with logic.

More on switches, and how to build switch-logic interfaces that work reliably.                                                                                                                                                                             

In my last blog, LCB#3, I discussed switches and how contact material affects reliability.  A "dry" circuit is one where you are switching logic voltage levels at low current.  In these applications, a switch with gold-plated contacts is necessary, since there is not enough energy to burn through the oxide coating that will form on other contact materials.  At higher energies, however, gold plating will burn off and  you should use silver contacts.  See Figure A. 



Broadly speaking:

1. With low voltages and currents (for example, 5V and 20 MA) always use a switch with gold-plated contacts ("dry circuit" rated).  Most magnetic reed switches will also be OK, since their contacts are sealed in an inert atmosphere.
 
2.  With voltages greater than about 12 V and with currents greater then 40mA or so, there is enough energy to burn through the silver oxide coating. Therefore, silver contacts will provide reliable operation while gold plating would be destroyed.

3.  If your application is switching low voltages and very high currents (such as an automotive application), you are on your own because I don't know what to suggest.  This is, actually, a real problem: when was the last time you turned on a flashlight and it didn't flicker until you banged it on a table and cleaned the contacts?  The best solution in this case is often to use copper contacts with extremely high contact pressure and/or a lot of "wiping" action to keep the contacts clean.


Now, finally, we get to the circuits.  I'm going to discuss how to interface logic, or other electronic circuits, with different switch types.  Assume that CMOS gates are used, and I strongly suggest the use of a gate with a Schmitt-trigger input.

Case 1:  Switching a logic level with a "dry circuit" (gold) switch.  An example would be a miniature, on-board, tactile pushbutton of the type that is commonly used as a "reset" button.  In this case it is usually OK to use a relatively high-value pull-up resistor (1K to 22K or so) and wire the switch directly to the logic input.   See Figure 1.


 

Remember, however, that you may still have to contend with contact bounce (see LCB #1).

What is insidious about this circuit, however, is the fact that you no longer have a circuit with good noise immunity; this is because, when the switch is open, you have a relatively high impedance at the logic input-  equal to the pullup resistor.  As a result, fast transients (such as caused by a static discharge) can be easily coupled into the node. One solution is to simply add a capacitor across the switch contacts.  But...... ouch.......lousy idea; see Figure 2

When the switch in Figure 2 is closed, its poor little contacts will short the capacitor, producing a very high-current pulse.  These switches are often only rated for 10-20mA, and the gold plating can be quickly destroyed.  The solution is to add a resistor before the cap to limit the current.  See Figure 2A. 

 

 The added resistance also helps noise immunity by increasing the time constant and forming a low-pass filter.  This is of great value if the switch is located some distance from the gate, such as off the board on a front panel. A gate with a Schmitt-trigger input should definitely be used in this circuit, since the slow transitions should be squared up.  As a plus, you will then have a circuit which is immune to contact bounce; see LCB#1.

CAUTION:  THIS CIRCUIT WILL NOT PROTECT AGAINST STATIC DISCHARGE.  More on that in future blogs.


Case 2:  Switching a logic level with a general-purpose (silver or copper) switch. There are many instances where you have to generate a logic input from a switch that is not designed for "dry circuit" operation.  Examples would include rocker or slide switches, 120V wall switches, "micro switches", heavy-duty toggle switches, or even door-bell buttons.  In these cases, we have to configure the circuit so the switch is switching high voltage and current, yet level-shift down to the necessary logic level.

However, it is a beautiful day with fresh powder, so I am going skiing now.  I'm sorry, but you'll have to wait until next time for the solution to Case 2.  I hope you can stand the suspense. Until then.......


Sunday, January 17, 2010

LCB #3. SWITCHES, that's right, switches !

 A switch?  Pretty simple, right?  Make two wires touch, right?  Sorry, read on....
                                                                                                                                                                             


Many years ago I was called by a company that was experiencing problems with an industrial packaging machine.  It was a fairly simple product, with several motors, heaters, indicators, etc.  It was controlled by 5-volt CMOS logic configured as a simple state machine.  The inputs were sensors and a few front-panel "oil-tight" industrial switches.

The company's engineer showed me the product, gave me a schematic, and left for a few minutes.  When he returned, I said "I know what the problem is."   He replied, "What? I haven't even told you what the symptoms are".  Arrogantly, I said, "You don't have to; I know what they are."

He probably mumbled something under his breath, but he told me to continue.  I replied, "I bet that sometimes the front-panel switches work, and sometimes they don't."

He said, "That's exactly right; how did you know?"  I should have said that I was ordained with special powers by the spirit of Nikola Tesla.  Instead, I opened the schematic and pointed to the circuit that interfaced to the front-panel switches.  I have drawn this circuit below, in Figure 1.



                                                     Figure 1
 

It was immediately clear that the problem was that the big, gross, front-panel switches were not making and breaking enough current for reliable operation. Yes, "Use it or Lose it" applies to switches, -or at least "Use it with Lots of VA (volts x amps) or Lose it.

The problem is that switches have contacts.  Being metal, they are subject to formation of oxide films.  Naturally, switch designers try to select a contact material or plating which is optimum for their application.  For example, the oil-tight switches in my story were rated at something dainty like "10A, 600V."  To switch at levels like this, they use copper contacts plated with silver.  This is a great choice for big, hairy, high-power contacts.  Even though the silver will oxidize to produce a thin insulating film of silver oxide, the film is easily blasted away when the switch opens or closes in a high voltage, high current application.  Unfortunately, the designer of this circuit used low voltage and an extremely large pull-up resistor; therefore there was not enough energy to break down and burn off the oxide layer. Incompetence reigns again. 

During my lectures, I try to demonstrate this problem as part of my famous repertoire of exciting pedagogical moments.  However, in the past I usually embarrassed myself in front of the class because a high-power switch will sometimes work on a low -energy circuit.  Then, at a garage sale I found a box of very old microswitches.  Eureka! These old switches are rated at 10A, 220V, and when I wire them up in a circuit like Figure 1, the contacts do not electrically close. since the switches have not been actuated for a long time.  If I raise Vcc and decrease the pull-up resister, the switch will eventually operate reliably.  Note that if a switch is repeatedly actuated, the "wiping" action may eventually clean the contacts, even at low voltages and currents.

So, what do you do with circuits and switches?  Well, if you have a "dry" circuit (less than a few volts and a few milliamps), you MUST use a switch with gold contacts; gold will not form oxide layers.  These switches can usually be identified because they have very low VA ratings, such as "0.4 A, 30V.  Keyboard or tiny board-mounted push-buttons are usually rated for dry-circuit operation.

If you must interface with a "standard" push-button, toggle, or microswitch with silver contacts, you will have to present high voltage and current to the switch.  However, since your circuit is probably 5 or even 3 volts, you will require a special interface circuit.

The exciting conclusion will follow in an upcoming blog.



Tuesday, December 22, 2009

LCB #2. ONE-GATE WONDERS, Part 1

Some of the handiest circuits around are those that can be made with a single gate.    

                                                                                                                                                         Timers, oscillators, one-shots, pulse stretchers and more can built with just a few components.  The fundamental reason that we can do this is that CMOS outputs swing close to rail-to-rail, while the input switching threshold is, conveniently, about 1/2 the supply (more typically in the range of 1/3 to 2/3 Vdd).  With other logic families, such as olde TTL,  this was not the case and One-Gate Wonders did not work well.

For starters, look at a simple circuit made with an RC circuit and a CMOS inverter. Please see Figure 1.  I show an inverter here, but you could use any kind of gate; for example you could use one input from a NOR gate or an AND gate.  The concept remains the same.




This circuit takes a long pulse and generates a short pulse at the rising edge.  In other words, it differentiates (sort of, at least) the input pulse and produces a short pulse, or "spike", which is independent of the length of the input pulse.

When the input goes HIGH, the voltage at C also goes HIGH by the same amount ("The voltage across a capacitor cannot change instantly"). The output immediately goes LOW.  The charge on the capacitor then decays through the resistor according to the RC time constant.  Eventually the voltage at C will fall  below the switching threshold of the inverter, and the output will go HIGH again.

In this case, the time constant is 33 usec;  we don't know what the exact threshold voltage is, but it is somewhere around 1/2 the supply voltage.  Since this voltage is not that different from  what the voltage will be at point C after RC seconds have gone by, you can crudely (but quickly and conveniently) estimate that the output pulse width will be--guess what-- RC seconds!


The truth is, unfortunately, that you can never be sure what the CMOS threshold will be.  It is a  percentage of the power supply, but it will vary from part-to-part and with temperature.  Therefore, the actual pulse length may be much different than RC seconds  In fact, it could range from about (1.2)RC  to about (0.3)RC.  Most of the time, however, it will be in the neighborhood of (0.7)RC.

This circuit is very handy when you want to trigger something which requires a sharp trigger pulse, such as a 555 one-shot. Or, when the ON time of a pulse is too long and you need a short pulse to save power in a load or reset a circuit.

I know this is a lot of babble about a pretty simple circuit.  So, if you have something better to do, I understand.  On the other hand, if you would like to know some interesting, down and dirty details, stick around:

First of all, somewhere out there is a curious, conscientious person who is wondering exactly what happens when the input goes negative.  This type of person is usually a real pain, but I guess I can't duck the question -even though I would like to do so because the answer is messy.

After the input has been high for a while, the capacitor charges to a voltage equal to the supply.  Then, when the input goes LOW, point C will also (try) to go LOW due to the action of the capacitor .  Again, "The voltage across a capacitor cannot change instantly".  If point C was not connected to a gate leg, the voltage at this point would swing negative, below ground, to a value equal to the supply voltage.  However, the CMOS input is connected to the cathode of an internal "protection" diode whose anode is connected to ground.  This diode will clamp point C to about -1/2 V, preventing it from going any further below ground. See Figure 2.



When the input goes LOW after going HIGH, whatever is driving the input (typically another logic element) must sink a lot of current as the capacitor discharges through the protection diode.  In fact, there is actually a short circuit, for a brief time, while current flows out of the input.  This is where it gets a little messy, because engineers don't like short circuits.  In fact, we don't like any situation, such as this one, where things are undefined.  This is an undefined case because this discharge current is not limited by an actual resistor; instead, it is limited to whatever current can be sunk by the device that is connected to the input.

We must be careful that this current  is not great enough to destroy the input protection diode, which has a maximum rating of 10 MA.  Unfortunately, that's all the CMOS data sheet says; we don't know if this is the average or peak current.  If the input is driven by the output of another CMOS gate, for example, the current will be limited to the short-circuit current that the output of the gate can sink. A standard CMOS output can only sink several MA when the supply is 5 V.  However, at higher supply voltages and lower temperatures, the output current may exceed 50 MA (check the datasheet).  This current clearly exceeds the 10 MA rating of the protection diode.  So, what do we do?  Fortunately, we can usually select small capacitor values which will limit the duration of the overcurrent.  With a .01 uFd cap and a 15 V supply, since I = C*dV/dT and I = 50 MA, the protection diode will be conducting for only about 3 usec.  This is a very short period and it is extremely unlikely that the diode will be damaged - especially if the frequency of operation is low and, thus, the average diode current is small.  On the other hand, if you used a 0.1 uFd capacitor, operated at 15 V, and switched at a frequency of 10 KHZ, the peak current would be 50 MA and the average diode current would be 15 MA and the diode would be subject to failure.  (Caution:  an extremely high-current reverse-voltage pulse, even if very short, can cause CMOS to "latch up" and fry.)

Conclusion?  This is a great circuit, but the subtle failure mode discussed above is sometimes ignored by people who don't read this blog. A discussion:

1.  If you operate at 5-6 VDC supply, don't worry about anything.

2.  At higher supply voltages, if your frequency of operation is less than a few KHz and your capacitor is less than .01 uFd, you should be OK.

3.  At higher supply voltages, if the capacitor and frequency are large, you better be careful.  Keep the capacitor very small (<.001uFd) or put a 1K or 2K series resistor right at the gate input to limit the current.  Note that this resistor will, however, decrease noise immunity somewhat.

4.  And what about the resistor?  It must be large enough so the driving circuit is not loaded down.  So, if a CMOS gate's output is driving the differentiator, better keep it greater than 22K if the supply is 5V.  At greater supply voltages, you can use 10K or so.  As far as a maximum value, the resistor must be small enough so that gate input current, diode leakage, board leakage, etc. will not drag the input HIGH. So, if you are running a circuit near room temperature in a clean and dry location, you can use 10 Meg or even  more without concern.  At high temperatures, or if you might have schmootz or moisture around, you better use 1 Meg or less.

5.  And what about  the pulse length?  You can make RC as short as a few usec, and this should be long enough to trigger things, reset something, etc. However, people who spend too much time at the bench know to make a pulse as long as possible. That way you will be sure that it will do its job, and, almost as important, it is simply easier to find and see on an oscilloscope during development or troubleshooting.

6.  Don't forget that this circuit, and any circuit, will load whatever is driving it.  When the input to the differentiator goes HIGH, the driving device is loaded only with the resistor.  However, when it goes LOW it sees the capacitor connected, essentially, to ground.  This will slow the fall time of whatever is driving the gate.  Therefore, be careful if you must use this node for anything else.

7.  This and similar circuits will work best if the gate has a Schmitt-trigger input. You will have a snappier output, with better rise time and better noise immunity.

And finally, there is still more to talk about.  Is this circuit better than a one-shot?  If so, why?  Who knows?  Who cares?   You'll find out on One-Gate Wonders Part 2, on Larry's Circuit Blog.




.                                                                                                                                                     

Wednesday, November 25, 2009

LCB #1. HOW TO LIVE WITH CONTACT BOUNCE

Contact bounce is a subtle problem that can lead to unreliable circuit operation.                                  

                                                                                                                                                         Switches and relays all have one thing in common: electrical contacts that make and break a circuit. Contacts have mass, of course, and at least one of the contacts is usually mounted on a springy strip of metal so that it provides “wiping” action and allows overtravel.

Some simple things aren't so simple......Because the moving contacts have mass and springiness, they will bounce as they make and break. For example, when a SPST normally-open pair of contacts is closed, the contacts will come together and bounce off each other several times before finally coming to rest in a closed position. This is "contact bounce". See Figure 1. Note that contacts can bounce on opening as well as on closing, but bounce is usually much worse upon closing.  However, NEVER assume that contacts don't bounce when opening.



The duration of the bounce will vary depending of the design and physical variables of the device. A small, high-speed reed switch, for example, will settle in a few milliseconds; a large contactor may take 10 ms or more. With some switches, such as cheap doorbell buttons, the “human factor” may result in erratic opening and closing as long as a person is touching the button.

The Problem
If you want your switch or relay to simply turn on a lamp or a motor, then contact bounce is usually not a problem. But if you are using a switch or relay as input to a counter, toggle flip-flop, one-shot, or similar device, then you must consider contact bounce. This is necessary, of course, because a digital circuit can respond in microseconds. For example, suppose you want to count widgets as they go by on a conveyor belt. You could set up a mechanical arm that activates a switch at the input of a digital counter. What you might see is that the first widget produces a count of 12, the second widget produces a count of 21, and so on. This would occur because the contacts bounce each time the switch is activated.


The Wrong Solution
There are several ways to solve the problem of contact bounce (that is, to "debounce" the input signal). A common debounce circuit for a switch is shown in Figure 2A. It attempts to use an RC delay to swamp out the bounce from a N.O pushbutton. Resister R1 is the pullup resister. R2 and C1 are chosen so that the time constant is longer than the expected bounce time. An R x C value of about 0.1 seconds is typical. An inverting buffer follows the switch to produce a sharp transition.




 The above circuit is often used; however, it does NOT provide protection against contact bounce. Even if the RC time constant is made extremely long, it is always possible that a particular sequence of bounces will cause the voltage at B to fall and then rise, crossing the voltage threshold and producing a spurious output pulse. Even a slight wiggle of a fraction of a millivolt is enough to cause a rogue pulse.  See Figure 2B.







 


The Correct Solution
For this circuit to work reliably, you MUST use a Schmitt Trigger buffer or gate; one that includes hysteresis. An inverter with input hysteresis will output a LOW when its input exceeds a certain threshold.   However it will not go HIGH until the input drops to a second, lower threshold.  Gates with hysteresis have "snap" action and are invaluable for eliminating spurious pulses when an input is noisy or "squaring up" a signal that changes very slowly.  A CMOS gate such as the 4584 inverter has a hysteresis voltage (the difference between the two thresholds) of about 7-10% of the supply.

By adding hysteresis as in Figure 3A, you can guarantee reliable operation by choosing a time constant large enough so that the ramp time from the lower threshold to the upper threshold exceeds the longest time period that will ever exist between two bounces. In this way, the inevitable transitions that occur during the bounce interval cannot produce a spurious output pulse. See Figure 3B.



The voltage at point B produces a HIGH transition when it drops below the lower threshold. However, even if it rises again due to bounce (as shown), with the addition of hysteresis the output will not go LOW unless the voltage at B rises all the way past the upper threshold and then drops below the lower.  By incorporating a suitable time delay, this cannot occur.  


Another Good Solution
A second debouncing approach is shown in Figure 4.  It requires a SPDT (“form C”) switch, but it provides reliable switch interfacing without the debounce delay required with a SPST switch. It uses a cross-coupled latch made from a pair of NAND gates, an SR flip-flop, or other gate combinations.

If the switch is in the upper position, Q must be high since one of gate A’s inputs is LOW. Since both of gate B’s inputs are HIGH, not-Q must be LOW. The flip-flop will be stable in this state, even if the upper switch contact should be broken.

However, if the switch wiper touches the lower contact, not-Q is forced high. Now both of gate A’s inputs are HIGH, so Q goes LOW and, in turn, not-Q goes HIGH.. The flip-flop is, again, in a stable state and will remain in this state until the upper switch contact is made again.

Because of this bi-stable characteristic, switch bounce has no effect. If noise should produce a temporary LOW on the HIGH input, the flip-flop will not change state because the other input is held LOW with the switch contact; this prevents the flip-flop from changing state.  IMPORTANT: Note that such a noise pulse will be seen as a temporary HIGH on one output; this may cause an error. For this reason it may still be necessary to filter and otherwise condition the input lines.



CAUTION: This paper discusses contact debouncing, but it does not address two absolutely crucial aspects of interfacing to switches and similar devices. One concerns the need for adequate current and voltage so that the switch contacts remain “clean’ and free of oxides and insulating films. The second issue relates to the need to provide protection against noise and destructive transients that can be coupled to inputs. The circuits described here are only suitable for operation with very low current, “dry” switches such as reed, keyboard, or small “tactile” switches; also, these must be located very close to the circuitry to minimize noise problems. Toggle switches or pushbuttons must have gold contacts and be rated for very low power, such as “0.4 VA”. Typical logic voltages and pull-up resistors will not provide enough voltage and current for reliable operation of switches with higher ratings, such as “6 A, 120VAC”. These switches have contacts which are prone to oxidation, and low voltages and currents may not burn through the insulating oxidation layer if the switch has not been operated for a long time.

Stay tuned to Larry's Circuit Blog for a future discussion of interfacing to switches, contacts, and other real-world input devices.

Followers