MKR Zero I2S 2x Microphones

dose anyone have a sample code where i can read from 2x microphones using the i2S interface on the MKR zero, i have tried a few things and they dont seem to be working correctly.

#include <I2S.h>

int sample32[2] = {0,0};

void setup() {
  Serial.begin(250000);
  while (!Serial) {
  }

  if (!I2S.begin(I2S_PHILIPS_MODE , 8000, 32)) {
    Serial.println("Failed to initialize I2S!");
    while (1); // do nothing
  }
  //I2S.setBufferSize(1);
}

void loop() {
  // read a sample
  if(I2S.available())
  {
	sample32[0] = I2S.read();
	sample32[1] = I2S.read();
  }
  else 
  {
	  sample32[0] = 0;
	  sample32[1] = 0;
  }

  if (sample32[0] || sample32[1] ) {
	  Serial.print(sample32[0]);
	  Serial.print(" ");
	  Serial.println(sample32[1]);
  }
}

this method seems to only read from my left microphone.
i did note that some one on this form used the following line

while (I2S.read(sample32, 2) == -1)

i tried this method and found that it dose not produce any reliable data from the port.

I fixed this by using a different library to the i2s library provided by Arduino, i used d"Adafruit_Zero I2S" and modeled from the basic example…

#include <Arduino.h>
#include <Adafruit_ZeroI2S.h>

#define BUFSIZE 2048
int32_t left[BUFSIZE];
int32_t right[BUFSIZE];
int32_t leftdata = 0;
int32_t rightdata = 0;

int looptimeout = 0;

void setup()
{
  Serial.begin(115200);
  while (!Serial)  { }

  i2s.begin(I2S_32_BIT, 8000);
  i2s.enableRx();
}


void loop()
{
  if(looptimeout >= 2000)
  {  //monitor using arduino Serial Plotter
    delay(1000);
    Serial.print("left");
    Serial.print(" ");
    Serial.println("right");
    for(int j = 0; j <= 1999; j++)
    {
      Serial.print(left[j]);
      Serial.print(" ");
      Serial.println(right[j]);
    }
    looptimeout = 0;
  }

  if(i2s.rxReady())
  {
    i2s.read(&leftdata, &rightdata);
    left[looptimeout] = leftdata >> 8;    // for 24bit microphone, last 8 bits are invalid.
    right[looptimeout] = rightdata >> 8; // for 24bit microphone, last 8 bits are invalid.
    looptimeout++;
  }
}

this will gather 2000 samples and send them to the serial monitor.

Another note, the output seems to swap from side to side.. and is not consistent, this may be an issue for some,, but for me it's not a problem..

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.