Sub topic 3
How a Push Button Connects to a Pin
This chapter covers the physical side of reading a button: what the part is, how its legs behave, and how it is wired to an Arduino pin. It assumes you can write a basic sketch with setup() and loop(), use Ohm's law (V = I × R), place parts on a breadboard, and drive an LED with digitalWrite. It also assumes you have met pinMode and digitalRead.
The circuit built here is complete and works. The next chapters explain in detail why it needs its resistor, so treat the resistor as a given for now.
What a push button is, electrically
A push button is a momentary, normally-open switch. "Normally open" means that with no force applied, no current can flow through it. "Momentary" means that it returns to that state as soon as you let go.
A button does not produce a voltage or a signal of its own. It only connects two points or leaves them disconnected. Everything the Arduino reads from a button comes from what those two points are attached to. That is why the wiring matters more than the code.
The four-leg tactile switch
The common breadboard button is a tactile switch with four legs. The legs are connected in pairs inside the part. Pressing the cap closes a metal contact that joins the two pairs.
| Button state | Legs on the same side | Legs on opposite sides |
|---|---|---|
| Released | Always connected | Not connected |
| Pressed | Always connected | Connected |
Because of this, the four legs act as one switch with two terminals. Each terminal is a pair of legs, and you only need one leg from each pair.
On a breadboard the button usually straddles the center gap, with two legs on each side of the gap. The rows on each side of the gap are joined along their five holes, so the two legs of one pair land in the same row group. This gives you a simple rule.
- Legs across the gap, from one side to the other, are the two terminals of the switch.
- Legs on the same side of the gap are the same terminal.
If the button is turned 90 degrees, the pairs line up differently. The result is either a button that is always closed or one that never closes. This is the most common wiring mistake with these parts, and the symptom is easy to misread as a software bug.
You can check the orientation with a multimeter in continuity mode. Probe two legs on the same side, and the meter should beep. Probe two legs on opposite sides, and it should stay silent until you press the cap.
The idea: a button joins a pin to a voltage
An input pin reports the voltage it sees. The button's job is to change which voltage that is. In the basic arrangement, one terminal of the button goes to the digital pin. The other terminal goes to a fixed voltage, which is either 5 V or GND.
- Button to 5 V. Pressing the button connects the pin to 5 V, so
digitalReadreturnsHIGH. - Button to GND. Pressing the button connects the pin to ground, so
digitalReadreturnsLOW.
This covers only half of the problem. When the button is released, the pin is connected to nothing at all. A pin that is connected to nothing does not read a reliable LOW. The next chapter shows what it reads instead. The fix is a second connection that holds the pin at a known level whenever the button is open. That second connection is a resistor.
So a working button circuit always has two parts:
- A button that connects the pin to one voltage when pressed.
- A resistor that connects the pin to the opposite voltage when the button is released.
Two wiring patterns
There are two ways to arrange these parts. They differ in which voltage the button connects to and which voltage the resistor holds.
| Pattern | Button connects pin to | Resistor connects pin to | Released reads | Pressed reads |
|---|---|---|---|---|
| Pull-down | 5 V | GND | `LOW` | `HIGH` |
| Pull-up | GND | 5 V | `HIGH` | `LOW` |
The resistor is named by its direction. A pull-down resistor pulls the pin down to GND when nothing else is driving it. A pull-up resistor pulls it up to 5 V. Later chapters cover each in turn, along with the built-in pull-up that removes the need for a separate resistor.
This chapter builds the pull-down pattern because its readings match intuition: pressed is HIGH.
Read the diagram left to right. When the button is open, the left half is cut off, and the pin connects to GND through the resistor. When the button is closed, the pin connects to 5 V directly.
Building the pull-down circuit step by step
You need an Arduino Uno (or similar), a breadboard, a tactile button, a 10 kΩ resistor, an LED with its own series resistor on pin 13 (the Uno's built-in LED on pin 13 works too), and jumper wires.
- Place the button across the center gap of the breadboard.
- Run a jumper from the Arduino's 5 V pin to a row on one side of the button.
- Run a jumper from digital pin 2 to a row on the other side of the button.
- Place the 10 kΩ resistor between that same row (the one connected to pin 2) and a row you have wired to GND.
- Check that the pin 2 row holds three things: one button terminal, one resistor lead, and the jumper to pin 2.
Now upload this sketch. It lights the LED while the button is held.
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);
}
}INPUT tells the pin to measure voltage without driving it. The sketch reads the pin on every pass through loop(). The pin reads HIGH while the button is held and LOW when it is released.
Why a 10 kΩ resistor
The resistor has to do two things. It must be small enough to hold the pin firmly at GND when the button is open. It must also be large enough that pressing the button does not waste current.
When you press the button, 5 V is applied across the resistor, and Ohm's law gives the current:
I = V / R = 5 V / 10,000 Ω = 0.5 mA
That is a tiny current, so the button and the 5 V rail are not stressed. A 1 kΩ resistor would draw 5 mA, which also works but wastes more power. A 100 kΩ resistor draws very little, but it holds the pin less firmly and makes it more sensitive to electrical noise. Values from about 4.7 kΩ to 47 kΩ are all common. 10 kΩ is the usual compromise.
Notice that the pin itself draws almost no current. An input pin has a very high input resistance, so the pin only senses the voltage. It does not load the circuit. This is why the resistor's value matters for noise and power but not for the pin's safety.
What not to do
Three wiring choices cause real damage or confusing behavior.
Do not connect a button directly between 5 V and GND. With no pin and no resistor in between, pressing the button shorts the supply. On a USB-powered board this can trip the protection on your computer's port or damage the board. Always route the button through the pin node, with the resistor limiting the current.
Do not leave a button wired to a pin that is set to OUTPUT. If the pin is driving LOW and the button connects it to 5 V, the pin fights the supply. The current can exceed the chip's absolute maximum for one pin, which is 40 mA on the ATmega328P used in the Uno. Set button pins to INPUT (or INPUT_PULLUP) and keep them there.
Do not use the wrong supply voltage. The "high" side of the button must match the board's logic level. An Uno uses 5 V. Many newer boards use 3.3 V. Wiring a button to 5 V on a 3.3 V board can exceed the pin's maximum input voltage and damage it. Take the voltage from the pin your board labels 3V3 or 5V according to its logic level.
Checking the wiring before you trust the code
A short diagnostic sketch prints the raw level whenever it changes. It is useful for confirming that the button and resistor behave as the table above says.
const int buttonPin = 2;
int lastLevel = -1;
void setup() {
Serial.begin(9600);
pinMode(buttonPin, INPUT);
}
void loop() {
int level = digitalRead(buttonPin);
if (level != lastLevel) {
Serial.println(level == HIGH ? "HIGH" : "LOW");
lastLevel = level;
}
}Open the Serial Monitor at 9600 baud. You should see LOW at start-up, since the resistor holds the pin down. Press and release the button once. You may see more than one HIGH and LOW pair for a single press. That is contact bounce, and a later chapter explains and fixes it. For now, it only tells you the circuit works.
Troubleshooting common faults
| Symptom | Likely cause | Fix |
|---|---|---|
| Always `HIGH`, never changes | Button rotated so its terminals are always joined, or the resistor is not connected to GND | Rotate the button 90 degrees, then check the resistor's GND leg |
| Always `LOW`, never changes | Button never closes, or the 5 V jumper is missing | Test continuity across the button, then check the 5 V jumper |
| Readings flicker without touching the button | The pin is not connected to the resistor, so it has no defined level | Confirm the resistor and the pin share the same row |
| Board resets or the USB port disconnects when you press | Short from 5 V to GND through the button | Remove power and check that the resistor sits between the pin node and GND |
| Several events per press | Contact bounce | Debounce the input, covered in a later chapter |
The third row hints at the next topic. A pin with no resistor, or with a loose one, has no reliable level when the button is open. The following chapter looks at that failure in detail.
Exercise
A button is wired with one terminal to 5 V and the other terminal to pin 2, and a 10 kΩ resistor connects pin 2 to GND. (a) What does digitalRead(2) return while the button is released, and while it is pressed? (b) How much current flows through the resistor when the button is pressed? (c) You swap the button's connections so one terminal goes to GND and the other to pin 2, and move the resistor to 5 V. What do the readings become?
Show answer
(a) Released: LOW, because the resistor connects the pin to GND. Pressed: HIGH, because the button connects the pin to 5 V. (b) I = 5 V / 10,000 Ω = 0.5 mA. (c) The pattern is now pull-up. Released reads HIGH and pressed reads LOW, so the logic is inverted.
Exercise
You wire a four-leg tactile button across the breadboard's center gap. The sketch always reads HIGH, even when you do not touch the button. Name two wiring faults that could cause this, and one test that tells them apart.
Show answer
One fault is that the button is rotated 90 degrees, so its two terminals are always connected and the pin is permanently tied to 5 V. Another is that the resistor's GND leg is not connected, so nothing holds the pin low. Probe the button's legs with a multimeter in continuity mode while it is released. If opposite legs beep, the button is the problem. If they stay silent, check the resistor's connection to GND.