Using an I2S Microphone - SPH0645LM4H

I2S is a digital standard for transferring mono or stereo audio data. The SPH0645LM4H is an I2S MEMS microphone. It is available on a breakout board from Adafruit. One nice advantage is that unlike earlier analog MEMs microphones, no preamp and A/D input is needed. With digital signals from the microphone chip, any noise issues should also be minimized.

I2S is not supported currently by mbed APIs, but an I2S library is available for the mbed LPC1768. I2S requires a special hardware interface on the processor and it uses two clock pins (i.e., bit clock and a left/right clock) and one data pin. Any mbed platform other than the LPC1768, will require rewriting the I2S library functions for the other processor's I/O registers and I2S setup. There is a select pin that makes it easy to use two devices for a stereo microphone.

/media/uploads/4180_1/i2smic.jpg

https://www.adafruit.com/product/3421

I2S Microphone Demo

Here is a quick I2S microphone demo using interrupts and the LEDs for an audio level meter.

Wiring

The pins are lableled on the bottom of the microphone breakout board.

mbed LPC1768SPH0645LM4H
Vout3V
gndgnd
p15BLCK
p17DOUT
p16LRCL
SEL - no wire for mono

#include "mbed.h"
//I2S Demo to display values from I2S microphone
//
//Only works on mbed LPC1768 - I2S is not an mbed API!
//Microphone used is SPH0645LM4H
//see https://www.adafruit.com/product/3421

#include "I2S.h"
//uses patched mbed I2S library from 
//https://os.mbed.com/users/p07gbar/code/I2S/
//Needed a typo patch - clock was swapped p30/p15 in pin function setup code
//"if (_clk == p15)" changed to "if (_clk != p15)" in I2S.cpp line 494
BusOut mylevel(LED4,LED3,LED2,LED1);
//4 built in mbed LEDs display audio level

#define sample_freq 32000.0

//Uses I2S hardware for input
I2S i2srx(I2S_RECEIVE, p17, p16, p15);
//p17(PWM value sent as serial data bit) <-> Dout
//p16(WS) <-> LRC left/right channel clock
//p15(bit clock) <-> BCLK 

volatile int i=0;

void i2s_isr() //interrupt routine to read microphone
{
    i = i2srx.read();//read value using I2S input
    mylevel = (i>>7) & 0xF; //Display Audio Level on 4 LEDs
}
int main()
{
    i2srx.stereomono(I2S_MONO);//mono not stereo mode
    i2srx.masterslave(I2S_MASTER);//master drives clocks
    i2srx.frequency(sample_freq);//set sample freq
    i2srx.set_interrupt_fifo_level(1);//interrupt on one sample
    i2srx.attach(&i2s_isr);//set I2S ISR
    i2srx.start();//start I2S and interrupts
    while(1) {
        printf("%X\n\r",(i>>16)&(0x0000FFFF));
        wait(0.5);
    }
}

Import programi2s_microphone

I2S microphone demo for LPC1768

The value from the microphone is used to setup an audio level meter using the LPC1768's four built-in LEDs. The audio level is a bit low on the webcam recording, but the microphone is picking it up well.


Please log in to post comments.