INMP441 with ESP32: Walkie Talkie Series | Mic Test and Speaker Test
In this tutorial, we will explore how to connect the INMP441 Microphone with the ESP32 and use it for audio data collection in a walkie-talkie style project. We will also show you how to interface the MAX98357A Speaker with the ESP32 to output audio. This blog will cover the necessary wiring, code for microphone sampling, and speaker test using AAC audio playback.
INMP441 Microphone with ESP32: Wiring and Setup
The INMP441 is a popular I2S microphone that can be easily integrated with the ESP32. For this setup, you will connect the following pins:
- VDD to 3.3V
- GND to GND
- L/R to GND (to select the left channel)
- WS (Word Select) to GPIO 15
- SCK (Serial Clock) to GPIO 14
- SD (Serial Data) to GPIO 32
This wiring setup ensures proper connection between the ESP32 and the INMP441 for audio signal reading.
INMP441 Mic Test Code
Now, let’s look at the INMP441 mic test code to sample audio from the microphone and send the data to the serial plotter
code:
#include <Arduino.h>
#include <driver/i2s.h>
// You shouldn't need to change these settings
#define SAMPLE_BUFFER_SIZE 512
#define SAMPLE_RATE 8000
// Most microphones will default to the left channel, but you may need to tie the L/R pin low
#define I2S_MIC_CHANNEL I2S_CHANNEL_FMT_ONLY_LEFT
// Pin definitions
#define I2S_MIC_SERIAL_CLOCK GPIO_NUM_14
#define I2S_MIC_LEFT_RIGHT_CLOCK GPIO_NUM_15
#define I2S_MIC_SERIAL_DATA GPIO_NUM_32
// I2S configuration
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_32BIT,
.channel_format = I2S_CHANNEL_FMT_ONLY_LEFT,
.communication_format = I2S_COMM_FORMAT_I2S,
.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};
// Pin configuration for I2S
i2s_pin_config_t i2s_mic_pins = {
.bck_io_num = I2S_MIC_SERIAL_CLOCK,
.ws_io_num = I2S_MIC_LEFT_RIGHT_CLOCK,
.data_out_num = I2S_PIN_NO_CHANGE,
.data_in_num = I2S_MIC_SERIAL_DATA};
void setup()
{
Serial.begin(115200); // Serial output for the plotter
i2s_driver_install(I2S_NUM_0, &i2s_config, 0, NULL); // Initialize I2S driver
i2s_set_pin(I2S_NUM_0, &i2s_mic_pins); // Set pin configuration
}
int32_t raw_samples[SAMPLE_BUFFER_SIZE]; // Buffer for audio data
void loop()
{
size_t bytes_read = 0;
i2s_read(I2S_NUM_0, raw_samples, sizeof(int32_t) * SAMPLE_BUFFER_SIZE, &bytes_read, portMAX_DELAY); // Read audio data
int samples_read = bytes_read / sizeof(int32_t);
// Send samples to serial plotter
for (int i = 0; i < samples_read; i++) {
Serial.println(raw_samples[i]);
}
}
RESULTS: When you wistle the wave form on serial plotter should be something like this
Speaker Test with MAX98357A
Next, let’s look at how to test the MAX98357A speaker with the ESP32. We'll play an AAC file using AudioGeneratorAAC and AudioOutputI2S libraries.
MAX98357A Speaker Pinout:
- Bit Clock (BCLK): GPIO 27
- Word Select (WS): GPIO 26
- Serial Data (SD): GPIO 25
Speaker Test Code:
#include "AudioGeneratorAAC.h"
#include "AudioOutputI2S.h"
#include "AudioFileSourcePROGMEM.h"
#include "sampleaac.h"
#define Bit_Clock_BCLK 27
#define Word_Select_WS 26
#define Serial_Data_SD 25
#define GAIN 0.125
AudioFileSourcePROGMEM *in;
AudioGeneratorAAC *aac;
AudioOutputI2S *out;
void setup(){
Serial.begin(115200);
in = new AudioFileSourcePROGMEM(sampleaac, sizeof(sampleaac)); // Load AAC file from PROGMEM
aac = new AudioGeneratorAAC();
out = new AudioOutputI2S();
out->SetGain(GAIN); // Set speaker gain
out->SetPinout(Bit_Clock_BCLK, Word_Select_WS, Serial_Data_SD); // Configure speaker pins
aac->begin(in, out); // Start AAC playback
}
void loop(){
aac->loop(); // Continuous playback
delay(10);
}
This will output the AAC file to the MAX98357A speaker. You should hear sound coming from the speaker if everything is set up correctly.
Post a Comment