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
- Click the Table of Contents link above --> then look under the "Arduino" heading
- Quick Tip: A Comprehensive List of Helpful Arduino Learning Links - New!
- Arduino Power, Current, and Voltage Limitations
- The Power of Arduino
- Using the Arduino Uno’s built-in 10-bit to 21-bit ADC (Analog to Digital Converter)
- eRCaGuy_ButtonReader Library for Arduino - Debounce and read the current button state, & most recent action ("just pressed" or "just released") easily!
- Arduino micros() function with 0.5us precision - using my Timer2_Counter Library
- Quick Tip: 4 Ways to Power an Arduino
- Recommended Soldering Kit & Tutorials (for Arduino, Electronics, & Radio Control)
- A Few Tips & Tricks: Arduinos, PCB Tricopter Frames, Home-made Acid Etchant for Copper
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.
-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.
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).|
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.
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.
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.
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).
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]
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):
- You are using a 10k resistor.
- Vmin_allowed = -(0.001 x 10000 + 0.5) = -10.5V DC.
- Vmax_allowed = +(0.001 x 10000 + 5.5 = +15.5V DC
- VmaxAC_allowed = 10.5V/sqrt(2) = 10.5/1.4142 = 7.42V AC
- You are using a 1M resistor:
- Vmin_allowed = -(0.001 x 1e6 + 0.5) = -1000.5V DC.
- Vmax_allowed = +(0.001 x 1e6 + 5.5 = +1005.5V DC
- VmaxAC_allowed = 1000.5/sqrt(2) = 707.5V AC
- 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.
- *****+ScienceProg - Using current limiting resistors on AVR I/O pins - very useful!
- *****Atmel Application Note - AVR182: Zero Cross Detector - very useful!
- Google search for "atmega328 clamping diode"
- Clipper circuits:
- Clamping circuits:
- Digi-Key: Protecting Inputs in Digital Electronics
- AllAboutCircuits.com - Chapter 3 - Electrical Safety
- 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  and  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 , 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.