Sub topic 1
Reading a Button with `digitalRead()`
Prerequisites
You should know the basic Arduino sketch structure with setup() and loop(). You should also be comfortable with pinMode(), digitalRead(), digitalWrite(), if statements, Boolean values, and integer types in C++. This chapter assumes an Arduino board with a pushbutton, an LED, and a breadboard.
A Digital Input Has Two States
A digital input reads an electrical level as one of two values:
HIGH: the pin is near the board's logic voltage, usually 5 V or 3.3 V.LOW: the pin is near 0 V, or ground.
digitalRead(pin) samples the voltage on a configured input pin and returns HIGH or LOW.
A button does not produce a special “button” value. It simply connects or disconnects two points in the circuit. The wiring determines which digital value means “pressed.”
For this example, wire the button as follows:
- Connect one button terminal to 5 V.
- Connect the other button terminal to Arduino pin 2.
- Connect a 10 kΩ resistor from pin 2 to GND.
- Use the built-in LED, so no external LED wiring is required.
The resistor is a pull-down resistor. When the button is open, it keeps pin 2 at LOW. When the button is pressed, 5 V reaches pin 2 and the reading becomes HIGH.
Configure and Read the Pin
Set the pin mode once in setup(). Then read the pin in loop(). The following sketch turns on the built-in LED while the button is held down.
const uint8_t BUTTON_PIN = 2;
const uint8_t LED_PIN = LED_BUILTIN;
void setup() {
pinMode(BUTTON_PIN, INPUT);
pinMode(LED_PIN, OUTPUT);
}
void loop() {
int buttonState = digitalRead(BUTTON_PIN);
if (buttonState == HIGH) {
digitalWrite(LED_PIN, HIGH);
} else {
digitalWrite(LED_PIN, LOW);
}
}pinMode(BUTTON_PIN, INPUT) configures pin 2 as a high-impedance input. High impedance means the pin measures the circuit without intentionally supplying meaningful current.
The variable buttonState stores the latest sample. Comparing it with HIGH makes the condition clear. You could also write if (buttonState), because HIGH is represented by a nonzero value, but the explicit comparison is easier to read when learning or reviewing wiring logic.
loop() runs repeatedly. Therefore, this sketch continuously mirrors the current button level: pressed means the LED is on, and released means the LED is off.
| Button condition | Pin 2 voltage | `digitalRead(BUTTON_PIN)` | LED |
|---|---|---|---|
| Released | Near 0 V | `LOW` | Off |
| Pressed | Near 5 V | `HIGH` | On |
Levels Are Not Press Events
This sketch detects a level, not an event. If the button remains pressed for one second, many passes through loop() see HIGH, so the LED remains on for that entire time.
That behavior is correct when you want to track whether a control is currently active. It is different from “do something once when the button becomes pressed.” Detecting that transition requires comparing the current reading with a previous reading. Mechanical buttons also create rapid unwanted transitions when their contacts meet. Those topics build on this basic reading operation.
Exercise
With the wiring in this chapter, what value should the sketch read when the button is released, and why?
Show answer
It should read LOW because the pull-down resistor connects pin 2 to ground while the open button disconnects it from 5 V.