How to connect a MAX98357A to ESP32

I2S on GPIO25, 26 and 27, 4-8 Ω speaker.

Wiring diagram

Open in the builder

Connections

StepFromToColor
1USB-C 6P GNDMAX98357A GNDblack
2ESP32 DOIT GNDUSB-C 6P GNDblack
3USB-C 6P VBUSMAX98357A Vinred
4ESP32 DOIT D25MAX98357A LRCwhite
5ESP32 DOIT D26MAX98357A BCLKwhite
6ESP32 DOIT D27MAX98357A DINwhite
7MAX98357A +Speaker +white
8MAX98357A −Speaker −white

Power and wiring check

  • The schematic check, the same as in the builder, found no errors: pin voltages match, ground is common, buses are fully connected.
  • MAX98357A draws up to 550 mA (about 20 typically) from the VBUS pin of USB-C 6P, the pin supplies about 1500 mA: USB-C with 5.1 kΩ resistors: charger up to 1.5–3 A, computer port 500–900 mA.

Current of the whole circuit and how long it runs on a battery: power budget. Where to plug what: breadboard layout.

Example code

Arduino IDE, ESP_I2S from the ESP32 Arduino core 3.x.

#include <ESP_I2S.h>

I2SClass i2s;
int16_t buf[32];

void setup() {
  i2s.setPins(26, 25, 27);  // BCLK, LRC, DIN
  i2s.begin(I2S_MODE_STD, 16000, I2S_DATA_BIT_WIDTH_16BIT, I2S_SLOT_MODE_MONO);
  // 500 Hz square wave: 16 samples up, 16 down at 16,000 samples per second
  for (int i = 0; i < 32; i++) buf[i] = i < 16 ? 3000 : -3000;
}

void loop() {
  i2s.write((uint8_t *)buf, sizeof(buf));
}

Part pinouts

Next steps for this circuit

How it is connected

The MAX98357A receives sound over I2S: LRC (channel select) on GPIO25, BCLK (clock) on GPIO26, DIN (data) on GPIO27. The numbers can be changed to any ESP32 outputs, and in code you then change the numbers in setPins. The amplifier doesn't need MCLK.

A 4 Ω amplifier at full volume draws up to 550 mA from 5 V, while the DevKit's VIN pin on USB gives about 300 mA. That is why in the schematic the amplifier's Vin comes from a separate USB-C board with a 5 V supply, with the grounds connected to the ESP32's GND. A 4-8 Ω speaker connects to the + and − terminals, and the speaker's minus is not connected to ground: the output is bridged.

Examples

  • GAIN floating gives 9 dB, to GND 12 dB, to Vin 6 dB. The SD pin left unconnected with 5 V power gives the average of the left and right channels.

FAQ

Why does the ESP32 reset at loud sound?
The amplifier draws power from the same 5 V line as the board, and the voltage sags. Power the MAX98357A from a separate 5 V supply rated 1 A, or put a 470-1000 µF capacitor at Vin.
How do I play MP3 or internet radio?
The ESP8266Audio and ESP32-audioI2S libraries decode MP3 and send the sound to I2S. The pins are the same: BCLK, LRC and DIN.

You might also need