A single button can do more than one job if you can tell a quick tap from a held-down press. This tutorial shows how to detect short press and long press events on a Raspberry Pi, using nothing but a button and GPIO timing. It's a handy pattern when you want to save pins: one press switches modes, a long press triggers something more drastic like a reset.
The full tutorial, with every detail and the line-by-line code explanation, is on our site: Raspberry Pi - Button - Long Press Short Press
What you need
- Raspberry Pi 5
- Breadboard-mount Button with Cap
- Breadboard-mount Button Kit
- Panel-mount Push Button
- Push Button Module
- Breadboard
- Jumper Wires
- Screw Terminal Block Shield for Raspberry Pi
- Raspberry Pi Prototyping Base Plate & Breadboard Kit
- DIYables Sensor Kit (18 sensors/displays)
- DIYables USB Type-A to Type-B Cable
Wiring
Wire the button to a GPIO pin using the Pi's internal pull-up resistor, so you don't need an external resistor. With the pull-up enabled, the pin reads HIGH when the button is untouched and drops to LOW the moment it's pressed. You can build this on a breadboard, wire it directly without one, or use a Screw Terminal Block Shield on the Pi for sturdier connections.
How it works
A short press is detected by timing the gap between the press and release events: if that duration is under a set threshold (like 500 milliseconds), it counts as short. Long press detection works one of two ways. In the first, you wait until the button is released and then check if the held duration passed your threshold. In the second, you check the duration continuously while the button is still down, so the long-press event fires immediately once the threshold is crossed, even before release.
Code
The scripts use the RPi.GPIO library to read the button state and time to measure durations between press and release. A constant like SHORT_PRESS_TIME sets the cutoff for what counts as a short press. Separate example scripts show short-press-only detection, long-press-on-release, long-press-while-held, and a combined script that distinguishes both in the same loop.
raspberry-pi-button-long-press-short-press.py
"""
This Raspberry Pi code was developed by newbiely.com
This Raspberry Pi code is made available for public use without any restriction
For comprehensive instructions and wiring diagrams, please visit:
https://newbiely.com/tutorials/raspberry-pi/raspberry-pi-button-long-press-short-press
"""
import RPi.GPIO as GPIO
import time
BUTTON_PIN = 16
SHORT_PRESS_TIME = 0.5 # 500 milliseconds
DEBOUNCE_TIME = 0.1 # 100 milliseconds
# Variables will change:
prev_button_state = GPIO.LOW # the previous state from the input pin
button_state = None # the current reading from the input pin
press_time_start = 0
press_time_end = 0
# Set up GPIO and configure the pull-up resistor
GPIO.setmode(GPIO.BCM)
GPIO.setup(BUTTON_PIN, GPIO.IN, pull_up_down=GPIO.PUD_UP)
try:
while True:
# Read the state of the switch/button
button_state = GPIO.input(BUTTON_PIN)
# Perform debounce by waiting for DEBOUNCE_TIME
time.sleep(DEBOUNCE_TIME)
if prev_button_state == GPIO.HIGH and button_state == GPIO.LOW: # Button is pressed
press_time_start = time.time()
elif prev_button_state == GPIO.LOW and button_state == GPIO.HIGH: # Button is released
press_time_end = time.time()
press_duration = press_time_end - press_time_start
if press_duration < SHORT_PRESS_TIME:
print("A short press is detected")
# Save the last state
prev_button_state = button_state
except KeyboardInterrupt:
print("\nExiting the program.")
GPIO.cleanup()
Full tutorial
The complete tutorial - wiring diagram, code explanation and troubleshooting - is here: https://newbiely.com/tutorials/raspberry-pi/raspberry-pi-button-long-press-short-press

Top comments (0)