A way to use the Robot-Hat speaker without sudo, please

Hello Community, I’m working with a PiCar-X and the Robot-Hat on a Raspberry Pi. I’m facing a permissions/configuration issue with the audio:

  1. The hardware is OK: The example script 13.sound_background_music.py works perfectly when executed with sudo.

  2. The issue: When I try to play a sound as a normal user (named jmcaneda) using aplay -D plughw:2,0, there is no error in the terminal, but the speaker remains silent.

  3. Setup details:

    • User jmcaneda is already in the audio, i2c, and gpio groups.

    • aplay -l correctly shows the card as card 2: sndrpihifiberry.

    • I’ve tried chmod 666 /dev/snd/* with no success for the normal user.

    • It seems the picarx.music library or the hardware access requires root privileges to actually output sound.

Question: Is there a way to use the Robot-Hat speaker without sudo? This is critical for integrating audio into a larger Python project that doesn’t (and shouldn’t) run as root.

Thanks in advance!

I believe its an alsa routing environment issue, I.e. root env vs user env, not permissions. So, try to add these 2 lines to the end of your ~/.bashrc

export SDL_AUDIODRIVER=alsa

export AUDIODEV=hw:sndrpihifiberry

Then reboot.

Or, as a quick starting point just cut and paste the above 2 lines into your terminal before running….

python 9_howling.py # now without sudo!

.bashrc won’t work via cron, system services or autostarts as they won”t run your login. You’d need to place them somewhere appropriate to whatever your project requires, if different.

1 Like