Digital Input

Arduino - Digital Input Tutorial

Pins 1-13 can be used as inputs or outputs. So far we've only used them as outputs. You set the pin to be an input in a similar fashion to setting the pin mode to an output as we've done in previous lessons.

You will need to build the circuit shown in the schematic. Pin 2 will be looking for an input of either HIGH or LOW. digitalRead looks to see whether we have positive or negative voltage on the pin of interest. If it is positive it is HIGH or one, if it is negative it is LOW or zero. Technically you don't need the 100 ohm resistor. It's there to protect the Arduino chip from a short circuit.

The size of the two resistors are not critical. They are a rough guide. The 10k resistor can be as small as 4.7k and there is really no maximum, but if it is too big the results can be unpredictable. The presence of the 10K resistor is critical, it is called a pull down resistor. There are three possible states an input can be: High (positive), Low (negative), or nothing. Unfortunately, the Arduino has only two choices, High or Low. If you leave out the pull down resistor the pin can never "see" Low and the results will be totally unpredictable. It might appear to work perfectly one moment and then suddenly not work. If you were to simply put a wire in for the 10k ohm resistor then you'd cause a short circuit any time you pressed the button.

 Reads a digital input on pin 2, prints the result to the serial monitor 
 This example code is in the public domain.
int sensorValue

void setup() {
  pinMode(2, INPUT);

void loop() {
  sensorValue = digitalRead(2);

  1. Run the code above and turn on the Serial Monitor. Press the button and see what you get.
  2. Try removing the 10k ohm resistor. Does the circuit still behave the same?
Quick Review: Included in the program below is an "if" statement. An "if" statement asks a question. If the statement is true it runs the following code in the curly braces once. If false the program just continues.

New Code: Here we expand on the "if" by adding an "else". Now if the "if" is false it will execute the code in the curly braces after the "else". The "else" code will be skipped if the "if" was true.

 Sends a message to the computer to say whether the button
 when pressing a pushbutton attached to pin 2. 
 The circuit:
 * pushbutton attached to pin 2 from +5V
 * 10K resistor attached to pin 2 from ground
 created 2005
 by DojoDave 
 modified 17 Jun 2009
 by Tom Igoe
 modified 30 Jan 2011
 by Steve Dickie

// constants won't change. It's used here to 
// set the pin number:
const int buttonPin = 2;     // the number of the pushbutton pin

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status

void setup() {
  pinMode(buttonPin, INPUT);   // initialize the pushbutton pin as an input:  
  Serial.begin(9600);          // initialize the serial communication:

void loop(){
  buttonState = digitalRead(buttonPin);  // read the state of the pushbutton value:
  if (buttonState == HIGH) {             // check if the pushbutton is pressed. 
    Serial.println("Button Pressed");    // send a message to the computer:    
  else {                                 // If the button is not pressed
    Serial.println("Waiting");           // send a message to the computer:

Run the program, push the button. What do you see?

Additional tasks

  1. Swap the positions of the wire and the 10k resistor attached to the push button (by this I mean plug the resistor into positive and the wire into negative). Run the program again. What do you see? Why is it different?
  2. Add an LED to pin 13 on your Arduino (you should know ho to do this) and modify the program so that the Arduino turns on and off the LED when the button is pressed.
  3. Change the program so that when the button is pushed the light turns on for 5 seconds and then shuts off.
  4. Build the LED sequencer you built in the last lesson. Have the leds light up one after the other and repeat and when you push the button have them reverse direction.