How to Use Digital INPUT and INPUT_PULLUP on Arduino
Updated: Sep 23
Reading values from the digital pins is one of the most fundamental skills when starting to use the Arduino. In this tutorial, I'll demonstrate how to read values using the digital pins using pull up and pull down resistors.
For this exercise you'll need:
an Arduino Uno,
two 10kΩ resistors,
solid core jumper wires,
two push buttons,
and stranded jumpers wires.
All of these parts can be found in the Arduino starter kit.
Building the Circuit
This first part of the circuit will be setup so that if a push button is pressed, a digital "HIGH" can be read, and a digital "LOW" can be read if the button is not pressed.
As with most of the Arduino exercises, start by connecting 5V and GND to the power bus rails on the breadboard.
Connect a push button over the center of the breadboard.
Connect one end of the push button to 5V.
Use a 10kΩ resistor to connect the other end of the push button to ground.
Use a jumper to connect Pin 2 to be in parallel with the resistor.
The second part of the circuit will be setup so that if a push button is pressed, a digital "LOW" will be read, and if the button is not pressed, a digital "HIGH" will be read.
Connect another push button over the center of the breadboard.
Wire a 10kΩ resistor from 5V to the push button.
Ground the other side of the button.
Connect a jumper from Pin 3 be in parallel with the resistor.
The resistor in the bottom circuit is called pull down resistor, because when the switch is not closed, the resistor is pulling the pin down to ground. The resistor in the other circuit is called a pull up resistor, because when the button is not being pressed, the pin is being pulled up to 5V.
The code for this example can be found here. It may be useful to have this sketch open while you read the this section.
Outside of void setup() and void loop(), define two variables to 2 and 3. These represent digital pin numbers.
In the setup, enable serial communication using the command, Serial.begin(9600). The "9600" represents the baud rate. Then use the command, pinMode() to assign the previously defined variables as inputs. There are two arguments that are needed for pinMode(). The first one is the pin number, and the second argument, defines whether that pin will be an input or output.
In the loop, define two variables as integers. The variables will be used to store the value being read by pin 2 and 3. This is done by using the digitalRead() command. The only argument that this function requires is the pin number that is being read. Then use the Serial.print() command to print the values. Serial.print() will print any variable inside of the parenthesis. Strings can also be printed if they are surrounded by quotes. Serial.println() will make so that the next print statement is printed on the next line. To view the print statements, click on the magnifying glass symbol, in the upper right corner, to open the serial monitor.
In the circuit with the pull down resistor, the printed value will say "0" if the button is not being pressed, and "1" if it is. A value of "0" is the same thing as "LOW" and a value of "1" is the same thing as "HIGH."
For the circuit with the pull up resistor, a value of "1" will print, if the button is not being pressed, and a value of "0" will print if the button is being pressed.
How to use INPUT_PULLUP
Now let's experiment with the circuit with the pull up resistor. If you remove the resistor and open the serial monitor, you may notice that there is inconsistent behavior when pushing the button. It's even possible to change the "HIGH" and "LOW" values by simply touching the metal jumper.
This is happening because, there is nothing to pull Pin 3 up to 5V when the button isn't being pushed.
If you don't want to use a resistor in your circuit, you can solve this problem by using the Arduino Uno's built in pull up resistor. To fix this, go back to the setup, and change digital pin definition from "INPUT" to "INPUT_PULLUP." Once you do this, the circuit will behave exactly as it did before.
In the next post in this series, I will build off of this same circuit and sketch to demonstrate how to use digital outputs and "if" statements.
If you would like to stay up to date with this Introduction to Arduino series, subscribe below.
The Bored Robot LLC is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com