0
0
AutocadHow-ToBeginner · 3 min read

How to Read Button Press in Arduino: Simple Guide

To read a button press in Arduino, connect the button to a digital pin and use pinMode(pin, INPUT_PULLUP) to set the pin mode. Then, use digitalRead(pin) inside loop() to check if the button is pressed (LOW) or not (HIGH).
📐

Syntax

Here is the basic syntax to read a button press in Arduino:

  • pinMode(pin, INPUT_PULLUP); sets the button pin as input with an internal pull-up resistor.
  • digitalRead(pin); reads the current state of the button pin.
  • The button is considered pressed when the pin reads LOW because the button connects the pin to ground.
arduino
const int buttonPin = 2;

void setup() {
  pinMode(buttonPin, INPUT_PULLUP);
}

void loop() {
  int buttonState = digitalRead(buttonPin);
  if (buttonState == LOW) {
    // Button is pressed
  } else {
    // Button is not pressed
  }
}
💻

Example

This example turns on the built-in LED when the button is pressed and turns it off when released.

arduino
const int buttonPin = 2;
const int ledPin = LED_BUILTIN;

void setup() {
  pinMode(buttonPin, INPUT_PULLUP);
  pinMode(ledPin, OUTPUT);
}

void loop() {
  int buttonState = digitalRead(buttonPin);
  if (buttonState == LOW) {
    digitalWrite(ledPin, HIGH);  // Turn LED on
  } else {
    digitalWrite(ledPin, LOW);   // Turn LED off
  }
}
Output
When the button connected to pin 2 is pressed, the built-in LED lights up; when released, the LED turns off.
⚠️

Common Pitfalls

Common mistakes when reading button presses include:

  • Not using INPUT_PULLUP or an external pull-up/down resistor, causing floating input and unreliable readings.
  • Assuming HIGH means pressed; with pull-up wiring, pressed is LOW.
  • Not debouncing the button, which can cause multiple rapid triggers.
arduino
/* Wrong way: No pull-up resistor, floating input */
const int buttonPin = 2;

void setup() {
  pinMode(buttonPin, INPUT);  // No pull-up
}

void loop() {
  int buttonState = digitalRead(buttonPin);
  if (buttonState == HIGH) {
    // Might read random values
  }
}

/* Right way: Using internal pull-up resistor */

void setup() {
  pinMode(buttonPin, INPUT_PULLUP);
}

void loop() {
  int buttonState = digitalRead(buttonPin);
  if (buttonState == LOW) {
    // Button pressed
  }
}
📊

Quick Reference

ConceptDescription
pinMode(pin, INPUT_PULLUP)Sets pin as input with internal pull-up resistor
digitalRead(pin)Reads the current state of the pin (LOW when pressed)
Button pressed stateDetected when digitalRead returns LOW
DebouncingNeeded to avoid multiple triggers from one press
WiringButton connects pin to ground when pressed

Key Takeaways

Use INPUT_PULLUP mode to avoid floating input pins when reading buttons.
digitalRead returns LOW when the button is pressed with pull-up wiring.
Always connect the button to ground and the input pin for reliable reading.
Debounce buttons in code or hardware to prevent false multiple presses.
Test your wiring and code carefully to ensure correct button detection.