Sub topic 3
How a Push Button Connects to a Pin
Assume you know basic Arduino sketches, voltage, current, resistance, breadboards, and digitalWrite. This chapter focuses on the electrical path between a push button and a digital input pin.
A push button is a switch. When you press it, the switch connects two terminals. When you release it, the terminals disconnect. The Arduino pin does not measure “pressed” directly. It measures the voltage that reaches the pin.
A button needs a complete logic path
A digital input should see one of two valid logic levels:
HIGH: a voltage close to the board's supply voltage, usually 5 V or 3.3 V.LOW: a voltage close to 0 V, connected to ground.
The button changes the connection between the pin and a voltage source. A resistor provides the opposite connection when the button is open.
With an external pull-down arrangement:
- Connect one button terminal to 5 V.
- Connect the other button terminal to the input pin.
- Connect a resistor, commonly 10 kΩ, from the input pin to GND.
- Released: the resistor pulls the pin to
LOW. - Pressed: the button connects the pin to 5 V, so it reads
HIGH.
The resistor limits current when the button is pressed. Without it, a direct connection from 5 V to GND could create a short circuit.
Wiring a four-leg tactile switch
Many breadboard buttons have four legs, but they usually contain only two electrical sides. The two legs on one side are internally connected. The two legs on the other side are also internally connected. Pressing the button connects these two sides.
Place the button across the breadboard's center gap. This orientation keeps each pair of connected legs on opposite sides of the gap. If the button is placed incorrectly, the two sides may already be connected through the same breadboard rows, so pressing it will not change the input.
A multimeter in continuity mode can identify connected legs. With the button released, each same-side pair should show continuity. Pressing the button should create continuity between the two sides.
Read the pin in a sketch
Configure the button pin as an input. Then use digitalRead to obtain its current logic level.
const int buttonPin = 2;
const int ledPin = 13;
void setup() {
pinMode(buttonPin, INPUT);
pinMode(ledPin, OUTPUT);
}
void loop() {
if (digitalRead(buttonPin) == HIGH) {
digitalWrite(ledPin, HIGH);
} else {
digitalWrite(ledPin, LOW);
}
}This program lights the LED only while the button is held. It responds to a level, not to a single press event.
Pull-up wiring uses the opposite arrangement
A pull-up resistor connects the input pin to 5 V. The button connects the pin to GND.
- Released: the pin reads
HIGH. - Pressed: the button pulls the pin to GND, so the pin reads
LOW.
This inverted arrangement is common because many Arduino boards provide an internal pull-up. It uses fewer external parts, but your condition must test for LOW when checking for a press.
Common wiring errors
| Symptom | Likely cause | Check |
|---|---|---|
| Random readings | No pull-up or pull-down path | Verify the resistor connects to 5 V or GND |
| Always `HIGH` | Pin is tied to 5 V or the switch is miswired | Check button orientation and pin voltage |
| Always `LOW` | Pin is tied to GND or the switch is shorted | Test the button legs for continuity |
| Button has no effect | Wrong pin number or breadboard row | Trace every wire from the pin |
Exercise
In a pull-down circuit, what should the input read when the button is released, and why?
Show answer
It should read LOW because the resistor connects the input pin to GND while the open button does not connect it to 5 V.