Thursday, June 22, 2023

Rugged Metal Buttons with LED Rings on Raspberry Pi

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.

Connection diagram — pushbuttons to Raspberry Pi GPIO

Connection diagram — two pushbuttons wired to Raspberry Pi GPIO pins.

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:

  1. Connect pushbuttons to Raspberry Pi GPIOs as shown in the connection diagram
  2. Prepare a Raspberry Pi OS Lite SD card using Raspberry Pi Imager
  3. SSH into the Raspberry Pi
  4. Add dtoverlay config lines to /boot/config
  5. Create push-buttons.conf under /etc/triggerhappy/triggers.d/
  6. Modify /lib/systemd/system/triggerhappy.service to run as the pi user
  7. Create action scripts configured in push-buttons.conf
  8. Reboot and press a button to see the configured action invoked

Prepare the SD card

  1. Download the Raspberry Pi Imager
  2. Open the app → CHOOSE OS → Raspberry Pi OS (Other) → Raspberry Pi OS Lite (32-bit)
  3. Insert the SD card and click CHOOSE STORAGE
  4. Press Ctrl + Shift + X to open advanced options
  5. Set hostname, enable SSH, username/password, and local time settings as shown below, then click SAVE
  6. Click WRITE to create the bootable SD card
Raspberry Pi Imager advanced options

Raspberry Pi Imager — advanced options for headless setup.

Setup and demo

  1. Connect two pushbuttons to Raspberry Pi GPIO pins as shown in the connection diagram
  2. Insert the SD card, connect the Pi to your local DHCP network, and power on
  3. SSH in: ssh pi@my-raspi-001 (use the password from the Imager advanced options)
  4. 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
Terminal showing setup commands

Running the setup commands on the Raspberry Pi.

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.

Hardware setup — pushbuttons connected to Raspberry Pi

Complete setup — rugged metal pushbuttons connected to the Raspberry Pi.

SOURCE CODE

github.com/hackboxguy/pi-pushbtn-demo — scripts and setup instructions