So long as you’re dealing with switches and potentiometers as input sources, there is little reason to consider overvoltage conditions on microcontroller pins. If you power the switch or potentiometer from the same supply as the microcontroller, you can’t go overvoltage. Once you’re taking input from less well-controlled sources, however, you’ll want to consider how to protect the microcontroller so it isn’t destroyed. This became a concern for us when designing our Metal Detector kit since the pulse we induce in the coil produces oscillations with peaks above 10V and below 0V. This oscillation is fed to analog inputs on an ATtiny microcontroller.
To start, it’s good to understand the overvoltage protection that already exists in the microcontroller. In our ATtiny, and all other AVR microntrollers that we know of, there are overvoltage protection diodes right at each input pin. Any voltage on the input pin higher than VCC plus the forward voltage drop of the diode will cause it to conduct, dumping current onto VCC until the voltage on the pin drops. Similarly, if the voltage on the pin dips down more than the lower diode’s forward voltage drop below ground, then current will flow up from ground to the pin.
The effectiveness of these diodes depends on the impedance of the overvoltage source and the current carrying capability of the diodes. The datasheet tells us to expect a forward voltage drop of 0.5 V for the diodes, but it doesn’t specify a maximum current. Atmel gives a hint, though, in a rather remarkable application note on using an AVR digital pin as a zero-crossing detector. In it, they propose hooking an AVR digital pin to AC mains through a 1 MOhm resistor. (I wouldn’t do it.) In the text, they say that it is best to limit current through the protection diodes to 1 mA.
So it would seem that throwing a 1 MOhm resistor in front of our pins would give us about +/- 1000V overvoltage protection. As appealing as a one-component solution is, there are some limitations. The most significant limitation is that this won’t work for ADC input pins. Returning to the ATtiny datasheet, we find the recommendation that the input impedance to any ADC pin not exceed 10 kOhms. This is because the ADC uses a sample and hold capacitor to keep the sampled voltage steady for ADC conversion. This capacitor can’t charge quickly enough with 1 MOhm of resistance between it and the voltage we want to measure. (Leakage current and pin capacitance are also considerations.)
Without checking their math, let’s assume 10 kOhms is a reasonable limitation. If we put a 10 kOhm resistor at the input and assume 1 mA current limit for the diode then we could accept input voltages on an ADC input pin from VCC + 10.5 V to GND – 10.5 V. This isn’t a very wide range, but it is enough for our metal detector.
So what if we really want that +/- 1000V protection? There is all kind of advice in forum posts about how to handle this, usually involving resistors and diodes. Much of this advice doesn’t account for the amount of impedance in front of the ADC or the voltages at which the internal protection diodes begin to clamp. Consider instead putting an op amp in front of the ADC. If we do this, we provide a low-impedance source to the microcontroller’s ADC while accepting a high-impedance signal on the op amp’s input. A low-cost op amp like the MCP602 doesn’t mind having a 1 MOhm resistor in front of its input. (They even discuss overvoltage protection in the datasheet.) With an op amp we will also get the opportunity to amplify, attenuate or bias the input signal into our ADC’s range, which is commonly needed. Many ADC application notes assume there will be an op amp in front of the ADC.
In a technical article about protecting ADC inputs, Analog Devices shows an approach I’ve used. (figure above) Single-supply op amps like the MCP602 often can’t reach VCC output when used as unity-gain buffers. However, for precision ADC measurements you would typically want to use a voltage reference anyway. Using a 4.096 V reference while powering the op amp from VCC gives you almost a volt of headroom at the upper end of the op amp’s output range, and makes the binary to decimal conversion of the ADC value easy. (Alternately, you could use the 2.56 V reference internal to AVR microcontrollers.)