Example Code for ESP32-Microphone Input Test

Last revision 2025/12/25

This article guides you through testing microphone input on Arduino with an ESP32 microcontroller, including hardware/software setup, wiring instructions, and example code for reading and outputting audio data.

Hardware Preparation

Software Preparation

Wiring Diagram

SEN0536-Wiring diagram

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

Other Preparation Work

Configure the microphone as receiving data of left channel by setting MODE_PIN (pin 4) to LOW.

Sample Code

ESP32-E initializes MSM261 I2S microphone (left channel, 44.1kHz), reads audio data and outputs via serial port.

#include "DFrobot_MSM261.h"

#define SAMPLE_RATE     (44100)
#define I2S_SCK_IO      (25)
#define I2S_WS_IO       (16)
#define I2S_DI_IO       (26)
#define DATA_BIT        (16)
#define MODE_PIN        (4)
DFRobot_Microphone microphone(I2S_SCK_IO, I2S_WS_IO, I2S_DI_IO);
char i2sReadrawBuff[100];
void setup() {
  Serial.begin(115200);//Serial rate 115200
  pinMode(MODE_PIN,OUTPUT);
  digitalWrite(MODE_PIN,LOW);//Configure the microphone as receiving data of left channel
  while(microphone.begin(SAMPLE_RATE, DATA_BIT) != 0){
      Serial.println(" I2S init failed");//Init failed
  }
  Serial.println("I2S init success");//Init succeeded
}

void loop() {
  microphone.read(i2sReadrawBuff,100);
  //Output data of left channel
  Serial.println((int16_t)(i2sReadrawBuff[0]|i2sReadrawBuff[1]<<8));
  delay(100);
}

Result

Blow into the microphone, and the corresponding peak & trough of the wave can be seen in the serial monitor.

SEN0526-Code result

Was this article helpful?

TOP