Buttons and Digital Input
Read pushbuttons with digitalRead, understand floating inputs, use pull-up and pull-down resistors and INPUT_PULLUP, and debounce noisy switches.
Welcome back!
Step 1 of 15Learn
The button that pressed itself
Sam wires a pushbutton between pin 2 and 5 V, writes code that lights an LED when the pin reads HIGH, and the LED flickers randomly even when nobody is touching anything. Waving a hand near the wires makes it worse.
The problem is a floating input. When the button is open, pin 2 is connected to nothing. An input pin has extremely high resistance, so it picks up stray electric fields from the air, your body and nearby wires. It reads HIGH, then LOW, then HIGH, at random.
The fix is to give the pin a default state with a resistor:
- A pull-down resistor (often 10 kΩ) from the pin to GND makes it read LOW until the button connects it to 5 V.
- A pull-up resistor from the pin to 5 V makes it read HIGH until the button connects it to GND.
The resistor is large enough that when the button is pressed, only a tiny current flows (5 V / 10 kΩ = 0.5 mA), and the button easily wins the tug of war. Without a button press, the resistor gently holds the pin at a known level.
Step 2 of 15Learn
INPUT_PULLUP: the resistor already built in
The Uno's chip has internal pull-up resistors (about 20 to 50 kΩ) on every digital pin. You switch one on with:
pinMode(2, INPUT_PULLUP);Now wire the button between pin 2 and GND: no external resistor needed. The catch is that the logic is inverted:
| Button | Pin reads |
|---|---|
| Not pressed | HIGH (pulled up) |
| Pressed | LOW (connected to GND) |
So the code checks for LOW:
void setup() {
pinMode(2, INPUT_PULLUP);
pinMode(9, OUTPUT);
}
void loop() {
if (digitalRead(2) == LOW) {
digitalWrite(9, HIGH); // pressed
} else {
digitalWrite(9, LOW);
}
}digitalRead(pin) returns HIGH or LOW. Most makers prefer INPUT_PULLUP because it saves a part and a wire, and wiring buttons to GND keeps 5 V away from loose switch wires. The Uno has no internal pull-downs, so a pull-down always needs an external resistor.
Step 3 of 15Practice · Sort into groups
Sort each setup by what the pin reads while the button is NOT pressed.
Tap an item, then tap its group (or drag it there).
Step 4 of 15Compare
Pull-up vs. pull-down wiring
| Aspect | Pull-up | Pull-down |
|---|---|---|
| Resistor goes to | 5 V (or the internal pull-up) | GND |
| Button goes to | GND | 5 V |
| Not pressed reads | HIGH | LOW |
| Pressed reads | LOW | HIGH |
| Extra parts on the Uno | None with INPUT_PULLUP | One external resistor, about 10 kΩ |
| Code reads as | Pressed when == LOW (inverted logic) | Pressed when == HIGH |
Step 5 of 15Note
Step 6 of 15Practice · Fill in the blanks
Fill in the blanks
With pinMode(2, INPUT_PULLUP) and a button wired from pin 2 to GND, digitalRead(2) returns while the button is pressed.
Step 7 of 15Learn
Bounce: one press, many clicks
Mechanical buttons don't switch cleanly. When the metal contacts meet, they bounce against each other for a few milliseconds, so the signal flickers HIGH-LOW-HIGH-LOW before settling. Your loop runs thousands of times per second, so it sees every flicker.
That doesn't matter when an LED follows the button: nobody notices a few milliseconds. It matters a lot when you count presses or toggle something. Press once and your counter may jump by 3, or your light may toggle on and immediately off again.
Debouncing means ignoring changes that happen too quickly to be real. The simplest software approach: when the reading changes, note the time with millis(), and only accept the new state if it stays the same for about 20 to 50 ms.
int reading = digitalRead(2);
if (reading != lastReading) lastChange = millis();
if (millis() - lastChange > 30 && reading != state) {
state = reading; // a real, settled change
if (state == LOW) count++; // counts each press once
}
lastReading = reading;A hardware alternative is a small capacitor across the button, but the software method costs nothing. Notice the code counts the change to LOW, not "is LOW". Counting while the pin is LOW would add hundreds per press.
Step 8 of 15Practice · Chart
What one button press looks like up close
Each point is one fast digitalRead with INPUT_PULLUP. The contacts chatter before settling LOW.
Tap the moment the signal goes LOW and stays LOW.
Tap the point that answers the question, then press Check.
| Time after the finger presses | Pin level (1 = HIGH) |
|---|---|
| 0 ms | 1 |
| 1 ms | 1 |
| 2 ms | 0 |
| 3 ms | 1 |
| 4 ms | 0 |
| 5 ms | 1 |
| 6 ms | 1 |
| 7 ms | 0 |
| 8 ms | 0 |
| 9 ms | 0 |
| 10 ms | 0 |
Step 9 of 15Note
Step 10 of 15Diagram
Edge detection with debouncing
Tap a shape with to see more.
- Read the pin
Store the raw reading in a variable.
- Different from last raw reading?
If yes, restart a timer with millis() and wait.
- Stable for 20–50 ms?
Only a reading that has not changed for the whole window is trusted.
- Stable state changed HIGH → LOW?
That transition is one real press. Act on it once.
- Save the state, loop again
Remember the stable state for the next comparison.
Step 11 of 15Practice · Choose one
A counter increases by one each time the code finds digitalRead(2) == LOW. One quick press adds about 200 to the count. What is the main problem?
Step 12 of 15Practice · True or false
Debouncing is usually unnecessary when a button simply turns an LED on while it is held down.
Step 13 of 15Note
Step 14 of 15Practice · Spot the mistake
This code should light the LED while the button (wired to GND, using the internal pull-up) is pressed. Click the line that makes it behave backward.
Step 15 of 15Practice · What would you do?
What would you do?
Elena's toggle button (press once for on, press again for off) usually works but sometimes ignores a press or flips twice. She uses edge detection but no debounce. What should she do?
Lesson complete!