Join GitHub today
GitHub is home to over 36 million developers working together to host and review code, manage projects, and build software together.Sign up
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 jukebox tweakers 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:
In this example we will work with build in pullup resistors of the Raspberry Pi. Normally most people says that you should use external resistors because it is not depending on software. But in your case we have a fixed solution where nobody is playing with the cables so you don't need to build this next additional step.
The script for the GPIO buttons we are using originally been provided by Andreas aka hailogugo. It's been modified since then and now provides
pull_up=True for all pins (not needed for shutdown pin see bcm2835 Datasheet table 6-31 on page 102)
(Optional Step): Wire hardware pullup resistor
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.
How to connect the buttons
Here is how to connect the buttons using the classic pinout:
- Volume Down GPIO19 (PIN35) and GND (PIN39)
- Volume Up GPIO16 (PIN36) and GND (PIN34)
- Play/Pause/Halt (or how you call it) GPIO21 (PIN40) and GND (PIN25)
- Next GPIO26 (PIN37) and GND (PIN30)
- Previous GPIO20 (PIN38) and GND (PIN20)
- Shutdown (you need to hold button for 2 secs for shutdown) GPIO3 (PIN5) and GND (PIN6)
!!! 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. !!!
Please not that the classic pin out blocks some special GPIO functions and you may want to use the new layout.
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. The updated GPIO from pullrequest #306 added 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
Install GPIO software
We need to run GPIO Zero, a simple interface to GPIO devices with Raspberry Pi. GPIO Zero is installed by default in Raspbian Jessie. To install see the installing chapter on their site. Better safe than sorry, so lets install the packages on our machine:
$ sudo apt-get install python3-gpiozero python-gpiozero
Note: No harm done to install both, python3 and python2. This needs trimming later on.
Make a copy of the python script for the GPIO buttons into the scripts folder. This way you are free to make changes to the script without changing your github repo.
$ sudo cp /home/pi/RPi-Jukebox-RFID/misc/sampleconfigs/gpio-buttons.py.sample /home/pi/RPi-Jukebox-RFID/scripts/gpio-buttons.py
And change the copy to be executable
$ sudo chmod +x /home/pi/RPi-Jukebox-RFID/scripts/gpio-buttons.py
Automatic start on system start
To have this started automatically at boot. Please read the section on autostart the phoniebox