Thursday, June 11, 2015

100+V Arduino/AVR Microcontroller Input/Output (I/O) Pin Over-Voltage Protection Using a *Single* Resistor!


Subscribe by Email!
By Gabriel Staples
Written: 11 June 2015
Last Updated: 2 Sept. 2015
Update History (newest on top):
-Added "Upfront Caveat", 2 Sept. 2015
-Added lots of extra info, & the Figures, 21 July 2015

Related Articles:

Here I present a pin protection method for very high and low input voltages that I learned from Atmel themselves, in their Application Note "AVR182: Zero Cross Detector."

If you've read my article titled "Arduino Power, Current, and Voltage Limitations" you know that the pin input voltage limits are as follows:
Maximum Input Voltage: Vcc + 0.5V, or +5.5V for a 5V Arduino
Minimum Input Voltage: -0.5V

However, if you simply add a single resistor in series with any input pin, you can protect it against higher or lower input voltages, with some caveats!

Ex: simply by placing a 10k resistor in series on an input pin, you get input voltage protection up to +15.5VDC and down to -10.5VDC. With a 100k resistor you get input voltage protection up to +105.5VDC, and down to -100.5VDC. Keep reading to learn more.

WARNING: 
-FOR YOUR PERSONAL SAFETY, WHEN USING THIS TECHNIQUE, I RECOMMEND THAT YOU DO NOT WORK WITH INPUT VOLTAGES GREATER THAN APPROXIMATELY 100VDC OR 20VAC UNLESS YOU KNOW WHAT YOU ARE DOING AND HAVE READ ALL OF MY WARNINGS HEREIN, INCLUDING THE ADDENDUM AT THE END OF THIS ARTICLE. 
-REGARDLESS OF WHAT INPUT VOLTAGES YOU ARE USING, YOU AND ONLY YOU ARE RESPONSIBLE FOR YOUR PERSONAL EXPERIMENTS, PRODUCTS, DESIGN DECISIONS, AND THE SAFETY OF YOU AND YOUR CUSTOMERS.

Upfront Caveat:
Though this technique may work, and be good for home-projects or quick prototyping, better techniques are recommended, especially if you are producing a commercial product. Redundancy is your friend. Using external, rated clipping diodes is certainly a better option than relying on the internal, integrated and unrated ones. For better protection techniques, including using *external* clipping diodes for redundancy, and adding transient voltage suppression, read more here: Science Prog: Using current limiting resistors on AVR I/O pins. Also see the AVR182 Application Note for references to additional sources to read in order to add extra pin protection.

Background & Concept:
Figure 1 below is a schematic showing how the external resistor you add to an input pin on an Atmel AVR microcontroller (ex: Arduino Duemilanove, Uno, Nano, Pro Mini, Mega, Leonardo, Pro Micro, etc) combines with the internal clipping diodes to safely clip the input voltage to within safe operating limits.

Figure 1: Schematic of clipping circuit on an Atmel AVR microcontroller (mcu) input pin, constructed via a single, user-added external resistor and the internal clipping diodes in the mcu. (Image source: Science Prog: Using current limiting resistors on AVR I/O pins

What is clipping? 
Clipping simply means to "cut off," or "clip" off the excess high and low voltages, without otherwise changing the shape of the waveform. Figure 2 below shows how the circuit above clips an input voltage to be within safe operating limits for an AVR microcontroller. For more on clipping circuits, see the sources at the bottom of this article.

Figure 2: Sinusoidal AC input voltage (left) vs its clipped output voltage (right), when clipped by an external resistor plus the internal clipping diodes in an AVR microcontroller (Source: I have created this image by modifying images from here).
For low (< approx. 100VDC or 20VAC) input voltages, a single, properly-sized resistor (providing adequate margin of safety for voltage spikes), should suffice. However, for better protection techniques, including using *external* clipping diodes for redundancy, and adding transient voltage suppression, read more here: Science Prog: Using current limiting resistors on AVR I/O pins. Also see the AVR182 Application Note for references to additional sources to read in order to add extra pin protection.

Mains (wall outlet) zero-cross detector:
WARNING: EXPERT USE ONLY. MAINS VOLTAGE CAN CAUSE DEATH, THE CIRCUIT BELOW IS MINIMALISTIC AND UNISOLATED, AND ADDITIONAL PROTECTION BEYOND WHAT IS SHOWN IN FIGURE 3 IS RECOMMENDED.

For mains (wall outlet) AC inputs which you want to clip to be within the safe operating limits of -0.5V to Vcc + 0.5V, Atmel recommends using two resistors. Refer to the figure below. Here, Atmel shows using one 1Mohm resistor on the mains "hot" line, and one 1Mohm resistor on the mains GND line. WARNING: in actuality, a single 1/4W 1M resistor is insufficient, as they are not rated for high voltage drops. Instead, it is recommended to use multiple resistors in series to distribute a portion of the large voltage drop across each resistor. Ex: using 5 x 200k resistors in series, to make the equivalent of a single 1M resistor, is far safer than a single 1M resistor. Again, standard 1/4W resistors are not rated for high voltage drops like this (see this AVRFreaks forum thread here for more info).

Figure 3: Zero cross detector using AVR (Image source: AVR182: Zero Cross Detector, Figure 1, pg. 2)
WARNING: WORKING WITH MAINS (WALL) VOLTAGE IS NOT FOR BEGINNERS. THIS SCHEMATIC DOES NOT ISOLATE MAINS VOLTAGE FROM YOUR MICROCONTROLLER. IF THE RESISTORS EVER FAILED, YOU COULD HAVE A LIVE, DEADLY VOLTAGE ON YOUR BOARD. USE GREAT CAUTION.
Using the 1M resistors as shown above gets you input voltage protection to +/-1000VDC, which is enough to connect 220V AC mains directly to an Arduino input pin to do zero-cross detection for high-power/low noise relay-switching on AC lines! Writing a zero-cross detection algorithm lets you switch the relay when the AC power is at ~0VDC, thereby reducing the wear-and-tear on your mechanical relay, and protecting the circuit from potentially harmful inductance-induced high voltage spikes which occur during high-current non-zero-cross power switching.
WARNING: MAINS (WALL OUTLET) VOLTAGES CAN BE DEADLY. DO *NOT* WORK WITH HIGH VOLTAGES LIKE THIS UNLESS YOU ARE COMFORTABLE WITH WHAT YOU ARE DOING. FOR HIGH VOLTAGES, ADDITIONAL VOLTAGE PROTECTION TECHNIQUES ARE RECOMMENDED FOR THE ARDUINO MICROCONTROLLER PINS AS WELL, BEYOND THE SIMPLE ONE OR TWO RESISTOR TECHNIQUE DESCRIBED HERE. See the ScienceProg and AVR182 references at the end of this article for more info.


Advertisement

Concept Summary:

Refer back to Figures 1, 2, and 3 above. Internal diodes on each AVR (Atmel microcontroller chip) clip** the input voltage to -0.5V on the low end and to Vcc + 0.5V (+5.5V for a 5V Arduino) on the high end. The resistor you add in series protects the internal clipping** diodes from overcurent, which is anything >1mA for an AVR microcontroller (see the ScienceProg & Atmel references below; the 1mA clipping diode limit is stated on pg. 4 of AVR182). Use larger resistors for greater voltage protection.  Beware that the larger you go, however, the more of a low-pass RC filter the resistor in series becomes, in conjunction with the capacitance of the pin. So, if you're trying to measure a 50kHz input frequency using the included demo in my Timer2_Counter library, you shouldn't be using a huge resistor, such as 1Mohm, as it will attenuate the high frequency.

**Note: the AVR182 application note says "clamp," and calls these internal diodes "clamping diodes," but I think the proper term in this case is actually "clip" and "clipping diodes," so I may be using these terms somewhat interchangeably in this article. See references below to read more about clipping and clamping.

This is seriously cool. Again, the below references are really good. AVR182 application note discusses how to find the zero-voltage crossing by connecting mains voltage (110V US, 220V Europe) directly to an Arduino input pin through a 1M resistor. Though this technique is not recommended as the only protection when dealing with very high voltages, it is fascinating how easy it is to provide simple, lower voltage overvoltage protection on input pins. The ScienceProg reference is where I first discovered this technique, and it also provides additional protection techniques I recommend you take a look at.

Extra Info:

The internal clipping/"clamping diodes," per AVR182, have max current capability of 1 mA (pg 4 of the document). Also note that AC voltage is an RMS ("Root Mean Square") value, so to calculate the sinusoidal DC voltage peaks of an AC voltage, you must multiply the VACrms value by sqrt(2). Ex: 120 x sqrt(2) = 120 x 1.4142 = 169.7. Therefore, 110VAC has sinusoidal peaks at +/-156VDC, 120VAC has peaks at +/-170VDC, and 220VAC has peaks at +/-311VDC. Again, the equation for sinusoidal DC peaks from AC sine wave is simply V_RMS_AC x sqrt(2).

Equations:

The equation to find out what voltage protection you can get for a given resistor is as follows (assuming, per AVR182 pg2, that the forward voltage drop, Vf, across the internal clipping diode, is 0.5V, and the maximum allowed clipping diode current is 1mA):

Vmin_allowed = -(0.001*R + 0.5)  [VDC]
Vmax_allowed = +(0.001*R + Vcc+0.5) [VDC]
VmaxAC_allowed = min(abs(Vmin_allowed),abs(Vmax_allowed))/sqrt(2) [VAC, for sinusoidal AC voltage]

If you are using a 5V Arduino, Vcc = 5V, so you get the following for Vmax: 
Vmax_allowed = +(0.001*R + 5.5)  [VDC]


Example Calculations (for a 5V Arduino): 
  1. You are using a 10k resistor. 
    1. Vmin_allowed = -(0.001 x 10000 + 0.5) = -10.5V DC
    2. Vmax_allowed = +(0.001 x 10000 + 5.5 = +15.5V DC
    3. VmaxAC_allowed = 10.5V/sqrt(2) = 10.5/1.4142 = 7.42V AC
  2. You are using a 1M resistor:
    1. Vmin_allowed = -(0.001 x 1e6 + 0.5) = -1000.5V DC
    2. Vmax_allowed = +(0.001 x 1e6 + 5.5 = +1005.5V DC
    3. VmaxAC_allowed = 1000.5/sqrt(2) = 707.5V AC
    4. FINAL SAFETY WARNING: UNLESS YOU ARE AN EXPERT, HAVE HEEDED ALL OTHER PREVIOUS WARNINGS, AND KNOW WHAT YOU ARE DOING, EVEN IF THIS CALCULATION SAYS YOUR RESISTOR VALUE IS ADEQUATE FOR A GIVEN VOLTAGE, DON'T PUT A VOLTAGE > ~100VDC OR 20VAC ON ANY ARDUINO/MICROCONTROLLER PIN.  For further information on electrocution, bodily resistances, hazardous currents, etc, see the "Additional Reading" links below.

Happy super-easy-input-pin-overvoltage-protecting!

References:
  1. *****+ScienceProg - Using current limiting resistors on AVR I/O pins - very useful!
  2. *****Atmel Application Note - AVR182: Zero Cross Detector - very useful!
  3. Google search for "atmega328 clamping diode"
  4. Clipper circuits:
    1. http://en.wikipedia.org/wiki/Clipper_(electronics)
    2. http://www.electronics-tutorials.ws/diode/diode-clipping-circuits.html
  5. Clamping circuits:
    1. http://en.wikipedia.org/wiki/Clamper_(electronics)
Additional Reading:
  1. Digi-Key: Protecting Inputs in Digital Electronics 
  2. AllAboutCircuits.com - Chapter 3 - Electrical Safety
  3. Wikipedia - Electric Shock

Addendum:
At what voltage is there a risk of electric shock?
Based on my research and calculations, electric shock becomes a hazard above ~100VDC or 20VAC. Current kills. Current, however, is a function of voltage and resistance. V=I*R, where V=voltage, I=current and R=resistance. According to the Additional Reading sources [2] and [3] above, bodily resistance is a factor of many things, with three of the primary factors being skin wetness, contact area, and contact pressure. Human resistance from hand to hand can vary from approximately 1kOhm~1MOhm. With wet skin, a large contact area (ex: multiple fingers, or a whole hand, contacting an electrical surface), and a firm contact pressure, hand-to-hand resistance can be as low as 1kOhm. A DC voltage of 100V causes a current of 100mA through a 1k load, such as the human body in these conditions. An AC voltage of 20V causes a current of 20mA. According to [2], a 100mA DC current, and a 60Hz 20mA AC current, are both approx. 1/5 of what is required to cause "possible heart fibrillation after 3 seconds." Therefore, for the purposes of human safety from the possible electrocution hazard, I am considering the 1-resistor technique as shown in Figure 1 above to be acceptably safe, for my personal experimentation purposes, for input voltages below approximately 100VDC or 20VAC. You can each draw your own conclusions and come to your own decisions. You must choose what level of risk is acceptable for you.

END


***Subscribe by Email!***

4 comments:

  1. 48 volts dc is used by all telephone companies because it is the maximum safe voltage for humans under worst case situations. 100 vdc can easily kill you. Depends on skin moisture, where the contact with voltage occurs, and more. At 60volts you can certainly get a shock on even dry fingers.

    ReplyDelete
    Replies
    1. Thanks for the comment. The 48V DC used by phone companies is good info to know, for sure. "100 vdc can easily kill you" -- do you have any sources or calculations to back up this statement? I'm not disagreeing yet, just asking for more info. Lastly, did you read my Addendum (At what voltage is there a risk of electric shock?) at the end of my article?

      Delete
  2. OK, on the analog side, as you increase resistance you reduce current available to fill the ADC. This can be solved by placing a capacitor across the ground leg of the voltage divider. So, I could measure analog volts from 0 to 100 with protection from 1000V spikes with a voltage divider of 1M and 20K and a capacitor of 0.1 µF as long as I can wait for a tenth of a second or so for the capacitor to charge.

    see:
    http://jeelabs.org/2013/05/16/measuring-the-battery-without-draining-it/#comments

    But how does a high resistance affect the ability to sense digital signals? If I decide I want to detect a 12V switch and I need protection from 1000V spikes and I place a 1M resistance in front of a digital input, how will sensitivity be affected? How big can the input resistor get before floating becomes a problem?

    ReplyDelete
    Replies
    1. Good question. At the moment I'm not sure. If you find out please post an answer here. Otherwise, I'll have to sit on this question until I have the need to solve it for one of my own projects. For the time being, I'll have to keep it on the back-burner, but I'm always anxious to learn from others in case someone else find the answer and wants to share.

      Delete

Thanks for your comment or question! If it is a question, I will try to get back to you quickly. Notice to spammers: I personally remove all spam promptly and report spammers to Google, so please don't do it.

Note: some HTML tags are allowed in your comments. To learn how to add bold (<b>...</b>), italics (<i>...</i>), or hyperlinks (<a href="URL">NAME</a>) to your comments, read here.

~Sincerely, Gabriel Staples.

P.S. Yo hablo español también. Je parle français aussi. (I speak Spanish & French too).