MAX98357A ESP32: Play MP3 and Web Radio with an I2S Amplifier
How to use the MAX98357A I2S amp with an ESP32: BCLK, LRC, DIN pins, GAIN and channel setup, 3 W speaker, tone and MP3 code, web radio, noise fixes, Pico notes.
The MAX98357A and ESP32 pair is one of the easiest ways to add clean, loud audio to microcontroller projects. The MAX98357A is a single chip that receives digital audio over an I2S bus, turns it into sound and drives the speaker directly with a class D amplifier. You don't need a separate DAC, a separate amplifier or audio cables: with three signal wires from the ESP32 plus power, you get more than 3 W from a 4 Ω speaker at 5 V.
The ESP32's own 8-bit DAC pins can also make sound, but it's thin and hissy, and the speaker still needs an amplifier. The DFPlayer Mini decodes MP3 by itself, but it can't play audio generated by the ESP32, web radio or speech synthesis. The MAX98357A fills the gap between the two. In this guide you'll see the I2S pins, the GAIN and SD pin settings, speaker and power calculations, ESP32 code that runs on core 2.0.x, MP3 from microSD and web radio, fixes for hiss and noise, and how to use I2S on the Raspberry Pi Pico.
What is the MAX98357A and how does it work?
I2S is a three-wire serial bus designed to carry digital audio. BCLK (bit clock) says when each data bit should be read, LRC (left/right clock, also called WS) tells whether the left or right channel is being sent, and DIN carries the audio samples. The LRC frequency equals the sample rate; BCLK is sample rate × bits × channels. For 44.1 kHz, 16-bit stereo audio, BCLK = 44,100 × 16 × 2 = 1.4112 MHz. The MAX98357A takes this data, derives its internal clock from BCLK (it needs no separate MCLK) and feeds a filterless class D output stage that drives the speaker with high-frequency PWM. Because a class D amp runs its transistors fully on or fully off, it's very efficient and the chip barely gets warm.
| Feature | Value |
|---|---|
| Chip | MAX98357A (Maxim, now Analog Devices) |
| Supply | 2.5–5.5 V |
| Output power (10% THD) | 3.2 W into 4 Ω, 1.8 W into 8 Ω (at 5 V) |
| Input | I2S (also TDM) |
| Sample rate | 8–96 kHz |
| Data width | 16, 24 or 32 bits; BCLK at 32, 48 or 64 times the sample rate |
| Gain | 3, 6, 9, 12 or 15 dB (GAIN pin) |
| Channel | Left, right or (left + right) / 2 (SD pin) |
| Speaker | 4–8 Ω, bridge-tied (BTL) output |
| Extra clock | No MCLK needed |
MAX98357A pins and ESP32 wiring
| MAX98357A pin | Function | ESP32 | Raspberry Pi Pico |
|---|---|---|---|
| LRC | Left/right channel clock (WS) | GPIO 26 | GP17 |
| BCLK | Bit clock | GPIO 27 | GP16 |
| DIN | Audio data (from the microcontroller to the amp) | GPIO 25 | GP18 |
| GAIN | Gain selection | Unconnected (9 dB) | Unconnected (9 dB) |
| SD | Shutdown and channel select | GPIO 33 (optional) or unconnected | Unconnected |
| GND | Ground | GND | GND |
| VIN | Supply 2.5–5.5 V | 5V (VIN) | VBUS (5 V) |
| + / − | Speaker output | Straight to the speaker | Straight to the speaker |
On the ESP32, thanks to its internal routing matrix, I2S signals can be sent to almost any output pin; pins 25, 26 and 27 in the table are just a common choice and match the ESP32 internet radio project. Avoid the input-only GPIO 34–39 and GPIO 0, 2, 12 and 15, which affect booting. To look up board pins quickly, use the pinout tool.
The GAIN pin: setting the gain
| GAIN connection | Gain | When? |
|---|---|---|
| To GND through a 100 kΩ resistor | 15 dB | Quiet source and a large speaker |
| Directly to GND | 12 dB | When you need a bit more volume |
| Unconnected | 9 dB | Default; the right choice for most projects |
| Directly to VIN | 6 dB | Small speaker and desktop listening |
| To VIN through a 100 kΩ resistor | 3 dB | Very quiet use, close to headphone level |
Every 6 dB increase roughly doubles the amplitude. But more gain doesn't always mean better sound: if the digital audio is already at full scale, high gain clips the signal and the sound becomes muffled and crackly. You get the cleanest result by leaving GAIN at the default 9 dB and setting the volume in software. If you think it's not loud enough, first put the speaker in a box; a speaker without an enclosure loses almost all its bass and sounds quieter than it is.
The SD pin: shutdown and channel select
| Voltage on the SD pin | Mode |
|---|---|
| Below 0.16 V | Shutdown; the amp goes silent and current drops to the microamp range |
| 0.16–0.77 V | Average of the left and right channels: (left + right) / 2 |
| 0.77–1.4 V | Right channel only |
| Above 1.4 V | Left channel only |
On most common modules the SD pin is pulled up to VIN by an on-board resistor, and together with the resistor inside the chip the voltage lands in the 0.16–0.77 V range; that's why the module plays a mix of both channels if you leave the pin unconnected. For stereo you use two modules: tie one module's SD pin directly to VIN (left), and add a suitable resistor to the other that brings its SD voltage between 0.77 and 1.4 V (right). The value of that resistor depends on your board's resistor and the supply voltage, so measure the SD pin voltage with a multimeter after adding it. If you connect the SD pin to a GPIO and drive it LOW, you silence the amp completely; it's the easiest way to avoid the "pop" at power-up and power-down and to save current when running on batteries.
Speaker choice, output power and current table
| Supply | Speaker | Maximum power (approx.) | Peak current (approx.) |
|---|---|---|---|
| 5 V | 4 Ω | 3.2 W | 0.75 A |
| 5 V | 8 Ω | 1.8 W | 0.4 A |
| 3.3 V | 4 Ω | 1.4 W | 0.5 A |
| 3.3 V | 8 Ω | 0.8 W | 0.3 A |
The 5 V values are the datasheet's 10% distortion (THD) point, where the sound clearly starts to clip. For clean sound, plan for roughly a fifth less power. The 3.3 V rows are approximate values calculated from the 5 V figures, because power scales with the square of the voltage: 3.2 W × (3.3 / 5)² ≈ 1.4 W. Peak current is the output power divided by the amp's efficiency and then by the supply voltage; you can try your own numbers with the Ohm's law calculator. The speaker's power rating should be at least what the amp delivers: for a 4 Ω speaker, choose 3 W or more.
ESP32 I2S code: the first sound (a sine tone)
The first sketch uses no extra library: with the driver/i2s.h driver that comes with ESP32 Arduino core 2.0.x, it sets up a 44.1 kHz, 16-bit stereo I2S bus, calculates a sine wave and plays the notes C, E, G and high C in turn. The code was compiled with core 2.0.11; since the I2S library changed in core 3.x, you may see warnings or build differences with this driver there. The AMP_SD pin keeps the amp off at power-up and switches it on once I2S is ready, so you don't hear a click at start-up.
1// ESP32 + MAX98357A: I2S ile sinüs tonu (çekirdek 2.0.x, driver/i2s.h) / sine tone over I2S (core 2.0.x)
2#include <driver/i2s.h>
3#include <math.h>
4
5#define I2S_BCLK 27 // MAX98357A BCLK
6#define I2S_LRC 26 // MAX98357A LRC (WS)
7#define I2S_DIN 25 // MAX98357A DIN (ESP32 veri çıkışı / ESP32 data out)
8#define AMP_SD 33 // MAX98357A SD: LOW = kapalı / LOW = shutdown
9
10const int SAMPLE_RATE = 44100; // örnekleme hızı (Hz) / sample rate (Hz)
11const float VOLUME = 0.25; // 0.0 - 1.0 dijital ses / digital volume
12const int FRAMES = 256; // her yazmada çerçeve / frames per write
13
14int16_t buffer[FRAMES * 2]; // sol + sağ / left + right
15float phase = 0;
16
17void i2sSetup() {
18 i2s_config_t cfg = {};
19 cfg.mode = (i2s_mode_t)(I2S_MODE_MASTER | I2S_MODE_TX);
20 cfg.sample_rate = SAMPLE_RATE;
21 cfg.bits_per_sample = I2S_BITS_PER_SAMPLE_16BIT;
22 cfg.channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT; // stereo çerçeve / stereo frame
23 cfg.communication_format = I2S_COMM_FORMAT_STAND_I2S;
24 cfg.intr_alloc_flags = 0;
25 cfg.dma_buf_count = 8;
26 cfg.dma_buf_len = 256;
27 cfg.use_apll = false;
28 cfg.tx_desc_auto_clear = true; // veri bitince sessizlik / silence on underrun
29
30 i2s_pin_config_t pins = {};
31 pins.mck_io_num = I2S_PIN_NO_CHANGE; // MAX98357A MCLK istemez / no MCLK needed
32 pins.bck_io_num = I2S_BCLK;
33 pins.ws_io_num = I2S_LRC;
34 pins.data_out_num = I2S_DIN;
35 pins.data_in_num = I2S_PIN_NO_CHANGE;
36
37 i2s_driver_install(I2S_NUM_0, &cfg, 0, NULL);
38 i2s_set_pin(I2S_NUM_0, &pins);
39 i2s_zero_dma_buffer(I2S_NUM_0);
40}
41
42void playTone(float freq, int ms) {
43 int total = (long)SAMPLE_RATE * ms / 1000;
44 float step = 2.0f * PI * freq / SAMPLE_RATE;
45 while (total > 0) {
46 int n = total < FRAMES ? total : FRAMES;
47 for (int i = 0; i < n; i++) {
48 int16_t s = freq > 0 ? (int16_t)(sinf(phase) * 32767.0f * VOLUME) : 0;
49 buffer[2 * i] = s; // sol / left
50 buffer[2 * i + 1] = s; // sağ / right
51 phase += step;
52 if (phase > 2.0f * PI) phase -= 2.0f * PI;
53 }
54 size_t written;
55 i2s_write(I2S_NUM_0, buffer, n * 4, &written, portMAX_DELAY); // 4 bayt/çerçeve / 4 bytes per frame
56 total -= n;
57 }
58}
59
60void setup() {
61 pinMode(AMP_SD, OUTPUT);
62 digitalWrite(AMP_SD, LOW); // açılışta sessiz / muted at boot
63 i2sSetup();
64 delay(50);
65 digitalWrite(AMP_SD, HIGH); // amfiyi aç / enable the amp
66}
67
68void loop() {
69 const float notes[] = {523.25, 659.25, 783.99, 1046.5}; // Do, Mi, Sol, ince Do / C5, E5, G5, C6
70 for (float f : notes) playTone(f, 250);
71 playTone(0, 1000); // 1 sn sessizlik / 1 s of silence
72}| Code part | What it does |
|---|---|
| I2S_MODE_MASTER | I2S_MODE_TX | The ESP32 generates the clocks and only sends data |
| sample_rate = 44100 | 44,100 samples per second; this is also the LRC frequency |
| I2S_CHANNEL_FMT_RIGHT_LEFT | Each frame carries a left and a right sample; the MAX98357A plays one of them or their average depending on SD |
| dma_buf_count / dma_buf_len | A buffer of 8 × 256 samples; audio streams in the background without gaps |
| tx_desc_auto_clear = true | The buffer is zeroed when data runs out, so old audio doesn't repeat |
| i2s_write(..., portMAX_DELAY) | Waits until there's room in the buffer, then writes the data |
| VOLUME = 0.25 | Digital volume; 1.0 is full scale and close to clipping |
Playing MP3 from a microSD card with ESP32-audioI2S

To decode MP3 we'll use the ESP32-audioI2S library (schreibfaul1). Its current releases (3.x and up) require ESP32 core 3.x, and the newest ones also require PSRAM; on core 2.0.x and a classic ESP32 board without PSRAM, install version 2.0.0. Copy your MP3 files to a FAT32-formatted microSD card named 001.mp3, 002.mp3 and 003.mp3. Connect the SD module to the ESP32's VSPI bus: SCK GPIO 18, MISO GPIO 19, MOSI GPIO 23, CS GPIO 5. For module wiring details, see the SD card module article. A button on GPIO 32 skips to the next track; when a track ends, the code plays the next one automatically.
1// ESP32 + MAX98357A + microSD: MP3 çalar (ESP32-audioI2S 2.0.0, çekirdek 2.0.x) / MP3 player
2#include <SD.h>
3#include <SPI.h>
4#include "Audio.h" // ESP32-audioI2S (schreibfaul1)
5
6#define I2S_BCLK 27
7#define I2S_LRC 26
8#define I2S_DIN 25
9#define SD_CS 5 // SD modülü: SCK 18, MISO 19, MOSI 23, CS 5 / SD module pins
10#define BTN_NEXT 32 // sonraki şarkı, diğer ucu GND / next track, other leg to GND
11
12Audio audio;
13const char *tracks[] = {"/001.mp3", "/002.mp3", "/003.mp3"}; // kartın kökündeki dosyalar / files in the card root
14const int TRACK_COUNT = sizeof(tracks) / sizeof(tracks[0]);
15int track = 0;
16
17void playTrack(int i) {
18 Serial.printf("Caliyor / playing: %s\n", tracks[i]);
19 audio.connecttoFS(SD, tracks[i]);
20}
21
22void setup() {
23 Serial.begin(115200);
24 pinMode(BTN_NEXT, INPUT_PULLUP);
25 if (!SD.begin(SD_CS)) {
26 Serial.println("SD kart yok / SD card not found");
27 while (true) delay(1000);
28 }
29 audio.setPinout(I2S_BCLK, I2S_LRC, I2S_DIN);
30 audio.setVolume(12); // 0-21
31 playTrack(track);
32}
33
34void loop() {
35 audio.loop(); // sık çağır, delay() kullanma / call often, no delay()
36 static unsigned long lastPress = 0;
37 if (digitalRead(BTN_NEXT) == LOW && millis() - lastPress > 300) {
38 lastPress = millis();
39 track = (track + 1) % TRACK_COUNT;
40 playTrack(track);
41 }
42}
43
44// Şarkı bitince sıradakine geç / move to the next track at the end
45void audio_eof_mp3(const char *info) {
46 track = (track + 1) % TRACK_COUNT;
47 playTrack(track);
48}Web radio: playing live streams from the internet
The same library also plays internet streams. Connect the board to Wi-Fi and call audio.connecttohost("http://...mp3") instead of connecttoFS(); the library decodes MP3 and AAC streams and reports the station name through the audio_info() callback. Streams need much larger buffers than reading from a card: an ESP32 without PSRAM can play a 128 kbps MP3 stream, but dropouts start on weak Wi-Fi; for comfortable radio, choose an ESP32-WROVER or ESP32-S3 board with PSRAM. For a complete project with buttons, an OLED display and station memory, have a look at the ESP32 internet radio article.
I2S on the Raspberry Pi Pico
The RP2040 has no hardware I2S unit, but its programmable PIO blocks can generate I2S signals perfectly. MicroPython's machine.I2S class works this way on the Pico. There's one rule: the WS (LRC) pin must be the SCK (BCLK) pin plus one; for example, if SCK is GP16, WS is GP17. The code below opens a mono I2S bus at 22,050 Hz and plays four notes. If you use the Arduino IDE, the I2S library of the widely used Arduino core for the Raspberry Pi Pico does the same job. Power the module from VBUS (USB 5 V) rather than the Pico's 3.3 V pin.
1# Raspberry Pi Pico + MAX98357A: I2S ile ton (MicroPython, PIO tabanlı I2S / PIO based I2S)
2from machine import I2S, Pin
3import math, struct
4
5SCK_PIN = 16 # MAX98357A BCLK
6WS_PIN = 17 # MAX98357A LRC, SCK + 1 olmalı / must be SCK + 1
7SD_PIN = 18 # MAX98357A DIN
8RATE = 22050 # örnekleme hızı / sample rate
9
10audio = I2S(0, sck=Pin(SCK_PIN), ws=Pin(WS_PIN), sd=Pin(SD_PIN),
11 mode=I2S.TX, bits=16, format=I2S.MONO, rate=RATE, ibuf=8000)
12
13def tone(freq, ms, volume=0.2):
14 period = int(RATE / freq) # bir dalgadaki örnek / samples per cycle
15 buf = bytearray(period * 2)
16 for i in range(period):
17 s = int(math.sin(2 * math.pi * i / period) * 32767 * volume)
18 struct.pack_into("<h", buf, i * 2, s) # 16 bit, little-endian
19 for _ in range(int(RATE * ms / 1000 / period)):
20 audio.write(buf)
21
22silence = bytearray(RATE // 10 * 2) # 0,1 sn sessizlik / 0.1 s of silence
23
24while True:
25 for f in (523, 659, 784, 1047): # Do, Mi, Sol, ince Do / C5, E5, G5, C6
26 tone(f, 250)
27 for _ in range(10):
28 audio.write(silence)Hiss, noise and sound problems
| Symptom | Likely cause | Fix |
|---|---|---|
| No sound at all | BCLK and LRC swapped, SD pin at GND or speaker not connected | Check the pins against the table, leave SD unconnected or drive it HIGH |
| Sound is badly distorted and too fast or too slow | The sample rate in the code differs from the file's | Set the sample rate to match the file |
| Constant hiss and hum | Long wires, grounds not shared, weak supply | Shorten the wires, join all grounds, add a 470–1000 µF capacitor on VIN |
| Ticking sounds while Wi-Fi is on | Wi-Fi transmit bursts make the supply ripple | Add a capacitor, power the amp from a separate clean 5 V |
| The board restarts at high volume | Brownout: the amp draws sudden current | Use an adapter of at least 2 A, don't power the amp from 3.3 V |
| Sound is muffled and crackly | Clipping: gain or digital volume too high | Set GAIN to 9 dB, lower the volume in software |
| A pop at power-up and power-down | The amp switches on before I2S is ready | Hold the SD pin LOW with a GPIO and set it HIGH once I2S has started |
| Slight hiss during silence | Natural noise of a class D amp or high gain | Shut the amp down with SD when not playing, lower the gain |
MAX98357A project ideas
- Wi-Fi internet radio: change stations with buttons and show the song title on an OLED (ESP32 internet radio).
- Talking alarm clock: play a spoken alert from microSD at the set time (ESP32 NTP clock).
- Voice assistant idea: capture audio with an INMP441 microphone and play the reply through the MAX98357A (INMP441 I2S microphone).
- Doorbell: play a tune from microSD when the button is pressed (ESP32 smart doorbell).
- Music-reactive lights: animate an LED strip according to the level of the audio being played (music-reactive LEDs).
If you want to start audio projects with the ESP32, the BlueGrays app takes you from the "ESP32 Buzzer Melody" project to core ESP32 projects such as Wi-Fi connection and web server, each step with a circuit diagram and ready-made code; swap the buzzer for a MAX98357A and the same melodies play through a real speaker.
Frequently asked questions
How many watts does the MAX98357A deliver?
At a 5 V supply it delivers about 3.2 W into a 4 Ω speaker and about 1.8 W into an 8 Ω speaker. These figures are the 10% distortion point; for clean sound you need to stay a little below them. At 3.3 V the power drops to roughly half.
Does the MAX98357A work on 3.3 V?
Yes, the chip runs from 2.5 to 5.5 V. But at 3.3 V the output power drops to about 1.4 W into 4 Ω, and the ESP32's 3.3 V regulator may struggle with the amp's current pulses. For the best results, power the module from 5 V; the I2S signals can stay at 3.3 V level.
Is the MAX98357A stereo?
No, a single MAX98357A gives a mono output; depending on the SD pin it plays the left channel, the right channel or the average of both. For stereo, connect two modules to the same BCLK, LRC and DIN lines and set one to the left channel and the other to the right.
Does the MAX98357A work with an Arduino Uno?
In practice, no. The Arduino Uno has no hardware I2S unit and not enough processing power to decode MP3. For the MAX98357A, use boards with I2S support such as the ESP32, the Raspberry Pi Pico or the Arduino Nano ESP32.
How do I connect the MAX98357A GAIN pin?
Left unconnected, the gain is 9 dB, which is the right choice for most projects. Tying it directly to GND gives 12 dB, to GND through 100 kΩ gives 15 dB, directly to VIN gives 6 dB and to VIN through 100 kΩ gives 3 dB. If the sound clips, lower the gain and set the volume in software.
Why does my MAX98357A hiss or crackle?
The most common causes are long wires, unshared grounds and a weak supply. Shorten the wires, join all grounds, add a 470–1000 µF capacitor on VIN and use a 5 V source that can deliver at least 2 A. If you hear a muffled crackle, the gain or the digital volume is too high.
Projects to build after reading this
See all projects
LED Blink with ESP32
Teaches basic GPIO output usage with ESP32. We achieve a blinking effect by turning an LED on and off from a digital pin. ESP32 version of the classic blink project on Arduino UNO; the basic blink commands in the Arduino environment are the same, but the ESP32's pin layout and 3.3 V GPIO levels differ from those of the Arduino UNO.

Turkish National Anthem with Buzzer
Press the button and a small buzzer starts playing the Turkish National Anthem, note by note.

ESP32 Melody Playback with Buzzer
Playing a simple melody (Twinkle Twinkle Little Star) with a passive buzzer. We play notes with tone() and noTone() functions like in Arduino - ESP32 works with the same syntax as Arduino Core 3.0+. Passive buzzer must be used (active buzzer does not play melody).

ESP32 LED Brightness Control with a Potentiometer (Analog → PWM)
In this project, we control the brightness of an LED analogically using a potentiometer. The ESP32's ADC pin reads the 0-3.3V voltage from the potentiometer, and this value is then converted into a PWM (Pulse Width Modulation) signal and sent to the LED. As a result, as you turn the potentiometer, the LED gradually brightens and dims. With Arduino Core 3.0+, analogWrite() can be used directly; on the ESP32-S2/S3, the ADC resolution is fixed at 12-bit.

ESP32 Wi-Fi STA Connection (with LCD Screen)
In this project, we connect the ESP32 to the home Wi-Fi network in Station (STA) mode in a secure, fault-tolerant manner. We store the SSID and password in a separate secrets.h file to prevent leaking to Git, set up an auto-reconnect mechanism, and display the RSSI signal level and IP address on an I2C LCD screen. The internal LED works as a status indicator. It is a production-grade connection manager.

ESP32 Asynchronous Web Server (with IP Display on LCD)
In this project, we’re turning the ESP32 into an asynchronous web server that you can control from your phone or computer on the same Wi-Fi network. Thanks to the ESPAsyncWebServer library, the server can process multiple requests simultaneously (unlike the classic WebServer.h, which blocks on a single request). You’ll be able to open the ESP32’s IP address in a browser and click the ON/OFF buttons (the /on and /off routes) to turn the LED connected to GPIO 5 on and off.

ESP32 HTTPClient + Weather API (with SSD1306 OLED Display)
In this project the ESP32 makes a REST API call over the internet to a weather API (wttr.in) using HTTPClient.h. Unlike the I2C LCD version, it uses an SSD1306 OLED (128×64) display, with large text and vector icons for a richer look. Seven weather icons (sunny, rainy, snowy and more) are drawn on the fly, with no bitmap files.

ESP32 IR Universal Remote (IR Receiver + Remote Control + 3 LEDs + Buzzer + OLED)
Read a ready-made IR remote with the ESP32 and control your own device! The infrared receiver (NEC protocol, 38kHz) captures the signals of a 20-key remote: keys 1/2/3 toggle three separate LEDs and PWR turns them all off. Press an unknown key and the OLED shows its HEX code, so you can easily add your own remote's buttons to the code.
More articles
BlueGrays app
Build this circuit step by step on your phone
Wiring diagram, code and parts list on one screen. 500+ Arduino, ESP32, ESP8266, Pico, mBot and micro:bit projects ready to build.
Scan with your phone
Point your camera at the code and your phone's app store opens.
or open the store

