Interface INMP441 microphone With Esp32-S3

Have you ever wished that your microcontrollers could “hear” their surroundings? It might be intimidating to deal with digital audio on embedded hardware, whether you are creating a visual sound meter, clapper switch, or smart home noise detector. However, that need not be!

In this article, we will configure an ESP32 with an INMP441 I2S digital microphone to measure audio strength in real-time and activate a hardware warning when noise levels become too high. We’ll examine how I2S functions within the Arduino framework, use Direct Memory Access (DMA) to read raw audio samples, and process the peak sound levels to flash an LED when instructed. Now let’s get started!

Video Tutorial

To see how everything works together and learn how to assemble the robot step by step, you can follow the complete video tutorial on my YouTube channel.

Watch the full tutorial on my YouTube channel

Components used

  • Red LED
  • INMP441 microphone
  • 100Ω resistor
  • ESP32 S3

INMP441 Microphone Pinout

Circuit Diagram

Where to Get the Parts (Available on TEMU)

It won’t cost a fortune to build this project! The ESP32 development board, the INMP441 I2S MEMS microphone module, breadboards, LEDs, and jumper wires are all easily accessible on TEMU. TEMU is a handy one-stop shop to get all your project supplies before you start wiring because they provide incredibly cheap multi-packs for parts like the INMP441 and ESP32 boards.

Download the TEMU app first and create an account: https://temu.to/k/exyzc2cpy6m after downloading the TEMU app and creating an account, click on these links:

INMP441 microphone: https://temu.to/k/eg0id11wbwh; ESP32-S3: https://temu.to/k/e7h0zlijiog

Arduino code

#include <driver/i2s.h>
#include <Arduino.h>

#define SAMPLE_BUFFER_SIZE 512
#define SAMPLE_RATE 16000

// I2S Microphone Pins
#define I2S_BCLK_MIC 47  // gpio 47 to SCK
#define I2S_LRC_MIC 10//GPIO 10 SW
#define I2S_DIN_MIC 21 //gpio 21 to SD

// LR pin to GNS 
#define I2S_SD_SPK 4  // DAC shutdown pin. 
#define I2S_MIC_PORT I2S_NUM_0

// Threshold and LED configuration
const int soundThreshold = 2500;  // Adjust based on your environment
const int ledPin = 3;           // Change to your target LED pin if needed

i2s_config_t i2s_config = {
    .mode = (i2s_mode_t)(I2S_MODE_MASTER | I2S_MODE_RX),
    .sample_rate = SAMPLE_RATE,
    .bits_per_sample = I2S_BITS_PER_SAMPLE_16BIT,
    .channel_format = I2S_CHANNEL_FMT_ONLY_LEFT,
    .communication_format = I2S_COMM_FORMAT_I2S_MSB,
    .intr_alloc_flags = ESP_INTR_FLAG_LEVEL1,
    .dma_buf_count = 4,
    .dma_buf_len = 1024,
    .use_apll = false,
    .tx_desc_auto_clear = false,
    .fixed_mclk = 0
};

i2s_pin_config_t i2s_mic_pins = {
    .bck_io_num = I2S_BCLK_MIC,
    .ws_io_num = I2S_LRC_MIC,
    .data_out_num = I2S_PIN_NO_CHANGE,
    .data_in_num = I2S_DIN_MIC
};

int16_t raw_samples[SAMPLE_BUFFER_SIZE];

void setup() {
  Serial.begin(115200);
  pinMode(ledPin, OUTPUT);

  // Initialize I2S
  if (i2s_driver_install(I2S_MIC_PORT, &i2s_config, 0, NULL) != ESP_OK) {
    Serial.println("I2S driver installation failed!");
    while(1);
  }
  
  if (i2s_set_pin(I2S_MIC_PORT, &i2s_mic_pins) != ESP_OK) {
    Serial.println("I2S pin configuration failed!");
    while(1);
  }

  pinMode(I2S_SD_SPK, OUTPUT);
  digitalWrite(I2S_SD_SPK, LOW);  // Disable the speaker DAC

  Serial.println("ESP32 INMP441 Sound Monitor Started");
  Serial.printf("Sound threshold: %d\n", soundThreshold);
}

void loop() {
  size_t bytes_read = 0;
  
  // Read a block of audio data from the DMA buffer
  esp_err_t result = i2s_read(I2S_MIC_PORT, raw_samples, sizeof(int16_t) * SAMPLE_BUFFER_SIZE, &bytes_read, portMAX_DELAY);
  
  if (result == ESP_OK && bytes_read > 0) {
    int samples_read = bytes_read / sizeof(int16_t);
    int maxIntensity = 0;

    // Process the batch to find the peak sound intensity
    for (int i = 0; i < samples_read; i++) {
      int soundIntensity = abs(raw_samples[i]);
      if (soundIntensity > maxIntensity) {
        maxIntensity = soundIntensity;
      }
    }

    // Print the peak intensity of this block
    Serial.print("Current sound intensity: ");
    Serial.println(maxIntensity);

    // Threshold check and LED alert
    if (maxIntensity > soundThreshold) {
      digitalWrite(ledPin, HIGH);  // Turn LED on
      delay(100);                  // Keep it visible
      digitalWrite(ledPin, LOW);   // Turn LED off
      delay(100);                  // Blinking spacing
    } else {
      digitalWrite(ledPin, LOW);   // Keep off if quiet
    }

  } else if (result != ESP_OK) {
    Serial.println("Failed to read from I2S");
    delay(1000); // Wait a bit before trying again instead of freezing completely
  }
  
  delay(10); // Tiny yields to prevent watchdog resets and control spam
}

Leave a Reply