Index
Just like we verified the “ears,” we must now verify the “voice.” The MAX98357A is an I²S amplifier that takes digital audio from the ESP32 and drives your speaker. Testing this now ensures you don’t spend hours wondering if your AI is silent or if your speaker is just broken.
What Is the MAX98357A?
- The MAX98357A is a mono Class-D amplifier with an integrated DAC (Digital-to-Analog Converter).
- It takes digital audio data from the ESP32’s I²S interface and outputs amplified analog sound to a spesaker.
Unlike a simple DAC, the MAX98357A can drive a speaker directly — which means no extra amplifier is needed for basic output.
What You’ll Need
| Component | Purpose |
|---|---|
| ESP32-S3 Board | Sends audio data |
| MAX98357A I²S DAC & Amp | Converts & amplifies audio |
| Speaker (4–8 Ω) | Produces sound |
| Wires & Breadboard | For connections |
| USB Cable | To upload and power the ESP32 |
The Goal
We will run a “Sine Wave Generator” sketch. This code creates a synthetic digital tone and sends it to the speaker. If you hear a steady beep or hum, your audio output hardware is working correctly.
Wiring the MAX98357A to ESP32
Connect your amplifier using the following pins. We will use a different set of pins than the microphone to avoid conflict.
| MAX98357A Pin | ESP32-S3 Pin | ESP32 | ESP32-C3 | Function |
|---|---|---|---|---|
| Vin | 5V (or 3.3V) | 5V (or 3.3V) | 5V (or 3.3V) | Power (5V is louder) |
| GND | GND | GND | GND | Ground |
| DIN | GPIO 47 | GPIO 27 | GPIO 7 | Data In (Audio Out from ESP) |
| BCLK | GPIO 48 | GPIO 26 | GPIO 6 | Bit Clock |
| LRC | GPIO 21 | GPIO 25 | GPIO 5 | Left/Right Clock (Word Select) |
Note: The MAX98357A often has a “Gain” pin. You can usually leave this floating (unconnected) for default gain, or connect it to GND via a resistor to lower the volume if it’s too loud.
The Test Code
Copy this code into your Arduino IDE. It uses the ESP32’s internal math functions to generate a sine wave and pushes it through the I²S bus.
#include <driver/i2s.h>
#include <math.h>
// --- Pin Definitions ---
#define I2S_BCK 48
#define I2S_LRC 21
#define I2S_DOUT 47
// --- Configuration ---
#define SAMPLE_RATE 44100
#define I2S_NUM I2S_NUM_0
#define WAVE_FREQ_HZ 440 // 440Hz = Musical Note 'A'
#define VOLUME 0.1 // 0.0 to 1.0 (Careful, 1.0 is VERY LOUD)
void setup() {
Serial.begin(115200);
// Configure I2S Driver
i2s_config_t i2s_config = {
.mode = (i2s_mode_t)(I2S_MODE_MASTER | I2S_MODE_TX),
.sample_rate = SAMPLE_RATE,
.bits_per_sample = I2S_BITS_PER_SAMPLE_16BIT,
.channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT, // Stereo output
.communication_format = I2S_COMM_FORMAT_STAND_I2S,
.intr_alloc_flags = ESP_INTR_FLAG_LEVEL1,
.dma_buf_count = 8,
.dma_buf_len = 64,
.use_apll = false
};
// Configure I2S Pins
i2s_pin_config_t pin_config = {
.bck_io_num = I2S_BCK,
.ws_io_num = I2S_LRC,
.data_out_num = I2S_DOUT,
.data_in_num = I2S_PIN_NO_CHANGE // Not using input here
};
// Install and Start
i2s_driver_install(I2S_NUM, &i2s_config, 0, NULL);
i2s_set_pin(I2S_NUM, &pin_config);
Serial.println("Speaker Test Started: Playing 440Hz Sine Wave");
}
void loop() {
// Generate a sine wave buffer
int16_t samples[128]; // Buffer for audio samples
static float phase = 0;
// Fill buffer with sine wave data
for (int i = 0; i < 128; i += 2) {
float output = sin(phase) * 32767 * VOLUME;
phase += 2 * PI * WAVE_FREQ_HZ / SAMPLE_RATE;
// Prevent phase overflow
if (phase >= 2 * PI) phase -= 2 * PI;
// Write same data to Left and Right channels (Stereo)
samples[i] = (int16_t)output; // Left
samples[i+1] = (int16_t)output; // Right
}
// Write buffer to I2S
size_t bytes_written;
i2s_write(I2S_NUM, samples, sizeof(samples), &bytes_written, portMAX_DELAY);
}
How to Verify
- Upload the code to your ESP32.
- Listen: You should hear a continuous, clear tone (A4 note).
- Troubleshooting:
- Hearing nothing? Double-check the Vin and GND. Ensure the DIN pin on the amp goes to GPIO 22 (Data Out).
- Hearing crackling? Check your jumper wires; loose connections are the enemy of digital audio.
- Too loud? Lower the
VOLUMEvariable in the code (e.g., to 0.05).
Expected Result
✔ You should hear a steady tone from the speaker
✔ No distortion means good power and clean wiring
✔ If you don’t hear anything, check:
Troubleshooting Tips
| Problem | What to Check |
|---|---|
| No sound | Verify wiring (BCLK, LRC, DIN) matches code |
| Distorted / noisy audio | Ensure solid ground & proper power supply |
| ESP32 resets | Ensure power supply can handle peak current |
| Very quiet | Check speaker rating & wiring quality |

