Interface INMP441 microphone WithEsp32S3

I’ll show you how to interface the INMP441 digital microphone with the ESP32 S3 board.

Circuit Diagram

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