Home‎ > ‎My Blog‎ > ‎

Microcontroller Input Protection Techniques

posted Feb 20, 2012, 6:21 AM by Kevin Fodor   [ updated Mar 8, 2012, 5:01 AM ]
A recent project I have been working on required a single input from a pushbutton. Simple enough I thought, as this is a very common thing in most electronic projects. Especially those with microcontrollers. My initial thought was to carelessly place one leg of the pushbutton to ground and the other to the microcontroller input (the MCU has internal pull-ups). Easy enough, a low signal means pressed, a high signal means not pressed. Done! Debouncing would be done in software (that's another subject entirely).

Well I thought about this a little more. The pushbutton may actually be located away from the PCB (out side any project case I might put it in). It might even not be a switch at all, but perhaps two contacts which come together. I began to think more about this. What if the switch fails? What if someone applies power to one of the switch terminals? What if the switch picks up ESD, over voltage, under voltage? The concerns for the input of the MCU mounted. Am I using the right input protection?

So this made me think a little more about the seemingly simple addition of a pushbutton directly into the microcontroller. What kind of input protection method should I be using? I figured this might be a good time to survey the well know input protection options there are.

Keep in mind for the discussion below, I am considering inputs which are relatively slow (pushbuttons, contacts, etc) and not high-speed inputs which would require other considerations beyond the scope of this post. Also the example MCU input equivalent circuit is just a representation of what an MCU pin might look like for the sake of discussion, your mileage may vary. In general we assume an MCU can drive an output (high or low, both not both!) and an input is buffered through some high impedance device. I didn't want to go into too much depth of the internal pin constructions since every MCU manufacturer is a bit different. Please consult your specific MCU data sheet for those details. However I think you will find them all very similar.

Input protection can be classified into two broad categories. Ones which protect against software or configuration bugs, or misuse; while another protects against exposure to the outside world. We will discuss protection from software configuration bugs and misuse first.

Current Limiting Series Resistor

The first thing anyone who has been around the block a few times will tell you, is that it is always a good idea to throw a series resistor (Rs) in the path of any MCU input. This limits the current from the outside world into (or out of) your MCU. Why does this matter?

Your MCU can only source or sink so much current through the input/output pins. Exactly how much can be found in your MCU's data sheet. For this discussion let's assume this is 10ma maximum. Typically part of the initial MCU configuration is the setup of each pin and its function. We are discussing general purpose inputs here, so we intend to configure the pin as an input. However take a look at the simplified equivalent circuit for an MCU input I have shown above.

Notice the output stage is formed using two FETs in series and the input is buffered back into the MCU. When configured as an output, the MCU will tie the output pin to either Vcc or GND depending on your pin output configuration. When configured as an input, the pin is buffered back to the MCU indicating it's state. This is what we expect from the MCU pin equivalent circuit.

However consider what would happen if the output state were to be accidentally configured as an output and set to high. This turns the upper FET on and when the pushbutton is pressed, forms a short circuit direct to ground and the pin blows.

Click here to see an example of what happens when you do not have a series resistor in place to limit current in this case. Press the pushbutton switch and see how you create a short circuit from supply to ground which will most likely cause your MCU pin to blow immediately.

Now many MCUs will use some combinational logic to lockout the output stage when the pin is configured as an input, so it isn't likely we could set the output to high if the pin is configured as input. This may not always be the case though, so to be sure you should consult the data sheet. The point here is you may not know precisely how your MCU will behave at the first few microseconds of initialization, so its good to be cautious.

Consider another perhaps more likely case that someone (or you) decides to (or mistakenly) reconfigures that input pin as an output. If the output state through either default or explicit configuration sets this pin high, disaster is certain. When the pushbutton is pressed, the high output on the input pin is tied directly to ground creating the short. This is not what you intended, but accidents can happen.

To avoid this situation altogether a series resistor (Rs) can be inserted between the external input (the pushbutton) and the MCU pin. This limits the current which is sourced or sunk by the MCU. You'll need to look at the maximum current capability of your pin to determine the right value for Rs. Consider the maximum current for a 5V MCU pin is 10mA. Ohm's law says to limit 5V to 10mA you need a 500-ohm resistor (pick nominal 470-ohm).

Click here to see how the current is limited when a series resistor of 500-ohms is added. Now if this inadvertent software bug or misconfiguration happens the maximum, current your MCU will see is about 10mA.

Some designs may use lower resistor values such as 300 or 220-ohms. Again, check your MCU's data sheet and it will guide your selection. Clearly if you can be absolutely certain the code is correct and will not be inadvertently used in away you did not intend, the series resistor could be overlooked. But if your doing any sort of open source, experimental or other otherwise modifiable design which could have others changing (or more likely misunderstanding) your original intent it's best to take steps to protect against it.

External Pull-Up Resistor

On some MCUs, an external pull-up resistor (Rpu) is required when a pin is used as an input or output. In that case you need to add one and the data sheet will help you decide the correct value. Others more commonly have them already built in. You have the option to either turn them on or off. The question is when should you? Can you really save yourself the extra resistor by using only the internal one? Is it wise to rely on it in all cases? Should you ever?

Like most things, it depends. Consider if someone(or you) decides to modify the software which configures the pin to something other than what was intended?

Consider the case where your original design intended the pin to be an input, with the internal pull-up enabled. Somewhere along the way the code is modified in such a way that the pull-up is no longer enabled. In the example circuit, your input will certainly read a low when the pushbutton is pressed, but what does it read when the pushbutton isn't pressed? The input is floating in this case and the state is usually indeterminate. A floating input (or output) will produce random and strange results which might not be easy to debug.

Now consider someone actually thought about that and without much further thought tried to avoid the problem by tyeing the input high. Initially this seems like a good idea, at least when the button isn't pressed the input level is stable. But watch out! When the pushbutton is pressed, a direct short to ground will be made. Smoke happens.

Click here to make virtual smoke.

If that isn't bad enough, consider someone realizes that pushing the button would be a bad thing and cause a short. But they rationalize to themselves that the pushbutton (for whatever reason) isn't going to be pressed. However, what if the pin which is configured as an input, is set (or by default is set) to a logical 0. Boom! Out go the lights. Software just created a direct path from Vcc to ground.

Click here to see how an inadvertent software bug could actually wreck your hardware.

The right solution in this case is to add an appropriately sized pull-up resistor (perhaps 10k-ohms). This prevents either case from being a problem. In all cases the input is either a stable high or low (not floating) and misconfiguration in the MCU won't cause a short.

Click here to see an example of a pull-up resistor in action.

Granted if the input pin is driving a logic low, the circuit won't work as intended, but at least nothing blows up. Also notice if the MCU drives the pin high (instead of low) the pull-up wont save you from that either. For that you need a series resistor (see above). So to be safe, in all cases having both the pull-up resistor AND a series resistor saves the day. Go ahead, click there and just try to break it.

Finally, with the pin configured properly and not being driven, this is how's its done. In all cases the input if referenced (not floating) and doesn't cause any shorts.

Now, if your design is not intended to be modified, probed, hacked or otherwise mucked with, maybe it's fine to forgo the external pull-up or series resistors. It really depends on being able to adequately consider the risk. In most hobby projects saving a resistor isn't going to make or break a project. Cost in this case usually isn't an issue. However, what what if this is going to be a kit, or an assembled product you want to sell or mass produce? It might make a difference, especially if there are lots of inputs, these costs could quickly add up. This is why you often see "ruggedized" boards for sale with these extra components at a significantly higher cost. Designs with these additional input protections are more difficult to design, layout and produce, but protect you from just about anything.

Even if cost is not an issue, real-estate on a PCB might be. So it might be worth considering to use the built in pull-ups or forgo the series resistor when this is the case. Again, as with most things you need to weigh the options and make trade offs.

In my case, it's usually not worth quibbling about. Just add them and don't rely on the internal ones. It's only a few cents and some space on the PCB. Usually I don't mind spending a little more of either one for a more robust design.

The above techniques described will help protect your MCU inputs from bugs in your software, misuse and and other perils. The techniques below will help protect them from the harsh realities of the outside world which are not always under your control.

Diode Clamps

Two reverse biased diodes connected between the input and each supply rail is a good way to prevent input voltages which are over or under the supply by the diode voltage drop (Vf) of the diode selected. This limits the input signal seen my the MCU pin to typically Vcc + 0.6V and Vss – 0.6V. Many MCUs already have them included so check your data sheet. Again the series resistor (Rs) described earlier is used to limit current through each diode when the out of range input voltage is applied needed since this is when either one of the diodes will begin to conduct. For example if Rs selected is 500-ohms and 10V is applied and the diode Vf=0.6V, then 18.8mA will flow through the resistor. Just how much current these diodes can handle is based on which diode is selected. For example a 1N4148 can handle peaks of 1A and steady current of 300mA, while a 1N4001 can handle peaks of 30A and a steady current of 1A.

Click here for an example of how this works. Notice how the 10 VAC input signal is clipped by the diode clamp and current limited by Rs.

Zener Diode Clamp

Adding a zener diode instead of the two reverse biased diodes above yields similar clamping behavior. Selecting a Zener diode with a breakdown voltage slightly higher than the expected input voltage will help prevent excessive voltage from reaching the MCU. Again as shown in the diode clamp circuit above, the Zener will prevent voltage exceeding the breakdown voltage by beginning to conduct it away from the input. Again, a series resistor (Rs) is required. As an example, a 5.6V Zener diode such as the 1N4734A can handle 1W of power dissipation with peaks of 810mA.

Click here for an example of how this works. Notice how the 10 VAC input signal is clipped by the zener diode and current limited by Rs.

Optical Isolation

Probably the holy grail of input protection. It will add more cost and complexity (parts) but is nearly invincible to most input conditions. The idea here is to place an electrically isolated component (an optoisolator) between the outside world and your MCU pin. This makes the connection optically coupled rather than electrically coupled. The optical couple requires that the input be translated between the optical world and the electronic world. This is done via a photo-emitter(LED) and photo-detector (transistor) pair.

Not all inputs can be coupled this way. Optoisolators are generally slow acting devices (4N25) and are only suitable for relatively slow changing inputs (switches). However faster devices are available (6N137). Furthermore, analog signals (those which vary between 0 and 5V) need special analog signal optoisolators (HCNR200) which meet the bandwidth requirements of the input signal.

Another thing to consider is the ability of your input signal to drive enough current through the LED to turn it on sufficiently. The data sheet of the optoisolator will tell you how much current the LED needs to signal the output photo-detector reliably (usually a few milliamps). The LED's forward resistor (Rf) in series with the supply and LED photo-emitter control the amount of current from the signal used. The pull-up resistor (Rpu) references the input signal to the MCU's supply voltage Vcc. This is needed for open-collector outputs such as the optoisolator shown.

Lastly, to achieve full optical isolation from the source signal and the destination MCU, the power supplies used should be different. In the diagram above, you will notice that the MCU's supply voltage is Vcc, while the signal side is V+. They can be the same, but they need not be (but they wont be completely electrically isolated if they are not, although the input will still be protected). In fact another benefit of optical isolation is that you can easily provide a different input voltage (say +12V signal) from the source, into an MCU's 3.3V-level input as well as virtually any other voltage combination. In this case the optoisolator also provides signal level translation.


A PPTC is a Polymeric Positive Temperature Coefficient device, used to protect circuits from excessive current. That's a big long description, but it is more commonly known as a self-resettable fuse. We all know how regular filament fuses work. Too much current and the filament breaks protecting the circuit from excess current. Later someone replaces the fuse.

With PPTC devices, as the current through the device increases, the temperate increases until it reaches its switching temperature resulting in a dramatic non-linear increase in the resistance of the device into the mega-ohm region. Once the power is removed the device will return to its normal operating state. If the over-current conditions continues to exist, the process starts again. PPTC devices exist which can exhibit this behavior in many seconds (very slow) or milliseconds (faster devices). This technology is based on the thermal induced changes in the device material, so they are also sensitive to changes in ambient temperature.

The PRG18BB221MB1RB is a common PPTC device. In normal conditions it acts like a 220-ohm resistor, limiting current to an input to about 30mA at which point the device trips. The hold current is typically around 10mA at +60degC. This device can handle as much as 24V input. Other devices with higher or lower normal condition resistance also exist.


The MOV is a Metal-Oxide Varistor, used to protect circuits from excessive or transient voltage spikes. Its a voltage dependent, nonlinear device that provides very good transient voltage suppression. When exposed to high transient voltage, the metal oxide varistor clamps voltage to a safe level. A metal oxide varistor absorbs potentially destructive energy and dissipates it as heat, thus protecting vulnerable circuit components and preventing system damage.

The Littlefuse V8ZA2P is a 5.5 VDC device used to protect low voltage circuits. It clamps voltages of up to 20V at a current of 5 amps with peak currents of 250A.

MOVs have been typically used on inputs from household AC line voltage into a circuit to be protected as a surge protector, but can be used for low voltage digital inputs as well. The MOV device is connect in between the input signal and ground.

When selecting a MOV, you will need to consider the maximum surge voltage you are trying to protect. MOVs specify the energy rating, in joules, that must not be exceeded. The transient energy rating may be calculated by W = ½ LI2, where L is the inductance of the component or the system inductance responsible for the transient. I is the current flowing in the inductance during the interruption. The peak current interrupted will determine the clamping voltage.


The TVS is a Transient Suppression Diode, used to also protect circuits from excessive or transient voltage spikes caused from ESD, inductive load switching and other transient sources. Similar to MOVs you need to consider the maximum amount of energy the device may need to absorb and the power handing capabilities of the device while conducting. When a transient voltage occurs, the TVS clamps instantly to limit the voltage to a safe level and conducts current away from the circuit being protected. This is called the clamping voltage (Vc). The operation is similar to a Zener diode, but TVS diodes are faster acting and are designed to react more quickly to transients. Zener diodes are designed for slow voltage changes and are more commonly used in voltage regulators.

If needed, there are also bi-directional TVS diodes, which will handle transients on the inputs in both directions.

There is an interesting selection page provided by Littlefuse which is helpful for selecting the right PPTC device, MOVs and TVS diodes. Worth a look.


While this certainly isn't an exhaustive survey, it should provide some background and thought before your next design. Next time you have any external device connected to some circuit input you will want to consider one or more of these techniques.