Participants of our Arduino 101 workshop (and thanks for coming, it was a blast!) would have encountered the idea of a "pull up", or "pull down" resistor, while sensing a button press with the Arduino. Though they get a fancy name, a pull up/down resistor is just a normal resistor, but used in a specific configuration for a specific purpose. This article will tell you why we need a pull up/down resistor, the difference between the configurations, and what value resistances to use.
Why we need them?
Imagine we have an input pin on a microcontroller, (maybe an Arduino, where we've done something like pinMode(3, INPUT) ), and we want to use it to sense whether a button is pressed, or not pressed. We know the microcontroller can sense HIGH (Vcc, +5V), or LOW (Gnd, 0V) so we hook up one side of the button to ground, and the other to the input pin, like so.
When the button is pressed, the input pin is connected directly to ground, and the pin will read LOW, which is great. How about when the button is not pressed? The input pin is not connected to anything! This is referred to as having the pin floating, and is something to avoid. If the pin state is read, it may be HIGH, it may be LOW, or it may be something else entirely. Not good.
Logic then says, hook it up the Vcc like so, that way when the button is not being pressed, the pin is connected to 5V and will definitely read HIGH. Problem solved? Not really!
Yes, the input will definitely read HIGH when the button is not pressed. However when the button is pressed, you should see there is now a short circuit from Vcc to ground. This is definitely not good, we want to avoid this at all costs (unless you want to release the magic smoke)!
This is where the pull up resistor comes in. By adding in a resistor between VCC and the input pin, the input pin is pulled high when the button is not pressed, and pulled low when the button is pressed. So the resistor, when connected to Vcc in this configuration, is called a pull up resistor. With the pull up resistor, instead of a short circuit when the button is pressed, current is limited by the resistor as it flows to ground, while still giving us definite HIGH/LOW readings at the input pin when the button is not pressed/pressed.
If you want the opposite HIGH/LOW, not pressed/pressed behaviour, you can use the pull down configuration, shown below. The pin is pulled down to LOW by the resistor when not pressed, and pulled up to HIGH when pressed.
Choosing R Values
TLDR: About 10k ohms is usually good.
You are balancing 2 things when choosing the value of the pull up resistance:
Smaller values of R means more current flows when the button is pressed, more power used, and more heat generated.
Larger values of R means the voltage at the pin might not be equal to Vcc like you would expect.
Without delving too deep into the rabbit hole, input pins on microcontrollers have a parameter called the input leakage current. This is, an often very small, current that flows into the input pin when it is connected to a voltage.
Using Ohm's law, we can ballpark some values. If the leakage current is 1uA, and we say that a 0.01V below Vcc is an acceptable voltage at the input pin (i.e. 4.99V instead of 5V), then
Contrast this if we chose instead a 1M ohm resistor, we get a 1 volt drop across the resistor, and 4V at the input pin instead of the desired 5V HIGH value!
Usually leakage currents are quite small, so you pull up resistor values can go quite high before you'll have a problem. But it's often worth checking the data sheet. The other time large resistor values can get you into trouble is when you need fast switching times (e.g. for comms pins), but for simple switches and buttons, this is not usually a problem.
To make it all even easier, a lot of microcontrollers, including those used on the Arduino, allow you to use internal pull up resistors on input pins, so you don't even need to use a physical resistor. You just specify in software pinMode(3, INPUT_PULLUP) and you're done!
Using a pull up or pull down resistor, you should now have no trouble integrating a switch or button into your next microcontroller based project. If you're feeling diligent, grab that datasheet and get calculating, otherwise, reaching for the trusty 10k Ohm resistor probably won't see you wrong.