How-to · Beginner · 3 min read
Push Buttons with Arduino
Pull-Ups, Debouncing and Long Press
By Geekobot Learning Team · Updated 24 Oct 2026
On this page
Wire a push button between a digital pin and ground, set the pin to INPUT_PULLUP, and the pin reads LOW while pressed. Mechanical contacts bounce for a few milliseconds, so ignore changes for about 20 to 50 ms after each transition, or use a debounce library. For long-press detection, record when the button went down and check how long it has been held.
Key specs at a glance
- Tactile switch
- 4 pins; pins on the same side are connected in pairs
- Wiring
- Pin to button to GND, with INPUT_PULLUP
- Bounce time
- Typically 1 to 20 ms
- Debounce delay
- 20 to 50 ms in code, or a 10 kΩ + 100 nF RC filter
- Panel switches
- Momentary (NO/NC) or latching; rated voltage and current printed on the body
- Libraries
- Bounce2, OneButton, EasyButton
A shorted, over-charged or damaged lithium cell can get very hot and catch fire. Use a protection board or BMS, never short the terminals, charge on a non-flammable surface and do not leave a charging pack alone. Throw away cells that are swollen or dented.
Wiring a tactile button (no external resistor)
A 4-pin tactile switch has two pairs of legs: legs on the same side are already connected, so wire across the gap. Connect one side to the input pin and the other to GND. With pinMode(pin, INPUT_PULLUP) the internal pull-up holds the pin HIGH until the button connects it to ground. See pull-up and pull-down resistors for why this matters.
Why buttons bounce and how to debounce
When the contacts close, they rattle for a few milliseconds, producing several HIGH/LOW edges. Code that counts edges sees one press as two or five. Debounce by accepting a new state only after it has been stable for a short time:
const int BTN = 2;
const int LED = 13;
const unsigned long DEBOUNCE = 30;
int stable = HIGH, lastRead = HIGH;
unsigned long lastChange = 0;
bool ledOn = false;
void setup() {
pinMode(BTN, INPUT_PULLUP);
pinMode(LED, OUTPUT);
}
void loop() {
int r = digitalRead(BTN);
if (r != lastRead) { lastChange = millis(); lastRead = r; }
if (millis() - lastChange > DEBOUNCE && r != stable) {
stable = r;
if (stable == LOW) { // a clean press
ledOn = !ledOn;
digitalWrite(LED, ledOn);
}
}
}Detecting long press and double press
Store the time when the button goes down. If it is still down after 800 ms, treat it as a long press (and remember so you do not also fire a short press on release). For double presses, note the time of each release and compare with the previous one. The OneButton library handles click, double-click and long-press callbacks and is worth using for menus.
Hardware debouncing and interrupts
A 10 kΩ pull-up with a 100 nF capacitor across the button smooths the bounce so even an interrupt pin sees one edge; a Schmitt-trigger buffer (74HC14) makes it bullet-proof. Use interrupts (attachInterrupt) only when the loop is too busy to poll; for most projects polling every loop pass is simpler and avoids bounce-triggered interrupt storms.
Choosing a button for the job
| Switch | Use |
|---|---|
| 6 x 6 mm or 12 x 12 mm tactile | Breadboards, PCBs, menus |
| Panel-mount momentary (12 to 22 mm) | Enclosures, user-facing buttons; often with LED ring |
| Latching push button | On/off power without code |
| Mushroom emergency stop (NC) | Machines and robots: cuts power when pressed, wired in series with the supply |
| Micro / limit switch | End-stops, lid detection |
| Capacitive touch (TTP223) | Sealed panels, no moving parts |
- 6 x 6 mm or 12 x 12 mm tactile
- Breadboards, PCBs, menus
- Panel-mount momentary (12 to 22 mm)
- Enclosures, user-facing buttons; often with LED ring
- Latching push button
- On/off power without code
- Mushroom emergency stop (NC)
- Machines and robots: cuts power when pressed, wired in series with the supply
- Micro / limit switch
- End-stops, lid detection
- Capacitive touch (TTP223)
- Sealed panels, no moving parts
Try it: RC time constant calculator
Change the values and the diagram and results update as you type.
Common mistakes to avoid
- Wiring the button to 5 V with INPUT_PULLUP, so it never changes.
- Using delay() for debouncing inside a loop that also drives motors or displays.
- Treating a bounced edge in an interrupt as multiple presses.
- Choosing a tactile switch for a panel and having it break off.
- Using a normally-open button for an emergency stop; it must be normally-closed.
Frequently asked questions
Do I need a resistor for a push button on Arduino?
No. Use pinMode(pin, INPUT_PULLUP) and wire the button to ground. An external 10 kΩ pull-up or pull-down works too.
Why does my button register multiple presses?
Contact bounce. Ignore changes for 20 to 50 ms after each transition in code, or add an RC filter.
How do I detect a long press?
Record millis() when the button goes down. If it stays down past your threshold (for example 800 ms), fire the long-press action and flag it so the release does not count as a short press.
Which pins of a tactile switch are connected?
Legs on the same side of the switch body are connected to each other. Wire across the body (opposite sides) so pressing closes the circuit.
Can I use a push button with an ESP32 or Pico?
Yes. Both have internal pull-ups and pull-downs. On the ESP32, avoid GPIO 34 to 39 for buttons unless you add an external resistor, since they have no internal pull-ups.
Parts for this guide
Calculators
Terms in this guide
Projects that use it
Put this into practice: 133 step-by-step builds in our project library use parts from this guide.
Keep learning
Sources
- Arduino Debounce tutorial (Arduino)
- Bounce2 and OneButton library documentation
- Omron B3F tactile switch datasheet
Written by the Geekobot team from manufacturer datasheets. Always check the datasheet for the exact part and variant you buy.




