Analog MEMS Microphone (VM2020) Hookup Guide

Pages
Contributors: QCPete, bboyho
Favorited Favorite 0

Arduino Example

Note: We used espressif's v2.0.6 board package for the SparkFun IoT RedBoard - ESP32. Previous versions of the board package seem to save the channels differently in the buffer. When reading the WM8960's I2S left microphone channel with v2.0.5 and this example, the Serial Plotter would only display the "right channel."

From the menu, select the following: File > Examples > SparkFun WM8960 Arduino Library > Example_15_VolumePlotter_MEMS_Mic_Differential. If you have not already, select your Board (in this case the SparkFun ESP32 IoT RedBoard), and associated COM port. Then hit the upload button.

Open the Arduino Serial Plotter and set it to 115200 baud to view the output. Make some noise by saying "Woooo!," clapping, or rubbing your fingers on the microphone. You should see an output showing the left input microphone's audio signal!

Arduino Serial Plotter Output from the Left Microphone Channel Input

Try placing the microphone next to a loud amplified speaker and adjusting the PGA as necessary for your application. Or add a second MEMS microphone to the right channel and adjusting code to include the right channel.

Note: To adjust the code to include the right channel as well, you will need to adjust the mean for both the left and right channels in the if (result == ESP_OK){} statement. You will then need to calculate and plot the values as comma separated values (CSV) for the Arduino Serial Plotter to display properly. Below is how the adjusted code should look like.

  if (result == ESP_OK)
  {
    // Read I2S data buffer
    int16_t samples_read = bytesIn / 8;
    if (samples_read > 0) {
      float meanLeft = 0;
      float meanRight = 0;
      // Only looking at left signal samples in the buffer (e.g. 0,2,4,6,8...)
      // Notice in our for loop here, we are incrementing the index by 2.
      for (int16_t i = 0; i < samples_read; i += 2) {
        meanLeft += (sBuffer[i]);
      }
      
      // Only looking at right signal samples in the buffer (e.g. 1,2,5,7,9...)
      // Notice in our for loop here, we are incrementing the index by 2.
      for (int16_t i = 1; i < samples_read; i += 2) {
        meanRight += (sBuffer[i]);
      }

      // Average the data reading
      // Calculate left input for this example. So we must divide by
      // "half of samples read" (because it is stereo I2S audio data)
      meanLeft /= (samples_read / 2); 
      
      // Calculate right input for this example. So we must divide by
      // "half of samples read" (because it is stereo I2S audio data)
      meanRight /= (samples_read / 2);

      // Print to serial plotter
      Serial.print(meanLeft);
      Serial.print(",");
      Serial.println(meanRight);
    }