Using GPIO hardware buttons
Control Jukebox with buttons / GPIO
Add buttons to your jukebox to control volume, skip tracks and more.
Before we start: One of the plus points about this projects, at least in my mind, was the fact that you don't need a soldering iron to build it. Everything is USB, plug and play, thank you, boot and go.
Many, many fellow Phonieboxers have contacted me to push the envelope a bit further and add buttons to the jukebox. Buttons to change the volume and skip between tracks in a playlist.
Enough said, here we go.
Pin numbering on the RPi
On your RPi there are pin numbers printed on the board. In the following we are not referring to the board numbers, but the Broadcom (BCM) pin numbering for the GPIO pins. You can find more information on this issue on the pin numbering section of the GPIO Zero documentation site.
Any pin marked “GPIO” in the diagram above can be used as a pin number. A button attached to “GPIO17” would be specified in the script as pin number 17 rather than 11 (which would be the count on the board).
Ok, you asked for it, roll up your sleeves and read these before you start:
How to connect the buttons
In general you can choose freely which GPIOs you want to use for which (type of) button. Some best-practice GPIO usage examples are listed here, you can use these a reference / starting point.
For example, this is how to connect the buttons using the flexible pinout:
- Volume Down GPIO6 (PIN31) and GND
- Volume Up GPIO5 (PIN29) and GND
- Play/Pause/Halt (or how you call it) GPIO27 (PIN13) and GND
- Next GPIO23 (PIN16) and GND
- Previous GPIO22 (PIN15) and GND
- Shutdown (you need to hold button for 2 secs for shutdown) GPIO3 (PIN5) and GND
!!! IMPORTANT Only when using the above listed pins for wiring your shutdown pin you will be able to power-up the Raspberry PI from firmware halt. !!!
After you completed your wiring, you only have to activate the respective button(s) in the GPIO config file (
Since the sample file already contains many standard buttons, you can probably use one of the existing entries and only need to activate it by changing the parameter
False (default) to
[PlayPause] enabled: True Type: Button Pin: 27 pull_up_down: pull_up functionCall: functionCallPlayerPause
If you chose different GPIO ports you can also edit this directly.
Circuit example(s) for the button wiring
You will be using push buttons, which are essentially the same as arcade buttons, meaning: when you press them down, they are ON, when you let go, the are OFF. So skipping a track is tapping a button once, changing the volume, each tap changes it a bit. GPIO control has the option to hold down the volume buttons and change every 0.3 seconds the volume according to the configured volume steps.
There are a number of different ways to connect a button. The easiest one is well explained on O'Reilly's RPi site:
- Connecting a Push Switch, O'Reilly
- Watch the Connecting a Push Switch with Raspberry Pi video on YouTube
Use GPIO in Phoniebox
Since 2.1 we have packed all GPIO stuff into GPIO control.
Please follow the instructions there to configure your GPIO devices.
- Connect buttons to a GPIO pin (see above) and a Ground (GND) pin.
- Update the pins (use the GPIO number!) here:
~/RPi-Jukebox-RFID/settings/gpio_settings.ini. Don’t add the Ground pins to the settings file!
- Then restarting the service with
sudo systemctl restart phoniebox-gpio-controlwill activate the new settings.
You can find more examples here: https://github.com/MiczFlor/RPi-Jukebox-RFID/tree/develop/components/gpio_control/example_configs
(Optional Step): Wire hardware pullup resistor - not needed anymore
You will be running wires from the RPi board to another board where the buttons will be connected. The following image is what this looks like in a successful project.
ATTENTION this example doesn't fit exactly to the code and wiring in How to connect the buttons. On the above image you can also see the pin numbering, in this case containing both: the RPi board numbering on the RPi3 and the Broadcom (BCM) pin numbering used for the code.