Configuring ALSA Audio output on Analog and HDMI of Raspberry Pi

Here I dump sequence of commands I used to get audio working in Raspberry Pi B Model with the help of guides referred at end of post. Nothing blah.. blah.. blah..!

1. Install necessary packages

# apt-get install alsa-base alsa-oss alsa-utils alsa-tools mpg123

Here mpg123 is a simple command line MP3 player and rest of the packages are for ALSA (Linux Audio System modules and tools).

2. Loading kernel modules

The below terminal log shows how to test modules loaded. There is a chance of snd_bcm2835 missing. In that case add “snd_bcm2835” line in “/etc/modules” file.

pi@raspbian:~$ lsmod | grep snd
snd_bcm2835 22236 0
snd_soc_bcm2708_i2s 7456 0
regmap_mmio 3556 1 snd_soc_bcm2708_i2s
snd_soc_core 168033 1 snd_soc_bcm2708_i2s
snd_compress 8832 1 snd_soc_core
snd_pcm_dmaengine 5754 1 snd_soc_core
snd_pcm 92124 3 snd_bcm2835,snd_soc_core,snd_pcm_dmaengine
snd_seq 62040 0
snd_seq_device 5193 1 snd_seq
snd_timer 23443 2 snd_pcm,snd_seq
snd 68099 7 snd_bcm2835,snd_soc_core,snd_timer,snd_pcm,snd_seq,snd_seq_device,snd_compress

3. Configuration changes

Ensure that “/usr/share/alsa/alsa.conf” file has following update.

Change the line “pcm.front cards.pcm.front” as “pcm.front cards.pcm.default” in this file.
If you want to select Analog stereo jack for output, run following command with output-device-number as 1. In case of audio through HDMI, select it as 2 (0 is automatic).

# amixer cset numid=3 <output-device-number>

In case of HDMI audio, you may need to add “hdmi_drive=2” line to /boot/config.txt

4. Reboot Raspberry Pi

5. Verification

 Speaker Test to test Left speaker (you should hear smooth noise):

$ speaker-test -t sine -f 440 -c 2 -s 1

Now it is turn of Right speaker

$ speaker-test -t sine -f 440 -c 2 -s 2

Verifying .wav file playback

$ aplay /usr/share/sounds/alsa/Front_Left.wav

Verifying MP3 file playback

$ mpg123 <my_favourite_track.mp3>

6. Volume control

Set volume level through alsamixer tool by runing “alsamixer” from shell. You should see a screen similar to one below. Use up arrow and down arrow to adjust volume level and press Esc to exit.

alsamixer volume setting
alsamixer volume setting

Save the selected volume level permanently through following command as root user.

# alsactl store

One of my application for Audio in Raspberry Pi

I have setup audio in my Raspberry Pi to use it as scheduled MP3 playing and talking clock. To play MP3 “mpg123” application is used and to say time “saytime” application is used. They both are scheduled in cron entries so that they are run periodically. I created file /etc/cron.d/play_mp3 with content shown below which does the same. I have also used festival tool to talk any message given as input to make it more friendly.

54 5 * * * pi /usr/bin/mpg123 /home/pi/songs/My_prayer_song.mp3
15,30,45 6 * * * pi /usr/bin/saytime -f \%P\%l\%M
0,15,30,45 7,8 * * * pi /usr/bin/saytime -f \%P\%l\%M
0,30 9-22 * * * pi /usr/bin/saytime -f \%P\%l\%M
1 22 * * * pi echo "It's time to go to bed, Good night" | festival --tts
30 22 * * * pi echo "If you are still listening to this message, you don't take care of your health. Please take care" | festival --tts



1. Official Raspberry Pi Audio Configuration guide

2. A nice blog post to get Audio on RPi

Share this post