Core program for a Raspberry Pi button box controller that uses the gpiozero Python library. This repo is a companion to my blog post about repurposing external HDD cases into button boxes. If you have any questions or want to share your opinions, use the Discussions option. If you have issues with the program, then open an issue.
This is free and has no warranty whatsoever. Use it at your own risk. Misconfigured pins might damage your board.
- Raspberry Pi
- 40 GPIO pins version (but you can easily adapt to any revision)
- Python3
gpiozero,rpi.gpio, (and other standard Python libraries)
The button box controller was developed for the Lite version of the Raspberry Pi OS but it should work with other similar systems for single board computers (e.g., Armbian).
The following instructions assume you're logged in with the pi user with sudo permission. (This is not a requirement but if different, make sure to change file permissions accordingly. This applies to systemd service file and logrotate/button-box config as well.)
- Use
aptto install required programs
sudo apt update
sudo apt install git python3 python3-pip
- Clone the
rpi-button-boxrepo in/opt
cd /opt
sudo git clone https://github.com/cgomesu/rpi-button-box.git
sudo chown -R pi rpi-button-box
- Install Python libraries from
requirements.txt
pip3 install -r /opt/rpi-button-box/requirements.txt
- Test
button-box.py
cd /opt/rpi-button-box
./button-box.py -h
- (Optional.) Allow
logrotate(enabled by default) to manage thebutton-box.loglog files:
sudo cp /opt/rpi-button-box/logrotate.d/button-box /etc/logrotate.d/
./button-box.py -h
usage: button-box.py [-h] [--buzzer BUZZER] [--cmd {Popen,run}]
[--g1_pressed G1_PRESSED] [--g1_released G1_RELEASED]
[--s1_held S1_HELD] [--s1_released S1_RELEASED] [-i] [-d]
RPi button box controller. Repo: https://github.com/cgomesu/rpi-button-box
optional arguments:
-h, --help show this help message and exit
--buzzer BUZZER If installed, the buzzer's GPIO number.
--cmd {Popen,run} Popen: run external scripts in a NON-BLOCKING fashion.
run: run external scripts in a BLOCKING fashion.
Default=run
--g1_pressed G1_PRESSED
/path/to/script to run when G1 is pressed. The
--btn_pressed arg is available to other PUSH buttons
as well.
--g1_released G1_RELEASED
/path/to/script to run when G1 is released. The
--btn_released arg is available to other PUSH buttons
as well.
--s1_held S1_HELD /path/to/script to run when S1 is held. The
--btn_held arg is available to other SWITCHES as well.
--s1_released S1_RELEASED
/path/to/script to run when S1 is released. The
--btn_released arg is available to other SWITCHES as
well.
-i, --info Show the board information.
-d, --debug Print additional messages to the terminal.


