Page 1 of 1

Arduino IDE with PDM

Posted: Thu Sep 28, 2023 10:57 am
by siab14
I'm working on a program which is communicating via PDM with a microphone (IM72D128V01) from the ESP32-S3-WROOM-2 (Devkit-C).
I believe this is supported by the ESP32, although getting it to work via the Arduino IDE has proved difficult to me. I am getting some bits of information in the buffer every time I reset, but not a constant stream of data.

Any advice or working examples of similar would be much appreciated!

Code: Select all

#include <I2S.h>

const int buff_size = 128;
uint8_t buffer[buff_size];
const int sclkPin = 14;
const int wsPin = 3;
const int sdataPin = 16;
const int outSdPin = 9;
const int inSdPin = 10;
const int bufferSize = 128;
int readD;

//SCK (Serial Clock): is the clock signal also referred as BCLK (Bit Clock Line);
//FS (Frame Select): used to discriminate Right or Left Channel data also referred WS (Word Select);
//SD (Serial Data): the serial data to be transmitted;

//Frequency = SampleRate x BitsPerChannel x numberOfChannels
// begin(int mode, int sampleRate, int bitsPerSample)

void setup()
{
  Serial.begin(115200);
  
  I2S.setAllPins(sclkPin, wsPin, sdataPin, outSdPin, inSdPin);
  I2S.setBufferSize(bufferSize);
  I2S.setDuplex();
  
  I2S.begin(PDM_MONO_MODE, 4000, 16);
  I2S.read(); // Switch the driver in simplex mode to receive
  /* Modes:
    I2S_PHILIPS_MODE
    I2S_RIGHT_JUSTIFIED_MODE
    I2S_LEFT_JUSTIFIED_MODE
    PDM_MONO_MODE
  */
  
  I2S.setAllPins(sclkPin, wsPin, sdataPin, outSdPin, inSdPin);
  I2S.setBufferSize(bufferSize);
  I2S.setSimplex();
  I2S.read(); // Switch the driver in simplex mode to receive
}

void loop()
{
  I2S.read();
  Serial.println(I2S.available());
  if(I2S.available() < buff_size)
  {
    readD = I2S.read(buffer, I2S.available());
  }
  else
  {
    readD = I2S.read(buffer, buff_size);
  }
  Serial.println(readD);
  Serial.println("");
}