You can add mono or stereo I2S microphones to your Raspberry Pi, too! This will work with any Raspberry with a 2x20 GPIO connector.
This guide is largely based on this great git repo, which we forked for use here. Also useful was the information provided in this forum post.
Wiring For Mono Mic
- Mic 3V to Pi 3.3V
- Mic GND to Pi GND
- Mic SEL to Pi GND (this is used for channel selection, connect to either 3.3V or GND)
- Mic BCLK to BCM 18 (pin 12)
- Mic DOUT to BCM 20 (pin 38)
- Mic LRCL to BCM 19 (pin 35)
Wiring For Stereo Mic
Connect both mics as above except for the SEL pin.
- Left Mic SEL to Pi GND
- Right Mic SEL to Pi 3.3V
Install Raspbian on an SD Card
You'll need to start with Raspbian or Raspbian Lite. Get the latest version from the Raspberry Pi Download page and follow these instructions to install the OS image to the SD card.
sudo apt-get -y update sudo apt-get -y upgrade
and then REBOOT THE PI.
sudo apt install python3-pip
Installer Script
This script will do all the work for you. Here are the commands to run. Make sure your Pi has network access, it needs to download the software!
cd ~ sudo pip3 install --upgrade adafruit-python-shell wget https://raw.githubusercontent.com/adafruit/Raspberry-Pi-Installer-Scripts/master/i2smic.py sudo python3 i2smic.py
Auto Load at Boot
The Pi model should be automatically detected.
If you want the I2S mic module support to be loaded at boot, select Yes here. Otherwise you'll have to manually install the module each time you want to use it.
Reboot
Since the script runs a system update and installs some other needed support software, it may take a while to run. Just let it go. When it's finished, you should be prompted with an option to reboot.
You need to reboot for the settings to take effect.
Test & Record!
OK, that probably took a bit of time, but now you are ready to rock!
Use the following command to list the available input devices:
arecord -l
You should see a card entry with information similar to this:
Note the card number. In the screen shot above it is 0
. You can record a wav file in mono with this command (change the -plughw
parameter to match the card number from above):
arecord -D plughw:0 -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:0 -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:
Test Playback
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 :), make sure SSH is enabled and then:
scp pi@raspberrypi:file.wav ~/Desktop/file.wav
Manual Load
If you chose not to auto load the module at boot, then you will need to manually load it with the following command:
sudo modprobe snd-i2smic-rpi rpi_platform_generation=PI_SEL
Replace PI_SEL
with the approriate value for your Pi Model:
- 0 = Pi Zero or Zero W
- 1 = Pi 2 or 3
- 2 = Pi 4
You can use the arecord -l
command to verify it loaded. Example on a Pi 3:
Rebuilding After System Update
If you later run a system update and find that the I2S mic no longer works, you may need to rebuild and reinstall the driver. To do so, simply run the install script again:
sudo ./i2smic.sh
It may complain about things, like git, already being installed. But you can generally ignore those error messages.
If you've deleted the repo directory since the original install, then just reclone it.
Adding Volume Control
You can add volume control to your mic via alsamixer and alsa config. (Hat tip to RickTracer). To do so, create and edit a file .asoundrc in your home directory.
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 sndrpii2scard 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 sndrpii2scard } 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 sound card
It will complain there are no playback controls (because its for recording only).
Press F4 to switch to Capture mode and you should be able to adjust the volume with up/down arrow keys.
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