Lesson 6: Digital Inputs: Buttons, Switches and DebouncingNew-age
So far your circuits have had outputs — an LED that lights, a buzzer that sounds. Now they need inputs: a way for the world, or a person, to tell the microcontroller what to do. The simplest input is a button, and it teaches a surprising amount: how a digital pin reads HIGH and LOW, why a floating pin gives random readings, and why a single press can look like many presses to a fast microcontroller. This lesson builds a button circuit, explains the pull-up and pull-down resistors that make it reliable, and solves the problem of switch bounce.
You will also learn the pattern that underlies almost every interactive project: read the input, decide what it means, and change the output. A button that toggles an LED, a switch that selects a mode, a door sensor that triggers an alarm — all of them are the same pattern, and once you can build it reliably you can build any of them.
- Digital input — a pin read as either HIGH or LOW, with no values in between.
- Pull-up resistor — a resistor that connects a pin to the supply, so it reads HIGH when nothing else is connected.
- Pull-down resistor — a resistor that connects a pin to ground, so it reads LOW when nothing else is connected.
- Floating pin — an input pin connected to nothing, which reads randomly.
- Debouncing — removing the rapid on-off flicker a mechanical switch makes as its contacts settle.
- Edge — the moment an input changes state, from LOW to HIGH (rising) or HIGH to LOW (falling).
- Polling — repeatedly checking an input’s state in the main loop.
- Toggle — switching an output to the opposite of its current state.
HIGH, LOW and the floating pin
A digital pin reads only two values: HIGH (near the supply voltage, 5 V or 3.3 V) and LOW (near 0 V). When you connect a button between a pin and ground, pressing it pulls the pin LOW — but what does the pin read when the button is not pressed? Nothing is connected, so the pin floats: it picks up electrical noise from the surroundings and reads HIGH and LOW at random. A floating pin is the commonest cause of a button that “presses itself”.
The fix is a resistor that gives the pin a definite value when the button is open. A pull-up resistor connects the pin to the supply, so it reads HIGH when idle and LOW when pressed; a pull-down resistor connects it to ground, so it reads LOW when idle and HIGH when pressed. Most microcontrollers, including the Arduino, have built-in pull-up resistors you can enable in code with INPUT_PULLUP — one line that replaces a physical resistor.
Wiring a button
The simplest reliable button circuit uses the built-in pull-up: connect one side of the button to the pin and the other side to ground, then set the pin to INPUT_PULLUP. The pin reads HIGH when the button is idle and LOW when it is pressed, so your code tests for LOW to detect a press. The logic is inverted — LOW means pressed — which confuses beginners, but it is the standard way buttons are wired, and you will meet it in every project.
Switch bounce: one press, many signals
A mechanical button does not make a clean contact. When you press it, the metal contacts bounce apart several times in a few milliseconds, so the microcontroller — which can read the pin thousands of times a second — sees a rapid flicker of HIGH and LOW before the signal settles. If your code counts every LOW as a press, one press can be counted as five or six. This is switch bounce, and every button project must deal with it.
Toggle and edge detection
Most button projects do not want to know whether the button is held down; they want to know when it was pressed. That is edge detection: remember the previous state, and act only when the state changes from HIGH to LOW. A toggle — pressing the button switches an LED on, pressing again switches it off — is built on edge detection: on the falling edge, flip the LED’s state. Without edge detection, holding the button would toggle the LED hundreds of times a second.
Worked example — a debounced toggle in Arduino C++
The code below toggles an LED on pin 13 each time a button on pin 2 is pressed, with software debouncing. The button uses the built-in pull-up, so LOW means pressed.
const int BTN = 2, LED = 13;
bool ledOn = false;
int lastState = HIGH;
unsigned long lastChange = 0;
void setup() {
pinMode(BTN, INPUT_PULLUP);
pinMode(LED, OUTPUT);
}
void loop() {
int s = digitalRead(BTN);
if (s != lastState && millis() - lastChange > 50) {
lastChange = millis();
lastState = s;
if (s == LOW) { ledOn = !ledOn; digitalWrite(LED, ledOn); }
}
}
Common mistakes
- Leaving a pin floating with no pull-up or pull-down resistor, so it reads randomly.
- Forgetting that INPUT_PULLUP inverts the logic: LOW means pressed, not HIGH.
- Counting every LOW reading as a press, so one press toggles the LED several times.
- Using delay() for debouncing, which freezes the whole program while it waits.
Discussion questions
- Why do microcontrollers provide built-in pull-up resistors but usually not pull-down ones? What does that tell you about how buttons are normally wired?
- A button must be debounced in software. Could you debounce it in hardware instead? What would that look like?
- Edge detection acts on a change, polling acts on a state. Which projects need which, and why?
Practice questions
- A pin reads randomly when a button is not pressed. What is the problem, and what fixes it? Answer: The pin is floating — connected to nothing. A pull-up or pull-down resistor gives it a definite value when the button is open. Explanation: Never leave an input floating.
- With INPUT_PULLUP, what does the pin read when the button is idle, and when it is pressed? Answer: HIGH when idle, LOW when pressed. The pull-up holds it HIGH until the button connects it to ground. Explanation: Pull-up inverts the logic.
- What is switch bounce, and why does a microcontroller see it when a person does not? Answer: The contacts bounce for a few milliseconds, flickering on and off. A person cannot feel it, but a microcontroller reading thousands of times a second sees every flicker. Explanation: Fast reading reveals the bounce.
- Explain, in one sentence, how time-based debouncing works. Answer: Accept a change only if the pin is still in the new state after about 50 ms, because a real press lasts longer than a bounce. Explanation: Time separates presses from bounces.
- Why does a toggle need edge detection? Answer: Because without it, holding the button down would toggle the output hundreds of times a second. Edge detection acts once, when the state changes. Explanation: Act on the change, not the state.
✅ Check your understanding
Quick recall check, generated from this lesson’s key terms. It is practice only and is not graded.