Working with I2S audio input to your project can be daunting, but the CircuitPython_PIO_I2S library helps with the heavy lifting.
This guide is limited to devices supporting a compatible processor RP2xxx such as the Raspberry Pi Pico and Raspberry Pi Pico 2. Python support via Adafruit_Blinka is not supported at this time.
CircuitPython Microcontroller Wiring
Here is how you'll wire your I2S MEMS Microphone breakout (in this case, SPH0645LM4H) to a Raspberry Pi Pico (RP2040):
- Pico 3.3V to breakout 3V (red wire)
- Pico GND to breakout GND (black wire)
- Pico GP0 to breakout BCLK (yellow wire)
- Pico GP1 to breakout LRCL (purple wire)
- Pico GP2 to breakout DOUT (blue wire)
For the I2S bus clock and data signals, you can use nearly any GPIO pins available on the Pico. The only limitations are that they may not be reserved for another purpose and LRCL (word select) must be next in line sequentially from the BCLK (bit clock) (ie: BCLK = GP3 & LRCL = GP4). If this is not configured correctly, the CircuitPython_PIO_I2S library will throw an exception.
The CircuitPython_PIO_I2S library can be manually installed to your device by copying pio_i2s.py to lib folder of your CIRCUITPY drive. You will also need the Adafruit_CircuitPython_PIOASM library which can be obtained within the Adafruit CircuitPython Bundle or directly within GitHub.
The library and dependencies installation process can also be done automatically using circup with the following command:
circup install pio_i2s
For more information, refer to the documentation of CircuitPython_PIO_I2S.
Once everything is saved to the CIRCUITPY drive and you have the microphone breakout wired up, connect to the serial console to begin writing your script.
# SPDX-FileCopyrightText: Copyright (c) 2024 Cooper Dalrymple
#
# SPDX-License-Identifier: Unlicense
import board
import ulab.numpy as np
import pio_i2s
# The update rate per second which will determine the size of our audio buffer
RATE = 10
mic = pio_i2s.I2S(
bit_clock=board.GP0, # word select is GP1
data_in=board.GP2,
channel_count=1,
sample_rate=48000,
bits_per_sample=16, # ulab.numpy only works with integers up to 16-bits
samples_signed=True,
)
# Create an array which will contain one second of audio data
data = np.array([0] * int(mic.sample_rate * mic.channel_count / RATE), dtype=np.int16)
while True:
# Record one second of audio samples from the microphone into our array
mic.record(data)
# Determine the level of the incoming audio using ulab.numpy
level = (np.max(data) - np.min(data)) / 65536 # should contain a value from 0.0 to 1.0
print(level * 100) # scale to 0.0 to 100.0
Run the script within your serial console (ie: Mu or Thonny). You should see a number from 0.0 to 100.0 printed out 10 times a second representing the relative volume of the sound picked up by the microphone. Try making noises to see how that affects the volume level. You can use the Serial Plotter of the Arduino IDE to plot a line chart of the data in real-time like the following:
You can also perform FFT spectral analyzing on a set of audio samples from the microphone using the ulab.utils.spectrogram method.
# SPDX-FileCopyrightText: Copyright (c) 2024 Cooper Dalrymple
#
# SPDX-License-Identifier: Unlicense
import board
import ulab.numpy as np
import ulab.utils
import pio_i2s
FFT_SIZE = 32
mic = pio_i2s.I2S(
bit_clock=board.GP0, # word select is GP1
data_in=board.GP2,
channel_count=1,
sample_rate=48000,
bits_per_sample=16, # ulab.numpy only works with integers up to 16-bits
samples_signed=True,
buffer_size=1024, # increase this value to improve the resolution of the spectrogram
)
while True:
# Grab a single buffer (1024 samples) from the microphone and convert it to an np.ndarray object
data = np.array(mic.read(block=True), dtype=np.int16)
# Perform Fourier Fast Transform (FFT) algorithm on audio signal
data = ulab.utils.spectrogram(data)
# Interpolate down to a smaller set of data
data = np.interp(
np.arange(0, len(data), len(data) / FFT_SIZE, dtype=np.float),
np.arange(0, len(data), 1, dtype=np.uint16),
data
)
# Print out the spectrum
for i in range(len(data)):
print(data[i], end="\t")
print()
If you would like to learn more ways you can utilize your I2S Microphone Breakout in your project, check out the examples available in the CircuitPython_PIO_I2S repository.
This page ( Adafruit I2S MEMS Microphone Breakout: CircuitPython Wiring & Test) was last updated on November 27, 2024.