In understanding how pull-up and pull-down resistors work for microcontroller inputs, and why they’re necessary, it’s important to see a circuit diagram. Unfortunately, most circuit diagrams on the Internet are somewhat incomplete – that is, they’re usually missing the microcontroller along with its input impedance (resistance) and ground. Without these, it can be somewhat unclear when attempting circuit analysis (using Ohm’s Law and the like) just exactly why pull-up and pull-down resistors work. The following circuit diagrams and calculations are an attempt to provide a more complete picture.
To understand the images below, you’ll need to make sure you understand basic circuit analysis, including Ohm’s Law: V = IR (a Google or YouTube search on the topic should be sufficient to get you started).
It’s easy to assume that if a microcontroller pin is not connected to anything, it must be at 0V. Unfortunately, this is not always the case, as the pin is considered “floating” and is subject to slight changes in voltage due to environmental noise. The purpose of a pull-up or pull-down resistor is to ensure that an input pin has a known voltage at any given point in time. A pull-up or pull-down resistor can also protect against short-circuiting your power supply.
The diagram below shows a basic switch (like a pushbutton) connected to a microcontroller input with a 10 kΩ pull-up resistor Rp. The impedance of the microcontroller’s input (which can be found in the specific microcontroller’s documentation) is modeled with a 100 MΩ resistor Ri. My Ri value was taken from documentation for an Arduino. As you’ll soon see, resistor Rp is called a “pull-up” resistor because it helps in “pulling” the voltage of the input pin “up” to a known level, around 5V in our case, when the switch is open.
Notice that if Rp and the 5V source were not in this circuit, the microcontroller input would be floating while the switch is open (imagine cutting out the entire lefthand side of the diagram). Because of Rp and the 5V source, however, the input is at a known state of HIGH when the switch is open. HIGH usually corresponds to a range of about 3V to 5V for a 5V microcontroller, while a voltage less than 3V registers as LOW.
You might ask yourself: Why have the pull-up resistor? Wouldn’t the voltage at the input be HIGH without it? The answer is yes, but take a look at what would happen if the switch were closed.
When the switch is closed, the voltage at the input will be pulled to ground, registering as LOW. Because the current will take the path of least resistance from the source to ground, all the current will go through the switch to ground, rather than going through the microcontroller input. Now, imagine that our pull-up resistor Rp were not in the picture above. What would happen then? The input would still be grounded as desired, but we would also have a short circuit! Thus, Rp serves its purpose in that it allows us to keep the microcontroller input from floating while also keeping us from short-circuiting our power supply when the switch is closed.
Pull-down resistors function similarly to their pull-up counterparts, except that they will help in “pulling” the input voltage “down” to ground when the switch is open.
When the switch is closed, as seen below, the microcontroller input is pulled HIGH, as it will be connected directly to the 5V source.
Note that Rp is still essential in order to prevent a short circuit while the switch is closed.
Some microcontrollers, like Arduino, contain internal pull-up resistors that are configurable via software. While these are useful and can eliminate the need to use external resistors, they should be used with caution. Failing to properly configure an internal pull-up can result in a short circuit if a switch is closed while connected to an active power supply.
A couple of very good questions naturally come up when thinking about pull-up and pull-down resistors:
- What’s a good value for a pull-up or pull-down resistor?
- Which variant, pull-up or pull-down, should I use?
In answer to question #1, it’s a good rule-of-thumb to use a resistor value that is at most one tenth of the microcontroller’s input impedance. The goal is to pick a value that is large enough to keep the power supply from overheating or nearly shorting but also small enough to limit the current going into the microprocessor input pin. For the case of a pull-up resistor with an open switch, a resistor value that is too large could also cause the voltage at the input to become too low to be registered as HIGH.
The answer to question #2 is that it’s largely project-dependent. It seems the general opinion is that it doesn’t matter all that much whether you use a pull-up or a pull-down resistor circuit. They’re both implemented very similarly, and there will likely be other project constraints that govern which flavor is ultimately chosen.
One possible consideration might be power savings for input devices are normally open (NO) or normally closed (NC). It can be seen in the circuit diagrams above that a NO input device connected with a pull-up resistor would cause a small amount of current to flow into the microcontroller most of the time (the time in which the switch is open), while using a pull-down resistor would cause no current to flow most of the time. If, on the other hand, the device were an NC device, a pull-up resistor would cause less overall current to flow into the microcontroller input than a pull-down resistor.