How to wire the
MAX98357A I2S amp
with ESP32
What You Need
Parts
- ESP32 Dev Board (38-pin) or ESP32-C3
- MAX98357A I2S amplifier module
- 4-8Ω speaker (1W or above)
- Jumper wires
- USB-C cable for flashing
Software
No audio tools needed for this step - the tone test generates a 440Hz sine wave in code.
How It Works
The MAX98357A is a digital I2S amplifier. It receives audio as a digital signal directly from the ESP32's I2S peripheral, converts it to analogue internally, and drives a speaker - all in one chip. No external DAC, no coupling capacitors, no decoupling capacitors. Just three signal wires and power.
I2S (Inter-IC Sound) carries three signals: a bit clock (BCLK), a word select line (LRCLK) that indicates left or right channel, and a serial data line (DIN). The ESP32 generates all three in hardware. The tone test sketch feeds a 440Hz sine wave directly into the I2S peripheral - no audio file, no conversion, just maths.
Wiring
| MAX98357A Pin | ESP32 Dev Board | ESP32-C3 | Notes |
|---|---|---|---|
| VIN | 5V | 5V | 5V recommended - 3.3V works at lower volume |
| GND | GND | GND | |
| LRC | GPIO14 | GPIO5 | I2S word select (LRCLK) |
| BCLK | GPIO27 | GPIO4 | I2S bit clock |
| DIN | GPIO13 | GPIO6 | I2S data out |
| SD | Not connected | Not connected | Leave floating - amp enabled, mono mix of L+R |
| GAIN | Not connected | Not connected | Leave floating - 9dB gain |
Connect your speaker to the two output terminals on the MAX98357A module. Polarity only affects phase - either orientation will produce sound. Use a speaker rated 1W or above.
Tone Test
Flash this sketch. It generates a continuous 440Hz sine wave via I2S - no audio file needed. If you hear a tone, wiring and I2S are confirmed working and you are ready for Step 2.
/*
* We stand on the shoulders of giants when we build
* with knowledge gained from others' efforts.
* That doesn't make us giants. Be humble.
* Create with care. Open source is the way.
*
* MAX98357A I2S - Tone Test
* --------------------------
* Generates a 440Hz sine wave via I2S to confirm
* wiring and amp are working. No audio file needed.
*
* Board: ESP32 Dev Board (38-pin) or ESP32-C3
* Amp: MAX98357A
*
* Wiring (ESP32 Dev Board):
* VIN -> 5V GND -> GND
* BCLK -> GPIO27 LRC -> GPIO14
* DIN -> GPIO13 SD -> not connected
*
* Wiring (ESP32-C3):
* VIN -> 5V GND -> GND
* BCLK -> GPIO4 LRC -> GPIO5
* DIN -> GPIO6 SD -> not connected
*
* Open source - MIT Licence
* Electronic Zoology - field notes from the garage
* https://electroniczoology.com/guides/how-to-wire-max98357a-i2s-amp-esp32
*/
#include <driver/i2s.h>
#include <math.h>
// ESP32 Dev Board (38-pin)
#define I2S_BCLK 27
#define I2S_LRCLK 14
#define I2S_DOUT 13
// ESP32-C3 - comment out the three lines above and uncomment these:
//#define I2S_BCLK 4
//#define I2S_LRCLK 5
//#define I2S_DOUT 6
#define SAMPLE_RATE 44100
#define FREQUENCY 440
#define AMPLITUDE 20000
#define BUF_SIZE 256
int16_t buf[BUF_SIZE];
void setup() {
i2s_config_t cfg = {
.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,
.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,
.tx_desc_auto_clear = true,
};
i2s_pin_config_t pins = {
.bck_io_num = I2S_BCLK,
.ws_io_num = I2S_LRCLK,
.data_out_num = I2S_DOUT,
.data_in_num = I2S_PIN_NO_CHANGE,
};
i2s_driver_install(I2S_NUM_0, &cfg, 0, NULL);
i2s_set_pin(I2S_NUM_0, &pins);
}
void loop() {
static uint32_t phase = 0;
size_t written;
for (int i = 0; i < BUF_SIZE; i += 2) {
int16_t sample = (int16_t)(AMPLITUDE * sinf(2.0f * M_PI * FREQUENCY * phase / SAMPLE_RATE));
buf[i] = sample;
buf[i + 1] = sample;
phase++;
if (phase >= SAMPLE_RATE) phase = 0;
}
i2s_write(I2S_NUM_0, buf, sizeof(buf), &written, portMAX_DELAY);
}