Utility Bar

Read a Push Button on Arduino

Ethan Zaitchik |

So far you've told your Arduino what to do. This lesson is about the reverse: letting the outside world tell your Arduino something. A push button is the simplest way to send a digital signal into a pin, and reading that signal, known as a digital input, is a skill you'll reuse in almost every project from here on, whether it's a button, a switch, a sensor with a digital output, or a limit switch. By the end of this guide you'll understand why a digital input needs a pull resistor at all, how to wire and read a push button, and how to use that input to control an LED.

Table of Contents
    Momentary push button and Arduino Uno shown at an angle

    What You'll Need

    Want everything for this project in one box?

    Our Arduino Super Starter Kit includes the board, push button, LEDs, resistors, breadboard and jumper wires used in this guide, so you can just open the box and start building.

    Shop the Arduino Starter Kit →

    What Is a Digital Input?

    A digital pin on an Arduino can work two ways. As an output, the Arduino sends a signal out, which is how the LED guide turned a pin HIGH or LOW to switch an LED on and off. As an input, the Arduino instead listens to a pin and reads whether it's HIGH or LOW. A push button is the simplest possible source for that signal: pressed or not pressed, on or off, HIGH or LOW.

    Why You Need a Pull Resistor

    A digital input pin that isn't connected to anything definite doesn't reliably read as HIGH or LOW. It "floats," picking up tiny amounts of electrical noise from nearby wires and components, which can flip the reading randomly even when nothing is pressed. A pull resistor fixes this by holding the pin at a known state until the button forces it to the other state.

    There are two ways to do this:

    • Pull-up: the pin is held HIGH by default. Pressing the button connects it to GND, pulling it LOW. This is what we'll use, via the Arduino's internal pull-up resistor, no extra component required.
    • Pull-down: the pin is held LOW by default using an external resistor to GND. Pressing the button connects it to 5V, pulling it HIGH. This needs a physical resistor in the circuit, since Arduino boards don't have an internal pull-down option.

    Both approaches work, they just read the button backwards from each other. We're using the internal pull-up because it means one less component and one less thing that can be wired wrong.

    Side-by-side diagram comparing pull-up wiring (internal resistor, button to GND) and pull-down wiring (external resistor, button to 5V)

    Wiring the Push Button

    With the Arduino unplugged from USB, build the circuit as follows:

    1. Place the push button on the breadboard, straddling the centre gap.
    2. Connect one leg of the button to digital pin 2 on the Arduino.
    3. Connect the opposite leg (on the other side of the centre gap) to a GND pin on the Arduino.

    No resistor is needed for this part, the internal pull-up resistor handles it in code. You can use any digital pin here, we're using pin 2, but pins 2 through 13 all work equally well.

    IMAGE PLACEHOLDER: breadboard wiring diagram, push button straddling centre gap, jumper to pin 2 and jumper to GND

    Reading the Button

    Connect your Arduino to your computer with the USB cable, open the Arduino IDE, and make sure the correct board and port are selected under Tools. Paste in the sketch below and click Upload.

    /*
     * Ethan Zaitchik
     * Arduino Digital Input Example - Push Button
     * Full guide: https://zaitronics.com.au/blogs/arduino/arduino-digital-input-push-button
    */
    
    int buttonPin = 2;
    int buttonState = 0;
    
    void setup() {
      pinMode(buttonPin, INPUT_PULLUP);
      Serial.begin(9600);
    }
    
    void loop() {
      buttonState = digitalRead(buttonPin);
      Serial.println(buttonState);
      delay(500);
    }

    Open the Serial Monitor (Tools > Serial Monitor, or Ctrl+Shift+M) with the baud rate set to 9600. You should see a stream of 1s. Press and hold the button, and it should switch to 0 for as long as you're pressing it.

    IMAGE PLACEHOLDER: Serial Monitor screenshot showing the output switching between 1 and 0 as the button is pressed and released

    Here's what that looks like on the bench:

    Code Explanation

    int buttonPin = 2;
    int buttonState = 0;
    • buttonPin stores which pin the button is wired to. buttonState stores whatever that pin currently reads.
    void setup() {
      pinMode(buttonPin, INPUT_PULLUP);
      Serial.begin(9600);
    }
    • pinMode(buttonPin, INPUT_PULLUP) sets the pin to read input, and enables the Arduino's internal pull-up resistor so the pin defaults HIGH until the button pulls it LOW. Serial.begin(9600) starts serial communication so we can watch the readings on the computer.
    void loop() {
      buttonState = digitalRead(buttonPin);
      Serial.println(buttonState);
      delay(500);
    }
    • digitalRead(buttonPin) checks the pin's current state and stores it. Because we're using a pull-up, this reads 1 (HIGH) when the button isn't pressed and 0 (LOW) when it is, the reverse of what you might expect. Serial.println(buttonState) prints that value, and delay(500) pauses for half a second before reading again, which keeps the Serial Monitor slow enough to read.

    Extend It: Use the Button to Control an LED

    Reading a value in the Serial Monitor is useful for testing, but the real point of a digital input is usually to trigger something else. If you've already built the external LED circuit from our LED blink guide, on pin 5, leave it wired up and add the button circuit alongside it. If not, wire an LED with a 220Ω resistor to pin 5 and GND the same way that guide describes.

    IMAGE PLACEHOLDER: combined breadboard diagram showing both circuits, button on pin 2 and LED on pin 5, wired together on one board

    /*
     * Ethan Zaitchik
     * Arduino Digital Input Example - Button Controls LED
     * Full guide: https://zaitronics.com.au/blogs/arduino/arduino-digital-input-push-button
    */
    
    int buttonPin = 2;
    int ledPin = 5;
    int buttonState = 0;
    
    void setup() {
      pinMode(buttonPin, INPUT_PULLUP);
      pinMode(ledPin, OUTPUT);
    }
    
    void loop() {
      buttonState = digitalRead(buttonPin);
    
      if (buttonState == LOW) {
        digitalWrite(ledPin, HIGH);
      } else {
        digitalWrite(ledPin, LOW);
      }
    }

    Upload this and the LED should light up only while the button is held down. The logic is a simple if statement: when buttonState reads LOW (pressed, remember it's inverted with a pull-up), turn the LED on. Otherwise, keep it off.

    Expected Outcome

    With the first sketch, the Serial Monitor should print a steady stream of 1s that switches to 0 while the button is held. With the second sketch, the LED should turn on only while the button is pressed and switch off the instant it's released.

    Troubleshooting

    Serial Monitor shows nothing

    Check the baud rate in the Serial Monitor dropdown matches Serial.begin(9600) in the code, and confirm the right port is selected under Tools.

    Reading never changes when I press the button

    Confirm the button is actually straddling the breadboard's centre gap. If it's placed on one side only, both legs may already be connected to each other regardless of whether it's pressed.

    Reading is inverted from what you expected

    This is normal with INPUT_PULLUP. The pin reads HIGH (1) when not pressed and LOW (0) when pressed, the opposite of a pull-down setup.

    LED doesn't respond to the button

    Check the LED circuit independently first using the blink sketch from the LED guide. If that works, confirm pin 5 in the code matches the pin your LED is actually wired to.

    Frequently Asked Questions

    Do I need a resistor for the push button?

    Not if you use the internal pull-up resistor, as shown in this guide. You'd only need an external resistor if wiring a pull-down configuration instead.

    Why does my button read backwards, 0 when pressed?

    That's expected with INPUT_PULLUP. The pin is HIGH by default and the button pulls it LOW when pressed. If you want pressed to read HIGH, you'd need a pull-down circuit instead.

    Can I use any digital pin for the button?

    Yes, any digital pin works for a basic digitalRead. We used pin 2, but 2 through 13 are all suitable, just update the buttonPin variable to match your wiring.

    What's the difference between a digital input and an analog input?

    A digital input only reads two states, HIGH or LOW, on or off, like this button. An analog input can read a range of values, which is what you'd use for something like a potentiometer or a light sensor.

    Arduino Lessons

    Lesson Complete

    You've now covered both halves of Arduino I/O: sending a signal out with the LED guide, and reading one in with this guide. Combined, that's the foundation almost every future project builds on.

    Next lesson: Reading an Analog Input Using a Potentiometer (link once published)

    Leave a comment