Thursday, June 22, 2023

Interfacing Rugged-Metal-push-button with Raspberry-Pi




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

  1. Connect pushbuttons to Raspberry-Pi gpio's as shown above in the connection diagram
  2. Prepare Raspberry-pi-OS-Lite sdcard using raspberry-pi-imager and boot the board
  3. ssh to raspberry pi's shell terminal,
  4. Add dtoverlay config lines to /boot/config as shown above
  5. create push-buttons.conf file under /etc/triggerhappy/triggers.d/
  6. modify /lib/systemd/system/triggerhappy.service file to run the triggerhappy as pi user
  7. create action-scripts which were configured in push-buttons.conf
  8. reboot the board and press a push-button to see the configured action being invoked
For beginners and  DIY enthusiast, here are the exact steps explaining a working demo setup where two push-buttons are used for switch ON/OFF of built-in ACT LED on Raspberry-Pi board.

Prepare the SDCard Image
  1. Download Raspberry-Image Application
  2. Open the Raspberry Pi Imager application ==> CHOOSE OS ==> Raspberry pi OS(Other) ==> Raspberry Pi OS Lite(32-bit)
  3. Insert the sdcard on your PC and select the sdcard by clicking "CHOOSE STORAGE" button on Raspberry pi Imager Application
  4. press "CTRL + Shift + X" to open the advanced options
  5. In Advanced options, set your hostname/enable-ssh/username/pw/local-time-settings as shown in the picture below and click SAVE button
  6. When done, click on the WRITE button to start creating the bootable sdcard for raspberry pi.

Next Steps:
  1. Connect two push-buttons to raspberry-pi Gpio pins as shown the diagram at the top of this blog
  2. Insert the sdcard, connect raspberry-pi to your local-dhcp-network and power ON the 5V to Raspberry-Pi
  3. 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
  4. 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


Wait for the raspberry pi to reboot(~40seconds) and press one of the push-buttons, and notice LED ring on this push button would start blinking at around 2Hz, Pressing the other push-button would start blinking its LED and at the same time LED ring on other push-button would stop blinking.


Instead of just blinking the LED's, this demo can be extended to do some real tasks upon button press. Just by overwriting two files(as shown below), button press can invoke flashing of different firmware binaries on esp8266-board(e.g wemos-d1-mini).  Just run the following two commands to replace blinking action to esp8266 firmware flashing:
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
with this change, After pushing the button, you will notice that the led-ring around the push-button starts blinking for about 30seconds(to indicate that wemos-d1-mini is being flashed) and then the blinking stops and results into different blink program being flashed to wemos-d1-mini.

Take a look at this and this script to see what exactly gets invoked upon button press. Sources/Scripts used in this demo are available here at github-repo