Arduino Short and Long Button Presses

A single button can do more than tell the Arduino that it has been pressed: a short press and a long press can trigger different actions. We will start by reading the button, then measure how long it is held. Next, we will use the Uno’s built-in LED and a sounder to show the result. The final example takes a brief look at an interrupt-based approach.

Difficulty: Beginner

Created: February 2021

Technically reviewed: September 2026

Development board: Arduino Uno

For the first three examples, connect one side of the button to D8 on the Arduino and the other side to GND. The sketch enables the internal pull-up resistor, so an unpressed button reads HIGH and a pressed button reads LOW. Open Serial Monitor at 9600 baud to see the messages.

Each of the four sketches is a complete program. Upload only the example you want to try to the Uno.

1. Read the button

Upload the first sketch and press the button. BUTTON_PRESSED should appear once in Serial Monitor. Keeping the button down does not print it again. Release the button, then press it once more to get another message.

const byte BUTTON_PIN = 8;
const unsigned long DEBOUNCE_TIME = 30;

byte lastRawButtonState = HIGH;
byte stableButtonState = HIGH;
unsigned long rawChangedAt = 0;

void setup() {
  Serial.begin(9600);
  pinMode(BUTTON_PIN, INPUT_PULLUP);
  lastRawButtonState = digitalRead(BUTTON_PIN);
  stableButtonState = lastRawButtonState;
  rawChangedAt = millis();
}

void loop() {
  const unsigned long now = millis();
  const byte buttonState = digitalRead(BUTTON_PIN);

  if (buttonState != lastRawButtonState) {
    rawChangedAt = now;
    lastRawButtonState = buttonState;
  }

  if ((now - rawChangedAt) >= DEBOUNCE_TIME &&
      buttonState != stableButtonState) {
    stableButtonState = buttonState;
    if (stableButtonState == LOW) {
      Serial.println("BUTTON_PRESSED");
    }
  }
}

Why does the message appear only once?

digitalRead() checks the button on every pass through loop(). A mechanical button can briefly make and break contact several times as it is pressed; this is called contact bounce. The sketch therefore keeps the raw reading separate from the accepted, stable state. It accepts a change only after the reading has remained unchanged for 30 ms.

Because we use INPUT_PULLUP, the input reads HIGH at rest. Pressing the button connects D8 to GND, making the reading LOW. lastRawButtonState holds the latest immediate reading, while stableButtonState holds the accepted state. Whenever the raw reading changes, rawChangedAt records the time; the 30 ms stability check starts from there.

The message is printed when the stable state changes from HIGH to LOW: the press event. You can then hold the button for several seconds without creating another event. Meanwhile, loop() keeps running; there is no need to pause it with delay().

On release, the accepted state returns to HIGH, but nothing is printed. This leaves the sketch ready for the next press. If one press produces several messages, first check the button wiring and the D8-to-GND connection.

2. Short press or long press?

Now we will measure how long the button is held. The sketch records the time when it accepts a press and calculates the elapsed time when the button is released. A press of 400 ms or less is short; anything longer is long. Each physical press produces exactly one SHORT_PRESS or LONG_PRESS message.

const byte BUTTON_PIN = 8;
const unsigned long DEBOUNCE_TIME = 30;
const unsigned long LONG_PRESS_TIME = 400;

byte lastRawButtonState = HIGH;
byte stableButtonState = HIGH;
unsigned long rawChangedAt = 0;
unsigned long pressStartedAt = 0;
bool pressInProgress = false;

void setup() {
  Serial.begin(9600);
  pinMode(BUTTON_PIN, INPUT_PULLUP);
  lastRawButtonState = digitalRead(BUTTON_PIN);
  stableButtonState = lastRawButtonState;
  rawChangedAt = millis();
}

void loop() {
  const unsigned long now = millis();
  const byte rawButtonState = digitalRead(BUTTON_PIN);

  if (rawButtonState != lastRawButtonState) {
    rawChangedAt = now;
    lastRawButtonState = rawButtonState;
  }

  if ((now - rawChangedAt) >= DEBOUNCE_TIME &&
      rawButtonState != stableButtonState) {
    stableButtonState = rawButtonState;

    if (stableButtonState == LOW) {
      pressStartedAt = now;
      pressInProgress = true;
    } else if (pressInProgress) {
      pressInProgress = false;
      const unsigned long pressDuration = now - pressStartedAt;
      if (pressDuration <= LONG_PRESS_TIME) {
        Serial.print("SHORT_PRESS ");
      } else {
        Serial.print("LONG_PRESS ");
      }
      Serial.println(pressDuration);
    }
  }
}

How does a press become a short or long event?

pressStartedAt stores the time of the accepted press. The sketch does not decide immediately: it keeps watching until the button is released. It then subtracts the start time from the current time and chooses the short or long event from the result. The measured duration appears after the message in milliseconds. Try a quick tap, followed by a noticeably longer press.

LONG_PRESS_TIME = 400 sets the threshold. Exactly 400 ms still counts as a short press; a duration above 400 ms counts as long. Watch the printed number as you test the boundary. To use a different threshold, change this one value and upload the sketch again.

pressInProgress tells us whether an accepted press is waiting for a release. This prevents a button that is already held when the Arduino starts from being treated as a new press. Time values use unsigned long, and elapsed time is calculated by subtraction; this continues to work when the millis() counter wraps around.

Notice that the message appears only after release. Until then, the sketch cannot know whether you are about to let go or keep holding the button. Because the input is debounced, the measured interval runs between the accepted press and release states.

3. LED and sound feedback for short and long presses

So far, we have seen the result only in Serial Monitor. This time the built-in LED stays on while the button is held. After release, a short press produces one beep and a long press produces two. The sketch keeps watching the button while the sounds play, so it can respond to another press.

Connect the sounder carefully: this example uses a passive piezo sounder with a series resistor of about 200 Ω between D9 and the piezo; the diagram shows 220 Ω. The resistor limits current in the sounder circuit and helps protect the Arduino’s D9 pin from excessive current. Do not assume the same wiring is suitable for a different sounder with unknown current requirements.

Build the circuit shown below. The button goes between D8 and GND. Connect D9 through the series resistor to the passive piezo, then connect the piezo’s other lead to GND. The LED is already on the Uno board, so no separate LED is needed.

Arduino Uno circuit with a push button between D8 and GND, and a 220-ohm series resistor between D9 and a passive piezo sounder sharing GND
Button and passive piezo wiring. The D9 circuit includes a 220 Ω series resistor.
const byte BUTTON_PIN = 8;
const byte LED_PIN = LED_BUILTIN;  // Built-in LED on the classic Uno: D13.
const byte SOUNDER_PIN = 9;
const unsigned long DEBOUNCE_TIME = 30;
const unsigned long LONG_PRESS_TIME = 400;
const unsigned long BEEP_TIME = 180;
const unsigned long BEEP_GAP = 60;
const unsigned int BEEP_FREQUENCY = 4699;

byte lastRawButtonState = HIGH;
byte stableButtonState = HIGH;
unsigned long rawChangedAt = 0;
unsigned long pressStartedAt = 0;
bool pressInProgress = false;

enum SoundPhase { SOUND_IDLE, SOUND_PLAYING, SOUND_GAP };
SoundPhase soundPhase = SOUND_IDLE;
unsigned long soundPhaseStartedAt = 0;
unsigned int queuedBeeps = 0;

void updateSound(unsigned long now) {
  if (soundPhase == SOUND_PLAYING &&
      (now - soundPhaseStartedAt) >= BEEP_TIME) {
    noTone(SOUNDER_PIN);
    soundPhase = SOUND_GAP;
    soundPhaseStartedAt = now;
  }

  if (soundPhase == SOUND_GAP &&
      (now - soundPhaseStartedAt) >= BEEP_GAP) {
    soundPhase = SOUND_IDLE;
  }

  if (soundPhase == SOUND_IDLE && queuedBeeps > 0) {
    tone(SOUNDER_PIN, BEEP_FREQUENCY);
    --queuedBeeps;
    soundPhase = SOUND_PLAYING;
    soundPhaseStartedAt = now;
  }
}

void setup() {
  Serial.begin(9600);
  pinMode(BUTTON_PIN, INPUT_PULLUP);
  pinMode(LED_PIN, OUTPUT);
  pinMode(SOUNDER_PIN, OUTPUT);
  digitalWrite(LED_PIN, LOW);
  noTone(SOUNDER_PIN);
  lastRawButtonState = digitalRead(BUTTON_PIN);
  stableButtonState = lastRawButtonState;
  rawChangedAt = millis();
}

void loop() {
  const unsigned long now = millis();
  const byte rawButtonState = digitalRead(BUTTON_PIN);

  if (rawButtonState != lastRawButtonState) {
    rawChangedAt = now;
    lastRawButtonState = rawButtonState;
  }

  if ((now - rawChangedAt) >= DEBOUNCE_TIME &&
      rawButtonState != stableButtonState) {
    stableButtonState = rawButtonState;
    digitalWrite(LED_PIN, stableButtonState == LOW ? HIGH : LOW);

    if (stableButtonState == LOW) {
      pressStartedAt = now;
      pressInProgress = true;
    } else if (pressInProgress) {
      pressInProgress = false;
      const unsigned long pressDuration = now - pressStartedAt;
      if (pressDuration <= LONG_PRESS_TIME) {
        Serial.println("SHORT_PRESS");
        if (queuedBeeps < 65535U) ++queuedBeeps;
      } else {
        Serial.println("LONG_PRESS");
        if (queuedBeeps <= 65533U) queuedBeeps += 2;
      }
    }
  }

  updateSound(now);
}

How does the sketch play one or two beeps?

Press duration is measured just as it was in the second example. An accepted press turns the LED on, and an accepted release turns it off. At release, the sketch also decides whether to play one beep or two. queuedBeeps remembers how many beeps are waiting.

tone(SOUNDER_PIN, BEEP_FREQUENCY) starts a sound on D9, and noTone(SOUNDER_PIN) stops it. BEEP_TIME sets the length of a beep; BEEP_GAP sets the pause between two beeps. A short press queues one beep, while a long press queues two. LED_BUILTIN selects the Uno’s on-board LED, so there is no extra LED to wire.

updateSound() uses millis() to start and stop each sound and to leave a short gap before the next one. Because there is no delay() between beeps, loop() can keep checking the button. Try several short presses in a row: the beeps play in order, while the LED follows each press.

soundPhase tells us whether a beep is playing, the sketch is waiting between beeps, or it is ready to start the next one. Using soundPhaseStartedAt, the sketch checks on each pass whether that phase has lasted long enough. If the LED does not light while the button is held, check the D8-to-GND button connection. If the LED works but you hear nothing, inspect the D9–resistor–passive piezo–GND circuit.

4. A brief look at button interrupts

The earlier sketches read the button on every pass through loop(). An interrupt takes a different approach: a change on D3 gives the sketch a separate signal. For this example, move the button wire from D8 to D3; its other side still connects to GND. You do not need the sounder here.

const byte BUTTON_PIN = 3;
const unsigned long DEBOUNCE_TIME = 30;

volatile bool interruptPending = false;
bool pressCandidate = false;
byte lastRawButtonState = HIGH;
byte stableButtonState = HIGH;
unsigned long rawChangedAt = 0;

void onButtonFalling() {
  interruptPending = true;
}

void setup() {
  Serial.begin(9600);
  pinMode(BUTTON_PIN, INPUT_PULLUP);
  lastRawButtonState = digitalRead(BUTTON_PIN);
  stableButtonState = lastRawButtonState;
  rawChangedAt = millis();
  attachInterrupt(digitalPinToInterrupt(BUTTON_PIN), onButtonFalling, FALLING);
}

void loop() {
  noInterrupts();
  const bool newInterrupt = interruptPending;
  interruptPending = false;
  interrupts();
  if (newInterrupt) pressCandidate = true;

  const unsigned long now = millis();
  const byte rawButtonState = digitalRead(BUTTON_PIN);

  if (rawButtonState != lastRawButtonState) {
    rawChangedAt = now;
    lastRawButtonState = rawButtonState;
  }

  if ((now - rawChangedAt) >= DEBOUNCE_TIME &&
      rawButtonState != stableButtonState) {
    stableButtonState = rawButtonState;
    if (stableButtonState == LOW && pressCandidate) {
      pressCandidate = false;
      Serial.println("INTERRUPT_BUTTON_PRESSED");
    } else if (stableButtonState == HIGH) {
      pressCandidate = false;
    }
  }
}

What happens after the interrupt?

FALLING means the D3 signal has changed from HIGH to LOW. The onButtonFalling() function only sets a flag. The flag is volatile because both the interrupt handler and the main sketch use it. Serial output and debouncing still happen in loop().

When the interrupt handler finishes, the sketch resumes where it left off. The interrupt does not exit loop(), and it does not replace the stable-state check. Press the button several times: each accepted press should produce one INTERRUPT_BUTTON_PRESSED message in Serial Monitor. For measuring short and long presses, the second example remains the simpler starting point.

If you would like to learn more about interrupts, see the Arduino Interrupts tutorial.

What should you take away?

The first sketch tells us when a button is pressed. The second also measures how long it is held. The third turns the same decision into visible and audible feedback with an LED and a piezo sounder. In all three, the key is to wait for a stable change rather than repeatedly reacting to a bouncing raw signal: one press should produce one event. The interrupt example shows another way to signal a press, but interrupts are not required to tell short and long presses apart.


← Further Arduino Tutorials