Hello
For my project I need to have a microphone for giving voice commands and detecting sounds. Because RaspberryPi board don't have a microphone input, I used for this task an external USB sound card adapter, Konig 3D Sound, based on C-Media CM108 Audio Controller. I also add a small audio amplifier and speaker for music and audio feedback.
USB sound card is connected to RaspberryPi through an USB Hub which act also as power distribution board for all components, see below image:
In order to install/enable USB sound card, let's check USB devices attached to RPi:
pi@pilot1:~ $ lsusb Bus 001 Device 005: ID 0d8c:013c C-Media Electronics, Inc. CM108 Audio Controller Bus 001 Device 004: ID 1a40:0101 Terminus Technology Inc. 4-Port HUB Bus 001 Device 003: ID 0424:ec00 Standard Microsystems Corp. SMSC9512/9514 Fast Ethernet Adapter Bus 001 Device 002: ID 0424:9514 Standard Microsystems Corp. Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
and sound modules:
pi@pilot1:~ $ cat /proc/asound/modules 0 snd_bcm2835 1 snd_usb_audio
showing that the USB soundcard is visible.
Next, I need to set snd_usb_audio (my USB soundcard) to be default sound playing device (position 0). One way to do this is to edit alsa-base.conf to load snd-usb-audio as first option. Compared to Raspbian Wheezy, in Raspbian Jessie this file no longer exists by default so I have to create it with the following content:
pi@pilot1:~ $ sudo nano /etc/modprobe.d/alsa-base.conf options snd_usb_audio index=0 options snd_bcm2835 index=1 options snd slots=snd-usb-audio,snd-bcm2835
In /usr/share/alsa/alsa.conf configuration file check to have following lines:
defaults.ctl.card 0
defaults.pcm.card 0
Make sure you have ~/.asoundrc file populated as follow:
pcm.!default { type hw card 0 } ctl.!default { type hw card 0 }
If not installed, install alsa-base, alsa-utils and mpg321 (or mpg123, mplayer, etc.) :
sudo apt-get update sudo apt-get upgrade sudo apt-get install alsa-base alsa-utils mpg321 mplayer sudo reboot
To check your configuration use command:
amixer -c 0 - to display current settings. Mine looks like this:
pi@pilot1:~ $ amixer -c 0 Simple mixer control 'Speaker',0 Capabilities: pvolume pswitch pswitch-joined Playback channels: Front Left - Front Right Limits: Playback 0 - 151 Mono: Front Left: Playback 137 [91%] [-2.69dB] [on] Front Right: Playback 137 [91%] [-2.69dB] [on] Simple mixer control 'Mic',0 Capabilities: pvolume pvolume-joined cvolume cvolume-joined pswitch pswitch-joined cswitch cswitch-joined Playback channels: Mono Capture channels: Mono Limits: Playback 0 - 127 Capture 0 - 16 Mono: Playback 26 [20%] [4.87dB] [off] Capture 0 [0%] [0.00dB] [on] Simple mixer control 'Auto Gain Control',0 Capabilities: pswitch pswitch-joined Playback channels: Mono Mono: Playback [on] pi@pilot1:~ $
and
alsamixer -c 0 - to modify speakers and microphone levels.
To check if the sound card is really working launch for example:
aplay -D plughw:0,0 /usr/share/sounds/alsa/Front_Center.wav
Ok, with playback working, let's check the recording side. Plug microphone into USB sound card input and launch:
arecord -D plughw:0,0 -f cd /home/pi/Music/test.wav
Use Ctrl+C to stop recording.
Check the result with the following command:
aplay -D plughw:0,0 /home/pi/Music/test.wav
Use alsamixer -c 0 to adjust sound levels to meet your requirements. That's it. Now both audio playback and recording are working on Raspberry Pi.
If someone know a better/cleaner way to install an external USB sound card with audio out and microphone, please share it in comments bellow.
Now is time to take all pieces and put everything in a nice box. As with most of my projects, it was a real struggle to find an enclosure to fit all components I have so far and at the same time to be suitable to be put in plain sight. So after spending more than a week on this subject, I found an enclosure from a broken wireless router which looks like the right choice, still not perfect, but will do for now:
Situation is about the same on the sensors side where the hunt for enclosures is still on. So far only one of them have an enclosure made from a kitchen timer...
Oddly enough, "antenna" is actually the temperature sensor which have to be as far as possible from ESP module to be able to show real temperature values. So far I did not found a way to shield the sensor from radio interference caused by ESP module and keep it inside the enclosure.
Top Comments