# Using current limiting resistors on AVR I/O pins

Today I want to talk about protecting digital Inputs of AVR or any other microcontroller from over-voltages. When you look at majority microcontroller circuits found on internet shared by hobbyists you don’t find any input protection. Some argue that in most cases this is not needed, or simply don’t understand how it works. Lets see how simple resistor can save a day.

Lets see at simplified version of digital input of AVR microcontroller.

We can see there that input uses CMOS logic where transistor is switched by voltage. According to AVR datasheet, gate control voltage should stay within -0.5V to VCC+0.5V range. If we power our device with 5V supply, we need to make sure that pin input voltage stays in range -0.5 to 5.5V. When input voltage source is taken from same power supply, then we don’t have to worry much about it. But what if AVR is accepting digital signals from other sources like sensors, other devices that are powered with their own power supplies. Can we be sure that voltage will always be within safe limits. This is why there are two clamping diodes (sometimes called ESD protection diodes) used. They are here to protect logic from over-voltages and under-voltages. Actually they do their work pretty well… until they die. Lets take situation when voltage at pin is 7V what happens here. D1 Anode voltage is 7V while cathode is VCC=5V. Then we get 7-5 = 2V on diode. But diode forward voltage drop is about 0.7V. Diode becomes unprotected and high current flows through diode until it fails. And so logic becomes unprotected. Same is with under-voltage. If we apply -2V at input pin then diode D2 starts conducting forward from GND to PIN with 2V across. Again voltage drop at diode is 0.7V and so current grows until diode fails. And so if we expect that input voltage may be off limits we need to add current limiting resistor.

Now when we added resistor, we have a place where voltage can be dropped. Like we had 7V on input pin and diode forward voltage drop is 0.7V resistor takes the rest of 1.3V. What about current? It is not recommended to exceed 1mA on clamping diodes. Having this data we can model worst case scenario. Lets say we expect that input voltage spikes will never exceed 10V. Then we can calculate the current limiting resistor value:

R = (10V – 5V-0.7V)/1mA = 4.3kΩ

We can simulate this on Ltspice:

DC simulation results:

As we can see current on diode D1 is about 1mA and voltage to CMOS Vout=5.65V.

Going even further there can be also a capacitor included which with current limiting resistor makes RC filter. Resistor part serves as current limiting resistor while capacitor adds filtering of glitches and debounces input signals.

You may need to calculate or model RC circuit individually.

What if you are designing critical device and do not want to rely on clamping diodes inside AVR or other micro. In this case you may add external clamping diodes. Even better – use Schottky diodes.

Schottky diode forward voltage drop is lower that standard diode. It varies from 0.28V to 4.3V. So if over-voltage or under-voltage occurs they will protect the circuit and internal diodes won’t conduct at all. So you get double defense in case any of diodes fail.

Can there be more protection added? Of course. If you are likely to work with high voltages – like building microcontroller based HV zapper or there is a chance to catch high voltage on input, then first line of defense should be transcient voltage protection circuit. These can be based on varistor, gas discharge tube. But probably most reliable in such situation would be using transcient voltage suppression diode (TVS). They can handle high peak currents and clamps really fast. This is how heavily protected pin would look like:

You can use ferrite bead L1 before TVS diode which slows down rise time of voltage giving enough time TVS diode to turn on. Probably you are not likely to build such protection as this is really overkill and quite expensive per pin. But in some industrial applications you can find such solutions.

Generally speaking if you are designing hobby circuit that will be battery operated, then probably input voltage will never exceed limits and so you can omit resistor. And this is how most hobby circuits are built. Nothing wrong with this. But if you are designing device for market and there is any risk of over-voltage then you must include current limiting resistor. Resistors are cheap, so adding them won’t hurt anyway.

### One Comment:

1. Happymacer

Thanks, your article is clear and most helpful. Any further advice on calculating the capacitor on the input RC filter?