Wiring guide

How to wire a push button to an Arduino Nano

To wire a push button to an Arduino Nano, connect 1a to pin 2 and 2a to GND. The button goes between the pin and GND and uses the board’s internal pull-up, so there is no resistor and no supply wire, and the pin never sees more than 5 V. The sketch below uses no library beyond what the board includes. It waits for the button, debounces it for 20 ms and prints a running count of presses.

  • 5 V logic
  • Library: none
  • 2 wires
Arduino Nano · push buttonlive0.000 s 0.00x
Hold the button
The circuit itself, running on the simulator with the sketch below. Press what can be pressed; click anything else to open it in the editor.
Pin to pin

Push button to Arduino Nano wiring table

Every wire in the circuit above, with the part’s pin names and the board’s own labels. The wire colors are the ones the circuit uses; on a real bench any colors work.

Push button pinArduino Nano pinWire colorWhy
One side (1a)pin 2amberWith INPUT_PULLUP the ATmega328P’s own resistor holds the pin high.
The other side (2a)GNDbluePressing connects the pin to ground, so it reads LOW.
Voltage

Power and logic levels for push button and Arduino Nano

Logic level

The Arduino Nano’s pins are 5 V logic. A 5 V part can drive them directly; a part whose signal pins are limited to 3.3 V needs a level shifter.

This pair

No resistor and no supply wire: the ATmega328P’s internal pull-up ties the pin to 5 V until the button pulls it to ground.

Pins to leave alone

A6 and A7 are analog only: pinMode(A6, OUTPUT) does nothing and digitalRead(A7) is not there to call. D0 and D1 are the USB serial port.

The sketch

The working code for the Arduino Nano

Library: none, the sketch reads the pin with digitalRead() and its internal pull-up. This is the sketch the circuit above runs.

Debounced button on Arduino Nano pin 2
const int BUTTON_PIN = 2;   // pin 2 to the button, the button to GND
const unsigned long DEBOUNCE_MS = 20;

int lastReading = HIGH;
int state = HIGH;
unsigned long changedAt = 0;
unsigned int presses = 0;

void setup() {
  Serial.begin(9600);
  pinMode(BUTTON_PIN, INPUT_PULLUP);   // HIGH when up, LOW when pressed
  Serial.println("Press the button");
}

void loop() {
  int reading = digitalRead(BUTTON_PIN);
  if (reading != lastReading) changedAt = millis();
  lastReading = reading;
  if (millis() - changedAt > DEBOUNCE_MS && reading != state) {
    state = reading;
    if (state == LOW) {
      presses++;
      Serial.print("Pressed, count ");
      Serial.println(presses);
    }
  }
}
Output

What you should see

Open the serial monitor at 9600 baud. The sketch prints Press the button once. Each press then adds a line, Pressed, with a count that goes up by one; holding the button down counts once, because the sketch reacts to the press and not to the level.

A button that reads pressed all the time is wired to the wrong pin pair or the pull-up is off; one that counts twice per press is bouncing and needs the debounce time raised.

The serial monitor prints this, recorded from the circuit above with the button pressed once after 300 ms:

Press the button
Pressed, count 1
Pitfalls

Common mistakes wiring a push button to an Arduino Nano

A floating input

With plain INPUT and no resistor, the pin reads whatever noise is nearby and the sketch sees presses nobody made. Use INPUT_PULLUP, or a 10 k resistor to the supply.

Counting the bounce

Without a debounce, one press can count as three or four. The simulated button bounces for a few milliseconds the way real contacts do, so the fix here is the fix on a desk.

Picking A6 or A7

A6 and A7 look like the other analog pins but are inputs to the ADC and nothing else. the button on either of them never sees a digital level. Any of D2 to D13 or A0 to A5 works.

Build your own push button project

Open this circuit in the editor, change the wiring or the code, and keep your version in a free account.