Raspberry Pi Wiring & Test

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.

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!

Download: file
cd ~
wget https://raw.githubusercontent.com/adafruit/Raspberry-Pi-Installer-Scripts/master/i2smic.sh
chmod +x i2smic.sh
sudo ./i2smic.sh

Run the Script

Once you run the script, you will be presented with options for configuration.

Pi Model

Pick the selection that matches you Pi Model.

Auto Load at Boot

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:

Download: file
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):

Download: file
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:

Download: file
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:

Download: file
aplay file.wav

Or, you can copy it over to your computer for playback :), make sure SSH is enabled and then:

Download: file
scp [email protected]: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:

Download: file
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:

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.

Download: file
nano ~/.asoundrc

and put the following in:

Download: file
#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:

Download: file
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:

Download: file
arecord -D dmic_sv -c2 -r 48000 -f S32_LE -t wav -V mono -v recording.wav

and playback with:

Download: file
aplay recording.wav
This guide was first published on Feb 22, 2017. It was last updated on Feb 22, 2017.
This page (Raspberry Pi Wiring & Test) was last updated on Jul 04, 2020.