How to interface rugged metal pushbuttons with LED rings to a headless Raspberry Pi — using the built-in triggerhappy daemon to invoke scripts on button press and provide LED feedback, with no display or keyboard needed.
How it works
Instead of userspace I/O libraries, this approach uses the triggerhappy daemon (built into Raspbian) to map standard key codes from /dev/input/eventX to scripts via /etc/triggerhappy/triggers.d/actions.conf.
Overview of the steps:
- Connect pushbuttons to Raspberry Pi GPIOs as shown in the connection diagram
- Prepare a Raspberry Pi OS Lite SD card using Raspberry Pi Imager
- SSH into the Raspberry Pi
- Add
dtoverlayconfig lines to/boot/config - Create push-buttons.conf under
/etc/triggerhappy/triggers.d/ - Modify
/lib/systemd/system/triggerhappy.serviceto run as thepiuser - Create action scripts configured in push-buttons.conf
- Reboot and press a button to see the configured action invoked
Prepare the SD card
- Download the Raspberry Pi Imager
- Open the app → CHOOSE OS → Raspberry Pi OS (Other) → Raspberry Pi OS Lite (32-bit)
- Insert the SD card and click CHOOSE STORAGE
- Press
Ctrl + Shift + Xto open advanced options - Set hostname, enable SSH, username/password, and local time settings as shown below, then click SAVE
- Click WRITE to create the bootable SD card
Setup and demo
- Connect two pushbuttons to Raspberry Pi GPIO pins as shown in the connection diagram
- Insert the SD card, connect the Pi to your local DHCP network, and power on
- SSH in:
ssh pi@my-raspi-001(use the password from the Imager advanced options) - Run the following commands:
sudo apt-get install -y git esptool git clone https://github.com/hackboxguy/pi-pushbtn-demo.git cd pi-pushbtn-demo sudo ./setup.sh sudo reboot
Wait ~40 seconds for the Pi to reboot. Press one pushbutton — its LED ring blinks at ~2 Hz. Press the other button — its LED starts blinking and the first one stops.
Extending the demo
Instead of just blinking LEDs, the buttons can invoke real tasks. For example, to flash different firmware binaries to an ESP8266 (e.g. Wemos D1 Mini) on button press, replace the action scripts:
cp /home/pi/pi-pushbtn-demo/flash-esp-1hz.sh /home/pi/pi-pushbtn-demo/key-pressed-down.sh cp /home/pi/pi-pushbtn-demo/flash-esp-4hz.sh /home/pi/pi-pushbtn-demo/key-pressed-up.sh
After this change, pressing a button blinks the LED ring for ~30 seconds (indicating the Wemos D1 Mini is being flashed), then stops when flashing completes. See the 1 Hz flash script and 4 Hz flash script for details.
SOURCE CODE
github.com/hackboxguy/pi-pushbtn-demo — scripts and setup instructions