Sunday, 10 January 2016

Pocket PiGRRL - adding a mute

I recently made myself a Pocket PiGRRL based on Adafruit's tutorial - it really is a great machine and a brilliant learning experience.


There was one aspect though that I didn't like, the speaker hisses, not particularly loud and when your playing a game its not that noticeable but if you have got the volume down it is really annoying. Its all down the Raspberry Pi's noisy analogue audio out so there isn't a great deal you can do to clean it up.

My solution was to add the ability to shutdown the amp, effectively muting it when I didn't want any sound.

The PAM8302A amp breakout board has a shutdown (SD) pin which when a ground (logic zero) is connected it puts the amp into idle mode, muting the amp and reducing power consumption.


I soldered a piece of wire between GPIO 26 on the bottom of the A+ and the shutdown pin on the PAM8302A ampl.

Note - a few people have noted in the comments that on other models of PiGrrl (such as the Pi Grrl 2) GPIO 26 is already used and have suggested changing this to GPIO 7.



This gave me a way of triggering the shutdown pin next I needed some software and a way of running it.

Using this tutorial as the basis I added an 'Apps' tab to emulation station where I could run 2 scripts to mute and unmute the amp.

If you want to configure your Pocket PiGRRL to do the same, you can follow the instructions below.

Download and install wiring pi
The software uses wiringpi's gpio command line utility, so download and install it.

Configure emulation station
Copy the emulation station default settings file to the pi user's emulation station configuration:
cp /etc/emulationstation/es_systems.cfg ~/.emulationstation/
Add the 'apps' tab to the settings file:
nano ~/.emulationstation/es_systems.cfg
Scroll down to the bottom and add the following before the </systemList> text:
  <system>
    <fullname>Applications</fullname>
    <name>Apps</name>
    <path>~/RetroPie/roms/apps</path>
    <extension>.sh .SH .py .PY</extension>
    <command>%ROM%</command>
    <platform>apps</platform>
    <theme>esconfig</theme>
  </system>
Save and exit using Ctrl X.

Make a directory in roms to hold the scripts:
mkdir ~/RetroPie/roms/apps
Create a script to mute the amp by setting GPIO 26 to low (0):
nano ~/RetroPie/roms/apps/mute_amp.sh

gpio -g mode 26 out
gpio -g write 26 0
And an unmute script:
nano ~/RetroPie/roms/apps/unmute_amp.sh

gpio -g mode 26 out
gpio -g write 26 1
Make the scripts executable:
chmod +x ~/RetroPie/roms/apps/mute_amp.sh
chmod +x ~/RetroPie/roms/apps/unmute_amp.sh
You can now test the scripts by running them:
~/RetroPie/roms/apps/mute_amp.sh
~/RetroPie/roms/apps/unmute_amp.sh
Reboot and the Apps tab will appear in emulation station with 2 options to mute and unmute the amp.

I wanted the amp to be muted by default, so I added the script to be run at boot by editing /etc/rc.local:
sudo nano /etc/rc.local
Scroll down and add the unmute command under '/usr/local/bin/retrogame &' but before 'exit 0':
/home/pi/RetroPie/roms/apps/mute_amp.sh &

21 comments:

  1. Hello,
    This was a very helpful guide, however there was one mistake. In the file es_systems.cfg:

    <command></command>%ROM%

    needs to be:

    <command>%ROM%</command>

    Emulation station would not add the apps tab until I made this change.

    Thanks again! =)

    ReplyDelete
    Replies
    1. Thank you. I wonder how that slipped through my extensive QA process ;)

      Delete
  2. great toutorial. thank you
    how do you switch it on / off when you booted up? can you expand your toutorial with some hints to bind the mute function to one of the tft buttons? would be great.

    ReplyDelete
    Replies
    1. Once it has booted up to emulation station, scroll across to the 'Apps' tab you created and select the mute / unmute amp links.

      Delete
  3. This comment has been removed by the author.

    ReplyDelete
  4. Whoah, there's a lot of codes there. Nevertheless, thank you for your tutorial...

    ReplyDelete
  5. This comment has been removed by the author.

    ReplyDelete
  6. Would this work for a PiGrrl2 as it looks like GPIO26 is used for the down button and can't see any other GPIO pins spare?

    ReplyDelete
  7. This is a good workaround to an annoying problem. A note for those using this technique for the Pi Grrl 2: GPIO 26 is being used by the down button on the d-pad. Looking through the GPIO pins I noticed that GPIO 7 and 8 weren't being used, so I used GPIO 7 instead of 26. Hopefully, I'm not missing anything, but it seems to work well.

    ReplyDelete
  8. when i try to run the programs after writing them, I get these errors:
    pi@retropie:~ $ ~/RetroPie/roms/apps/mute_amp.sh
    /home/pi/RetroPie/roms/apps/mute_amp.sh: line 1: gpio: command not found
    /home/pi/RetroPie/roms/apps/mute_amp.sh: line 2: gpio: command not found
    pi@retropie:~ $ ~/RetroPie/roms/apps/unmute_amp.sh
    /home/pi/RetroPie/roms/apps/unmute_amp.sh: line 1: gpio: command not found
    /home/pi/RetroPie/roms/apps/unmute_amp.sh: line 2: gpio: command not found

    ReplyDelete
    Replies
    1. You need to download and install wiringpi - see the instructions above for a link.

      Delete
  9. I want to make it so a button I press turns the mute function on and off (gpio 23 on my pigrrl 2). Is there any way I could do that?

    ReplyDelete
    Replies
    1. Do you mean a physical button? One of the buttons already on your PiGrrl 2?

      You will still need a physical connection to the shutdown pin on the amp. I suppose you could wire up a pin to the shutdown pin and then create a program which checked the state of your Pi Grrl 2 button and then switched the amp shutdown pin. Sounds do-able if you know a little python.

      Delete
  10. Hello, I really enjoyed the post. Could you please help me understand what I am doing wrong? I followed your instructions and I have all of the scripts made and ready to go. But after I run the mute or unmute script nothing happens to the amp. I think I may have wired it wrong. I tried to follow your pictures but I am at a loss. I soldered wire to gpio 7 because I have a pi 2. The wire from gpio 7 goes directly to the SD pin of the amp. I changed the script for mute and unmute to reflect that

    nano ~/RetroPie/roms/apps/mute_amp.sh

    gpio -g mode 7 out
    gpio -g write 7 0

    like I said, I think my problem is with the wiring because you mentioned having the wire somehow connect to the A+ pin and the shutdown pin. My wire just goes directly to gpio 7 to the SD pin on the amp. Any help would be great, thanks.

    ReplyDelete
    Replies
    1. The first thing I would check is that you have definitely wired it to GPIO 7 on the bottom of the Pi as you script looks fine. I found it really difficult to match up the pin as its on the bottom and clearly the position is reversed.

      You only need to connect a gpio pin to the SD pin on the amp - so providing you have got the right pin you are on the right track.

      Delete
    2. Thank you for your reply. I successfully soldered the gpio 7 to the shutdown pin and my scripts are running correctly to shutdown the amp and then to re-activate it. I like having the speaker to be muted at boot like your guide indicates. Is there any way you can help me figure out where I should put the script to unmute the speaker so that I get audio right after I launch a emulator/rom? Thank you

      Delete
    3. Super.

      I have never tried, but could you modify the script which launches the game to run the un_mute script before it launches the game and mute after the script it launched?

      It'll probably take some investigation. I would start looking at the retropie scripts, although they are likely to be pretty complicated.

      Delete
    4. Hello again, with a little bit of effort I successfully have made it so that when a ROM is launched with emulationstation it unmutes the speaker on launch. Then when you exit the game properly, the speaker is then automatically muted. To recap, Thanks to your guide I am able to have my PiGRRL 2 be muted on boot by shutting down the AMP. Then by adding some script I am now able to automatically unmute the speaker upon launch of a game and then automatically mute the speaker when exiting the game. I would not have been able to do any of this without your help/guide so once again. Thank you.
      All I did was create two files in
      /opt/retropie/configs/all/
      one of the files must be called runcommand-onstart.sh and in the file just put a line of script to direct to the ~/RetroPie/roms/apps/unmute_amp.sh file. Then make another file in that folder called runcommand-onend.sh and put in the following line of script for muting the speaker ~/RetroPie/roms/apps/mute_amp.sh
      This will then mute the speaker when your exit the ROM and return to main emulation station menu. Enjoy!

      Delete
  11. This comment has been removed by the author.

    ReplyDelete