How to connect a MAX98357A to ESP32
I2S on GPIO25, 26 and 27, 4-8 Ω speaker.
Wiring diagram
Open in the builderStep-by-step assembly
8 steps- 8 wires in order: ground, power, signals.Click a wire on the diagram or a table row to open its step.The ← → arrow keys flip through the steps.
- Step 1: GND of the module to GND of the USB-C jack
- Step 2: GND of the USB-C jack to GND of the board
- Step 3: Vin of the module to VBUS of the USB-C jack5 V. The module draws up to 550 mA, the pin supplies about 1500 mA.
- Step 4: LRC of the module to D25 of the board
- Step 5: BCLK of the module to D26 of the board
- Step 6: DIN of the module to D27 of the board
- Step 7: plus of the module to plus of the speaker
- Step 8: minus of the module to minus of the speaker
- Done: 8 wires in place.Check against the connection table below.
Connections
| Step | From | To | Color |
|---|---|---|---|
| 1 | USB-C 6P GND | MAX98357A GND | black |
| 2 | ESP32 DOIT GND | USB-C 6P GND | black |
| 3 | USB-C 6P VBUS | MAX98357A Vin | red |
| 4 | ESP32 DOIT D25 | MAX98357A LRC | white |
| 5 | ESP32 DOIT D26 | MAX98357A BCLK | white |
| 6 | ESP32 DOIT D27 | MAX98357A DIN | white |
| 7 | MAX98357A + | Speaker + | white |
| 8 | MAX98357A − | 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.