Bench NotesUno handbook

Reading a Pushbutton

Read a button with an external pull-down resistor and learn why an unconnected input floats.

BeginnerPushbuttonResistorBreadboard

Overview

A digital input reads HIGH or LOW, but only if something holds it at a defined voltage. A button alone connects the pin to 5 V when pressed and to nothing when released, so the released state "floats" and reads randomly. A pull-down resistor fixes that.

You will need

  • Arduino Uno R3
  • Momentary pushbutton
  • 10 kΩ resistor
  • Breadboard and jumper wires

Wiring

Uno pinConnects toNotes
5VButton, one side
2Button, other sideAlso goes to the 10 kΩ resistor
GNDOther end of the 10 kΩ resistorPull-down: pin reads LOW when released
Wiring for Reading a PushbuttonArduino Uno 5V to Pushbutton side A; Arduino Uno D2 to Pushbutton side B; Arduino Uno GND to Pushbutton side B through 10 kΩ.Connections (each line is one wire)Arduino UnoPushbutton5Vside AD2side BGND10 kΩ
Original wiring diagram. Each line is one jumper wire; boxes on a line are series resistors.

The sketch

pushbutton_pull_down.ino
const int BUTTON_PIN = 2;

void setup() {
  pinMode(BUTTON_PIN, INPUT);        // external pull-down resistor
  pinMode(LED_BUILTIN, OUTPUT);
}

void loop() {
  int pressed = digitalRead(BUTTON_PIN);   // HIGH while pressed
  digitalWrite(LED_BUILTIN, pressed);
}

How the code works

With a pull-down the button is active-high: HIGH means pressed. The more common alternative uses the built-in pull-up and no external resistor, which makes the button active-low. That is covered in Pull-ups and debouncing.

What you should observe

  • The L LED lights only while the button is held.
  • Remove the 10 kΩ resistor and the LED may flicker or light when you merely touch the wires: that is a floating input.