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
- FireBeetle 2 ESP32-E IoT Microcontroller (SKU: DFR0654) × 1
- Fermion: I2S MEMS Microphone (SKU: SEN0526) × 1
- M-M/F-M/F-F Jumper wires
Software Preparation
- Download Arduino IDE: Click to download Arduino IDE.
- Install SDK: click to enter FireBeetle ESP32-E IoT Microcontroller Wiki for SDK installation tutorial.
- Download and install the DFRobot_MSM261.
- For Arduino IDE V1.8.19 (or earlier), install the library manually: How to Add a Library?
- For Arduino IDE V2.0.0 (or later), directly search for the "DFRobot_MSM261" in the Library Manager and install it.
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.

Was this article helpful?
