Recently I was in need of attaching push-buttons to Raspberry-pi(In Headless server mode) for invoking certain actions/scripts and to get the feedback of those invoked actions/scripts over the LED. Rugged-Metal-Pushbutton-with-LED-Ring was a perfect match for my requirement where I could interact with Raspberry sw without having to connect an hdmi-display and a keyboard.
There are many ways to connect Push-buttons to Raspberry pi using userspace I/O libraries, but to keep the sw effort minimal, I decided to use built-in triggerhappy-daemon of raspbian-linux-distro, where standard key-codes from /dev/input/eventX can be configured to invoke scripts/commands through e.g /etc/triggerhappy/triggers.d/actions.conf file.
In short, here are few steps involved in getting Push-buttons to work with Raspberry-Pi
- Connect pushbuttons to Raspberry-Pi gpio's as shown above in the connection diagram
- Prepare Raspberry-pi-OS-Lite sdcard using raspberry-pi-imager and boot the board
- ssh to raspberry pi's shell terminal,
- Add dtoverlay config lines to /boot/config as shown above
- create push-buttons.conf file under /etc/triggerhappy/triggers.d/
- modify /lib/systemd/system/triggerhappy.service file to run the triggerhappy as pi user
- create action-scripts which were configured in push-buttons.conf
- reboot the board and press a push-button to see the configured action being invoked
- Download Raspberry-Image Application
- Open the Raspberry Pi Imager application ==> CHOOSE OS ==> Raspberry pi OS(Other) ==> Raspberry Pi OS Lite(32-bit)
- Insert the sdcard on your PC and select the sdcard by clicking "CHOOSE STORAGE" button on Raspberry pi Imager Application
- press "CTRL + Shift + X" to open the advanced options
- In Advanced options, set your hostname/enable-ssh/username/pw/local-time-settings as shown in the picture below and click SAVE button
- When done, click on the WRITE button to start creating the bootable sdcard for raspberry pi.
- Connect two push-buttons to raspberry-pi Gpio pins as shown the diagram at the top of this blog
- Insert the sdcard, connect raspberry-pi to your local-dhcp-network and power ON the 5V to Raspberry-Pi
- login to raspberry pi's shell terminal using "ssh pi@my-raspi-001" when asked, use password which you specified in advanced menu of raspberry-pi-imager
- Once you are logged into raspberry-pi shell terminal, run the following 5 commands on the ssh teminal
- 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
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