You can add mono or stereo I2S microphones to your Raspberry Pi, too!
This will work with Raspberry Pi B+, 2, 3, Zero and any other 2x20-connector-Pi
This guide is largely based on this great git repo https://github.com/nejohnson2/rpi-i2s
Wiring For Mono Mic
- Mic 3V - Pi 3.3v
- Mic Gnd - Pi Gnd
- Mic SEL - Pi Gnd (this is used for channel selection. Connect to 3.3 or GND)
- Mic BCLK - BCM 18 (pin 12)
- Mic LRCL - BCM 19 (pin 35)
- Mic DOUT - BCM 20 (pin 38)
Raspberry Pi i2s Configuration
Start by logging into your Raspberry Pi via a terminal, we recommend ssh so you can copy + paste the many commands.
Turn on i2s support by editing /boot/config.txt with:
sudo nano /boot/config.txt
Uncomment #dtparam=i2s=on
Next, we'll make sure sound support is enabled in the kernel with:
sudo nano /etc/modules
Add snd-bcm2835
on its own line, to the modules file as shown below
Now reboot your pi with:
sudo reboot
Once rebooted, re-log in.
Enter the following to confirm the modules are loaded
lsmod | grep snd
Kernel Compiling
Ok now its time for the fun part! You'll manually compile in i2s support.
Start by updating your Pi:
sudo apt-get update sudo apt-get install rpi-update sudo rpi-update
Then reboot!
Install the compilation dependencies:
sudo apt-get install git bc libncurses5-dev bison flex libssl-dev
Download kernel source & compile:
sudo wget https://raw.githubusercontent.com/notro/rpi-source/master/rpi-source -O /usr/bin/rpi-source sudo chmod +x /usr/bin/rpi-source /usr/bin/rpi-source -q --tag-update rpi-source --skip-gcc
On a Pi 3 this will take many many minutes, so don't worry if its taking 15 minutes. On a Pi Zero it can take an hour or longer!
If the script pauses at this prompt:
Code coverage for fuzzing (KCOV) [N/y/?] (NEW)
Just press enter to accept the default and continue.
sudo mount -t debugfs debugs /sys/kernel/debug
This may already be done - mount: debugs is already mounted - in which case keep going
If you are using Pi 3 or Pi 2 - make sure the module name is 3f203000.i2s
If you are using Pi Zero - the module name is 20203000.i2s
Run rpi-i2s-audio
Download the module, written by Paul Creaser
git clone https://github.com/PaulCreaser/rpi-i2s-audio cd rpi-i2s-audio
Pi Zero Only
If you are using a Raspberry Pi Zero, edit my_loader.c with nano my_loader.c and change the two lines
.platform = "3f203000.i2s",
and
.name = "3f203000.i2s",
with
.platform = "20203000.i2s",
and
.name = "20203000.i2s",
If you aren't using a Pi Zero, continue on!
Compile the module with
make -C /lib/modules/$(uname -r )/build M=$(pwd) modules sudo insmod my_loader.ko
Verify that the module was loaded:
lsmod | grep my_loader dmesg | tail
Note that on the Pi 2/3 you'll see asoc-simple-card asoc-simple-card.0: snd-soc-dummy-dai <-> 3F203000.i2s mapping ok
on the last line and on Pi Zero you'll see asoc-simple-card asoc-simple-card.0: snd-soc-dummy-dai <-> 20203000.i2s mapping ok
Auto-load the module on startup
Now you can set it up so the module is loaded every time you boot the Pi
sudo cp my_loader.ko /lib/modules/$(uname -r) echo 'my_loader' | sudo tee --append /etc/modules > /dev/null sudo depmod -a sudo modprobe my_loader
And reboot!
sudo reboot
Test & Record!
OK that was a lot of effort but now you are ready to rock!
Use the following command to list the available input devices:
arecord -l
you should see a snd_rpi_simple_card
You can record a wav file in mono with this command:
arecord -D plughw:1 -c1 -r 48000 -f S32_LE -t wav -V mono -v file.wav
Or, if you have two i2s mics installed, record in stereo with this command:
arecord -D plughw:1 -c2 -r 48000 -f S32_LE -t wav -V stereo -v file_stereo.wav
If all is working correctly, you should see the VU meter react at the bottom of the terminal window
If you have speakers hooked up to the Pi, you can play the file back directly on the device:
aplay file.wav
Or, you can copy it over to your computer for playback :), just insert your Pi's IP address below:
scp pi@<local-ip>:/home/pi/file.wav ~/Desktop/file.wav
Adding Volume control
You can add volume control to your mine via alsamixer and alsa config. (Hat tip to RickTracer)
Run sudo nano ~/.asoundrc
and put the following in:
#This section makes a reference to your I2S hardware, adjust the card name # to what is shown in arecord -l after card x: before the name in [] #You may have to adjust channel count also but stick with default first pcm.dmic_hw { type hw card sndrpisimplecar channels 2 format S32_LE } #This is the software volume control, it links to the hardware above and after # saving the .asoundrc file you can type alsamixer, press F6 to select # your I2S mic then F4 to set the recording volume and arrow up and down # to adjust the volume # After adjusting the volume - go for 50 percent at first, you can do # something like # arecord -D dmic_sv -c2 -r 48000 -f S32_LE -t wav -V mono -v myfile.wav pcm.dmic_sv { type softvol slave.pcm dmic_hw control { name "Boost Capture Volume" card sndrpisimplecar } min_dB -3.0 max_dB 30.0 }
Now before you can change the volume you need to use the device once (this is an alsa thing)
Run
arecord -D dmic_sv -c2 -r 44100 -f S32_LE -t wav -V mono -v file.wav
And cancel with ^C once it starts recording.
Now you can run alsamixer - press F6 and select the I2S simple sound card
It will complain there are no playback controls (because its for recording only).
Press F5 to change the volume.
Then you can record with the i2c mic device using
arecord -D dmic_sv -c2 -r 48000 -f S32_LE -t wav -V mono -v recording.wav
and playback with
aplay recording.wav