I am working on a pi project which uses audio out through HDMI. I am using respeaker mic for audio in. My problem is everytime when I startup the pi, the default audio is analog. I need to change it to HDMI by logging in and running

amixer -c 0 cset numid=3 2

using the terminal. I tried adding this line in a file sound.sh and when i execute the shell script using terminal it executes fine and changes to hdmi audio. My question is how to add this shell script to startup of pi, so that i dont have to run the script manually. i tried adding it to /etc/rc.local file. But doesnt seem to work. I am not sure of the exact command too. i tried:

sudo sh /home/pi/sound.sh &
sudo /home/pi/sound.sh &
/home/pi/sound.sh &
sh /home/pi/sound.sh &

Please help me out.

  • You probably need to add the full path to 'amixer' in your sound.sh script. – CoderMike Dec 3 at 8:51

Raspbian comes with systemd and old style SysV rc.local is only emulated by it. It is known that it has limitations and will be supported less step by step. The developer write in Compatibility with SysV:

In general, it's a good idea to write proper unit files with properly defined dependncies, and avoid making use of rc.local.

Here is a simple Unit file that may work. If not, tell it. Create it with:

rpi ~$ sudo systemctl edit --force set_audio.service

In the empty editor insert these statements, save them and quit the editor:

Description=Switch audio out to HDMI

ExecStart=/usr/bin/amixer cset numid=3 2


Enable the new service with:

rpi ~$ sudo systemctl enable set_audio.service

According to Audio configuration you also may have to set hdmi_drive=2 in /boot/config.txt.

Add your amixer -c 0 cset numid=3 2 a line to /etc/rc.local (just before the exit 0 line).

That gets run once at boot time.

If you add it as amixer -c 0 cset numid=3 2 > /tmp/amixer.out 2> /tmp/amixer.err you'll be able to look at those files to see any error messages that may be issued by your command.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.