How to control volume in RetroPie with buttons
Mods move this if needed
OVERVIEW
Due to numerous requests, I am writing up a quick tutorial for setting up RetroPie to use 2 buttons connected to the GPIO on a raspberry pi for volume control. This should work with all models of raspberry pi, but I've only done it on a Pi 3 and Pi Zero.
This mod is awesome for an arcade setup, as you can control the volume without having to mount your speaker/amp control knob/wheel on the outside of your machine, just add 2 buttons in the place of your choosing.
Before doing this or any software mod I really recommend you backup your SD card. That way if your setup goes awry you can restore it to the condition it was before you made any changes. Easiest way to backup is to create an image file with win32diskimager, or whatever imaging software you used, and save it on your computer's hard drive. If you do not backup your card and things get messed up, you may have to start all over again. I cannot implore you enough to do this before making any changes.
HARDWARE INSTALLATION
Let's hook up your buttons! Make sure your buttons are momentary push buttons, and not the kind that stay on until you push them again.
Here is a good page to visit with information about the raspberry pi's GPIO. Reference the diagram on that page for hooking up your wires.
Hooking them up is easy, take one lead (wire) from each button and hook it to a ground pin on the GPIO. You can hook them to the same ground pin or 2 separate ground pins, doesn't matter as long as 1 wire from each button is grounded.
Next is to hook the other lead (wire) from each button to an unused pin on the GPIO. It can be any pin, but I recommend using any of the green labeled ones shown on the page linked above, not that others won't work, but the green ones are generally not tied to anything else the pi uses, and shouldn't conflict with anything else in your setup.
Now your setup should have 2 buttons with 4 wires hooked into the GPIO. (3 wires if you joined the grounds together) One lead of each button should be on it's own GPIO pin and 1 lead of each button hooked to a ground pin.
SOFTWARE INSTALLATION
Now we are going to power up the Pi.
Connect a keyboard to the Pi, or SSH into it using putty or a similar program.
Once it has booted into emulation station push F4 to exit to command line. If you are SSH'ing it's probably a good idea to quit emulation station from the start menu so it drops you into the command line. Don't worry we will safely reboot it from the SSH terminal later.
Now the fun part. We are going to install GPIOnext. This program is awesome, you can emulate a joystick/keyboard as well as use it to issue commands, the latter is what we are going to use. GPIOnext is not mine and all credit for it goes to the developer.
In the terminal type:
cd ~
then press enter, type:
git clone https://github.com/mholgatem/GPIOnext.git
then press enter, once it drops you back to the command line, type:
bash GPIOnext/install.sh
then press enter
If it installed correctly it will ask if you want to run the configuration wizard, go ahead and press y then enter.
^(Alternatively you can type) ^(gpionext config) ^(from the command line if you chose not to start the config right after setup.)
You should be looking at the GPIOnext menu. There is a lot of awesome options here, but we only care about commands right now. Select that option and press enter.
Chose the option to make a new command.
This is where it will ask you for a name for your command. This name has no effect on what the command will do and is only for your reference. Go ahead and call it what you want , but calling it "volume up" might be a good idea to reference it if you want to make changes later. Press enter on your keyboard once you've typed in the name of the command.
Now it will ask you to hold down the button for the command. Hold down the new volume up button you installed. It should register on screen after a second or 2. Once it shows up on screen, let go of the button.
The dialog will now ask for the command you want to input. Here is the command I used, type it in and then press enter.
/usr/bin/amixer -q -c 0 sset PCM 1dB+ unmet no cap
It will ask if you want another command added press N for no and then push enter.
Now it should automatically save it and take you back to the main menu of GPIOnext. Select the command option again, and this time make a new command named "volume down" or something similar to reference later.
When it asks for you to hold down the button, you will hold down the button you want to be the volume down now. After a second or two it'll register and you can release the button.
This time type the following in the command prompt then press enter:
/usr/bin/amixer -q -c 0 sset PCM 1dB- unmet no cap
Say no to adding another command again.
Once it boots you back to the main menu you can now select the exit option.
Back on the terminal command prompt type:
gpionext start
then press enter.
The daemon will start right away, but there won't be anything in the prompt confirming this. You will, however, get an error if something isn't right. Check the developer of GPIOnext's page linked above if you have any errors.
If you wish you can restart now with the command:
sudo reboot
^(Alternatively you can type) ^(emulationstation) ^(at the command prompt to start emulation station again, but I recommend a reboot right now, as this will verify that GPIOnext is loaded at boot.)
Once your Pi has restarted go ahead and launch an emulator and try your new buttons. You will have to press the button quite a few times to volume it up or down, as it only changes the volume by 1 decibel for each button press, and holding the button down will not make it adjust more than 1 decibel in volume, you have to keep pushing and releasing it.
That's it! You now have push button volume control that works across all emulators system wide, even if they are not run by RetroArch (libretro cores).
ENJOY!!!
TWEAKING THE SETUP:
Now, if you want the volume to adjust with less button pushes, you can change that by going back into the GPIOnext configuration and editing the commands you made. Press F4 to exit emulation station, and once at the command line type:
gpionext config
This will launch you back to GPIOnext's config screen again, where you can edit, delete, or add new things.
If you want the volume to jump more than 1 decibel at a time you can change the last part of the command we used earlier.
So changing this:
/usr/bin/amixer -q -c 0 sset PCM 1dB+ unmet no cap
to this:
/usr/bin/amixer -q -c 0 sset PCM 5dB+ unmet no cap
will make it volume up quicker, but you will lose the finer control that the original code has.
Just replace the number right before dB+ (or dB- for volume down) and it will change how far it moves the volume for each button press. Experiment by changing that number to find the sweet spot for your setup.
OPTIONAL
If you are playing N64 games and notice you do not have volume control, there is something we can do to make the volume buttons work with it too. Follow this guide here to switch the audio plugin that the N64 emulator uses, then your buttons will work in there too.
COMMON PROBLEMS
If you are using a display that is hooked up to your GPIO pins, there is a chance that GPIOnext will not install. This is because the display is hogging the GPIO and won't let python install. My only workaround was to use this for my display, as it doesn't hog the GPIO's processes like the standard screen drivers do. It may not work for your display, but it does support quite a few of them.
If you are using a USB sound card or other audio device on your pi, the command will have to be altered to target your sound source.
Here is a good guide to help you figure out where your sound card is. Best way to figure out your card is to launch a game and SSH into the pi while the emulator is running with the sound going, that way when you experiment with the commands in that guide you will hear if the volume changes.
I have my pi zero using a USB sound card so my code changed from this:
/usr/bin/amixer -q -c 0 sset PCM 1dB+ unmet no cap
to this:
/usr/bin/amixer -q -c 1 sset Speaker 1dB+ unmet no cap
Obviously my volume down changed the same way with 1dB- on it's line instead of 1dB+
This example is just what my USB sound card needed, use the guide listed above to find out what your card may need changing.
Posted Mar 6, 2019, 7:15 AM UTC
Awesome, thanks!