Remember, the I2S microphone requires an I2S peripheral and won't work with chips that don't support it in hardware! For this example we'll use a Feather M0, but you can also use an Arduino Zero.
Wiring
For Feather M0, Ardruino Zero and friends, use the following wiring:
- GND connected GND
- 3.3V connected 3.3V (Feather, Zero) or VCC (MKR1000, MKRZero)
- LRCLK / WS connected to pin 0 (Feather, Zero) or pin 3 (MKR1000, MKRZero)
- BCLK connected to pin 1 (Feather, Zero) or pin 2 (MKR1000, MKRZero)
- Data /SD connected to pin 9 (Zero) or pin A6 (MKR1000, MKRZero
You can leave Select disconnected
I2S Library
Luckily, there's a nice little I2S library already written for Arduinos based on the SAMD processor. Make sure you have the most recent Arduino IDE and SAMD core. Then select the board you're using (e.g. Adafruit Feather M0) and you'll see the I2S library examples show up in the pulldown menu
You could try the InputPlotter demo but this code is higher resolution:
/*
This example reads audio data from an I2S microphone
breakout board, and prints out the samples to the Serial console. The
Serial Plotter built into the Arduino IDE can be used to plot the audio
data (Tools -> Serial Plotter)
Circuit:
* Arduino/Genuino Zero, MKRZero or MKR1000 board
* GND connected GND
* 3.3V connected 3.3V (Zero) or VCC (MKR1000, MKRZero)
* WS connected to pin 0 (Zero) or pin 3 (MKR1000, MKRZero)
* CLK connected to pin 1 (Zero) or pin 2 (MKR1000, MKRZero)
* SD connected to pin 9 (Zero) or pin A6 (MKR1000, MKRZero)
created 17 November 2016
by Sandeep Mistry
*/
#include <I2S.h>
void setup() {
// Open serial communications and wait for port to open:
// A baud rate of 115200 is used instead of 9600 for a faster data rate
// on non-native USB ports
Serial.begin(115200);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
// start I2S at 16 kHz with 32-bits per sample
if (!I2S.begin(I2S_PHILIPS_MODE, 16000, 32)) {
Serial.println("Failed to initialize I2S!");
while (1); // do nothing
}
}
void loop() {
// read a sample
int sample = I2S.read();
if ((sample == 0) || (sample == -1) ) {
return;
}
// convert to 18 bit signed
sample >>= 14;
// if it's non-zero print value to serial
Serial.println(sample);
}
Upload to your Arduino Zero/Feather wired up as above, and open up the Serial Plotter
Try blowing or whistling at the sensor to see response in real time
VU Meter Demo
Often times you don't want the actual audio data but the overall "sound pressure level". This example will take a bunch of samples, normalize the data to be around 0, then give you the maximum difference between the waveforms for a 'volume graph'
/*
This example reads audio data from an Invensense's ICS43432 I2S microphone
breakout board, and prints out the samples to the Serial console. The
Serial Plotter built into the Arduino IDE can be used to plot the audio
data (Tools -> Serial Plotter)
Circuit:
* Arduino/Genuino Zero, MKRZero or MKR1000 board
* ICS43432:
* GND connected GND
* 3.3V connected 3.3V (Zero) or VCC (MKR1000, MKRZero)
* WS connected to pin 0 (Zero) or pin 3 (MKR1000, MKRZero)
* CLK connected to pin 1 (Zero) or pin 2 (MKR1000, MKRZero)
* SD connected to pin 9 (Zero) or pin A6 (MKR1000, MKRZero)
created 17 November 2016
by Sandeep Mistry
*/
#include <I2S.h>
void setup() {
// Open serial communications and wait for port to open:
// A baud rate of 115200 is used instead of 9600 for a faster data rate
// on non-native USB ports
Serial.begin(115200);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
// start I2S at 16 kHz with 32-bits per sample
if (!I2S.begin(I2S_PHILIPS_MODE, 16000, 32)) {
Serial.println("Failed to initialize I2S!");
while (1); // do nothing
}
}
#define SAMPLES 128 // make it a power of two for best DMA performance
void loop() {
// read a bunch of samples:
int samples[SAMPLES];
for (int i=0; i<SAMPLES; i++) {
int sample = 0;
while ((sample == 0) || (sample == -1) ) {
sample = I2S.read();
}
// convert to 18 bit signed
sample >>= 14;
samples[i] = sample;
}
// ok we hvae the samples, get the mean (avg)
float meanval = 0;
for (int i=0; i<SAMPLES; i++) {
meanval += samples[i];
}
meanval /= SAMPLES;
//Serial.print("# average: " ); Serial.println(meanval);
// subtract it from all sapmles to get a 'normalized' output
for (int i=0; i<SAMPLES; i++) {
samples[i] -= meanval;
//Serial.println(samples[i]);
}
// find the 'peak to peak' max
float maxsample, minsample;
minsample = 100000;
maxsample = -100000;
for (int i=0; i<SAMPLES; i++) {
minsample = min(minsample, samples[i]);
maxsample = max(maxsample, samples[i]);
}
Serial.println(maxsample - minsample);
}
Open up the serial plotter to see how making noises will create peaks!
ArduinoSound Library
For most uses, its better to have a higher-level library for managing sound. The ArduinoSound library works with I2S mics and can do filtering, amplitude detection, etc!
Install it using the Arduino library manager
Various examples come with the library, check them out in the File->Examples->ArduinoSound sub menu
For example, amplitude Serial plotter will do basic amplitude plotting:
You can also do FFT spectral diagramming using SpectrumSerialPlotter. We made a small change to the example so that all 128 bins are plotted:
Page last edited February 22, 2017
Text editor powered by tinymce.