JustYorick
JustYorick

Reputation: 1

ESP32 DFRobot I2S MEMS Microphone output

I have a DOIT ESP32-DEVKIT and a DFRobot I2S MEMS Microphone. When the microphone and the ESP32 are connected the Serial Plotter should have an output something along the lines of the image below: Expected microphone output according to official docs However, when I connect the microphone to my ESP32 the only output I see is fluctuating 0's and -1's. Current microphone output It (very sporadically) spikes to something like 2000 or -2000.

The microphone does not respond to any audio input at all.

Below is the current connection between the microphone and ESP32 Current pin setup

As the documentation describes using a different type of ESP32, I've tried connect the pins to similar pins on my ESP32. I've also bought a second (same) microphone, but when using this one it has the same result.

The code I've used (which is basically the same code as used in the documentation but with different pins defined).

DFrobot_MSM261.h is the library the docs provide, which I've installed.

I'm not the most savvy person when it comes to embedded systems so I'm afraid I've missed something quite obvious.

At this point I'm afraid both my microphones just fried or they're just not compatible with my ESP32.


#define SAMPLE_RATE     (44100)
#define I2S_SCK_IO      (18)
#define I2S_WS_IO       (25)
#define I2S_DI_IO       (32)
#define DATA_BIT        (32)
#define MODE_PIN        (19)
DFRobot_Microphone microphone(I2S_SCK_IO, I2S_WS_IO, I2S_DI_IO);
char i2sReadrawBuff[100];
void setup() {
  Serial.begin(115200);
  pinMode(MODE_PIN,OUTPUT);
  digitalWrite(MODE_PIN,LOW); // set LEFT channel
  // digitalWrite(MODE_PIN,HIGH); // set RIGHT channel
  while(microphone.begin(SAMPLE_RATE, DATA_BIT) != 0){
      Serial.println(" I2S init failed");
  }
  Serial.println("I2S init success");
}

void loop() {
  microphone.read(i2sReadrawBuff,100);
  Serial.println((int16_t)(i2sReadrawBuff[2]|i2sReadrawBuff[3]<<8)); // read LEFT channel
  // Serial.println((int16_t)(i2sReadrawBuff[0]|i2sReadrawBuff[1]<<8)); // read RIGHT channel
  delay(100);
}

Upvotes: 0

Views: 224

Answers (1)

MartinR
MartinR

Reputation: 11

I have never used that library, but I have used a mems microphone before. In my case, I simply used the i2s library, so I would recommend you to at least try it with it.

The code I usually used to test the mic was the following one, you'll need to adjust the pins to your project:

/** 
 * ESP32 I2S Serial Plotter Example.
 */
 #include <driver/i2s.h>
 
 const i2s_port_t I2S_PORT = I2S_NUM_0;
 const int BLOCK_SIZE = 1024;
 
 void setup() {
 Serial.begin(115200);
   Serial.println("Configuring I2S...");
   esp_err_t err;

   // The I2S config as per the example
   const i2s_config_t i2s_config = {
       .mode = i2s_mode_t(I2S_MODE_MASTER | I2S_MODE_RX), // Receive, not transfer
       .sample_rate = 16000,                         // 16KHz
       .bits_per_sample = I2S_BITS_PER_SAMPLE_32BIT, // could only get it to work with 32bits
       .channel_format = I2S_CHANNEL_FMT_ONLY_LEFT, // although the SEL config should be left, it seems to transmit on right
       .communication_format = i2s_comm_format_t(I2S_COMM_FORMAT_I2S | I2S_COMM_FORMAT_I2S_MSB),
       .intr_alloc_flags = ESP_INTR_FLAG_LEVEL1,     // Interrupt level 1
       .dma_buf_count = 4,                           // number of buffers
       .dma_buf_len = 8                              // 8 samples per buffer (minimum)
   };
 
   // The pin config as per the setup
   const i2s_pin_config_t pin_config = {
       .bck_io_num = 26,   // BCKL
       .ws_io_num = 33,    // LRCL
       .data_out_num = -1, // not used (only for speakers)
       .data_in_num = 32   // DOUT
   };

   // Configuring the I2S driver and pins.
   // This function must be called before any I2S driver read/write operations.
   err = i2s_driver_install(I2S_PORT, &i2s_config, 0, NULL);
   if (err != ESP_OK) {
     Serial.printf("Failed installing driver: %d\n", err);
     while (true);
   }
   err = i2s_set_pin(I2S_PORT, &pin_config);
   if (err != ESP_OK) {
     Serial.printf("Failed setting pin: %d\n", err);
     while (true);
   }
    Serial.println("I2S driver installed.");
 }
 
 void loop() {
   // Read multiple samples at once and calculate the sound pressure
   int32_t samples[BLOCK_SIZE];
   int num_bytes_read = i2s_read_bytes(I2S_PORT, 
                                  (char *)samples, 
                                  BLOCK_SIZE,     // the doc says bytes, but its elements.
                                  portMAX_DELAY); // no timeout

   int samples_read = num_bytes_read / 8;
   if (samples_read > 0) {

     float mean = 0;
     for (int i = 0; i < samples_read; ++i) {
       mean += (samples[i] >> 14);
     }
     mean /= samples_read;

     float maxsample = -1e8, minsample = 1e8;
     for (int i = 0; i < samples_read; ++i) {
       minsample = min(minsample, samples[i] - mean);
       maxsample = max(maxsample, samples[i] - mean);
     }
     Serial.println(maxsample - minsample);
   }
 }

 // actually we would need to call `i2s_driver_uninstall(I2S_PORT)` upon exit. 

Upvotes: 0

Related Questions