Example Code for ESP32-Microphone Recording Test

Last revision 2025/12/25

This article provides a comprehensive guide for using Arduino with an I2S MEMS Microphone to record audio, including hardware setup, wiring instructions, and sample code to save audio as a WAV file on a MicroSD card.

Hardware Preparation

Software Preparation

Wiring Diagram

SEN0536-Wiring diagram 1

Pin Connections:

I2S Microphone Pin Corresponding ESP32-E Microcontroller Pin
VCC 3V3
GND GND
SEL 4/D12
DO 26/D3
SCK 25/D2
WS 16/D11
MicroSD Module Pin Corresponding ESP32-E Microcontroller Pin
+5V VCC
GND GND
MOSI 23/MOSI
SS 2/D9
SCK 18/SCK
MISO 19/MISO

Other Preparation Work

Configure the microphone as receiving data of left channel by setting MODE_PIN (pin 4) to LOW and ensure the SD card is inserted into the MicroSD Module.

Sample Code

This code enables ESP32-E to drive the MSM261 microphone, sample 20 seconds of left-channel audio at 44.1kHz/16-bit, generate a WAV file and save it to the MicroSD card.

#include "DFrobot_MSM261.h"
#include "SPI.h"
#include <SD.h>
#include <FS.h>
#define SAMPLE_RATE     (44100) //Change sampling frequency
#define I2S_SCK_IO      (25)
#define I2S_WS_IO       (16)
#define I2S_DI_IO       (26)
#define DATA_BIT        (16) //Change bit depth
#define MODE_PIN        (4)

const int record_time = 20;  // Sampling time
const char filename[] = "/sound.wav";//The name of the saved file

const int headerSize = 44;
const int byteRate = 176400;//The number of the byte sampled per second, calculation formula: sampling rate × the number of sound channels × data bit/8 
const int waveDataSize = record_time * byteRate;//10-second sampled byte
const int numCommunicationData = 512;//The amount of data collected every time
byte header[headerSize];//The file header of the WAVE file
char communicationData[numCommunicationData];

DFRobot_Microphone microphone(I2S_SCK_IO, I2S_WS_IO, I2S_DI_IO);
File file;

void setup() {
  Serial.begin(115200);
  pinMode(MODE_PIN,OUTPUT);
  digitalWrite(MODE_PIN,LOW);//Configure the microphone as receiving data of left channel
  //digitalWrite(MODE_PIN,HIGH);//Configure the microphone as receiving data of right channel
  while(microphone.begin(SAMPLE_RATE, DATA_BIT) != 0){
      Serial.println(" I2S init failed");
  }
  Serial.println("I2S init success");
  if (!SD.begin(2)) Serial.println("SD begin failed");
    while(!SD.begin(2)){
      Serial.print(".");
      delay(500);
    }

  microphone.createWavHeader(header, waveDataSize,44100,2,byteRate,4);
  SD.remove(filename);
  //Open the file, create one if there is no file
  file = SD.open(filename, FILE_WRITE);
  if (!file) return;
  //Write the header of the wave file into the file
  file.write(header, headerSize);
  Serial.println("start");
  for (int j = 0; j < waveDataSize/numCommunicationData; ++j) {
    microphone.read(communicationData, numCommunicationData);
    for(uint32_t i = 0;i < (numCommunicationData>>2);i++){
      communicationData[(i<<2)] = 0;
      communicationData[(i<<2)+1] = 0;
      //communicationData[(i<<2)+2] = 0;
      //communicationData[(i<<2)+3] = 0;
    }
    file.write((uint8_t*)communicationData, numCommunicationData);
  }
  file.close();
  Serial.println("finish");
}

void loop() {
}

Result

Burn the codes, open the serial port, then start to record when "start" appears, and finish it when "finish" appears.

SEN0526-Code result

Check if there is a recording file in the SD card.

SEN0526-SD file

Was this article helpful?

TOP