Guides · 9 min read

ESP32 Bluetooth Speaker: Stream Music from Your Phone (A2DP)

Build an ESP32 Bluetooth speaker: phone music with ESP32-A2DP, MAX98357A I2S amp or the built-in DAC, why S3/C3 can't, sound and power table, rechargeable box.

An ESP32 Bluetooth speaker project turns an ESP32 board that costs a few dollars into a real wireless speaker that shows up in your phone's Bluetooth list. You pair the phone with the ESP32, music playing in Spotify, YouTube or any other app streams to the ESP32 over Bluetooth, and the ESP32 sends the sound to an amplifier and then on to the speaker. What makes this possible is the A2DP profile used by headphones and speakers, and with the ESP32-A2DP library it only takes a few lines of code.

In this guide you'll see how A2DP works, why only the classic ESP32 can do this job (and why the ESP32-S3 and C3 can't), and the difference between an I2S amplifier and the built-in DAC as an output. Then we'll build the first speaker with a MAX98357A amp, try the built-in DAC version, add track and volume buttons, work out battery life with a sound quality and power table, and design a rechargeable portable box with an 18650 cell.

ESP32 development board for a Bluetooth speaker project
Classic ESP32 (ESP32-WROOM-32): thanks to Bluetooth Classic it can receive music over A2DP.

How does an ESP32 Bluetooth speaker work?

A2DP (Advanced Audio Distribution Profile) is the profile that carries stereo audio over Bluetooth Classic. The phone is the source and the ESP32 is the sink. The phone compresses the music with the SBC codec and sends it; the ESP32 decodes the packets and gets 44.1 kHz, 16-bit stereo PCM audio. The library writes this raw audio, about 176 kB per second, to the I2S bus or to the built-in DAC. The AVRCP profile running alongside carries commands in the other direction: play, pause, next track, volume and track title all travel through it. Since Wi-Fi isn't used, you don't need a router or internet; the range between the phone and the ESP32 is about 10 metres in the open.

FeatureESP32 A2DP sink
Bluetooth typeBluetooth Classic (BR/EDR), not BLE
Audio profileA2DP sink + AVRCP (control and track info)
CodecSBC, up to about 328 kbit/s
Audio out44.1 kHz, 16-bit, stereo PCM
LatencyAbout 150–250 ms (slightly noticeable with video)
RangeAbout 10 m in the open
Program sizeAbout 1.3 MB (Huge APP partition recommended)

Which ESP32? Why you need the classic ESP32

A2DP needs Bluetooth Classic. In the ESP32 family, Bluetooth Classic is only found on the first, "classic" ESP32 (ESP32-WROOM-32, ESP32-WROVER, ESP32-PICO-D4 and the DevKit boards built on them). The newer ESP32-S3, ESP32-C3 and ESP32-C6 support Bluetooth Low Energy (BLE) only; since BLE has no classic A2DP profile, these boards can't show up as a speaker on your phone. The ESP32-S2 has no Bluetooth at all. If the metal can on your board says "ESP32-WROOM-32" or "ESP32-WROVER", you're on the right track. The wireless chip of the Raspberry Pi Pico W also supports Bluetooth Classic and a similar receiver can be built with the arduino-pico core, but this article focuses on the ESP32.

Board / chipBluetooth ClassicBLECan it be an A2DP speaker?
ESP32 (WROOM-32, WROVER, PICO-D4)YesYesYes
ESP32-S2NoNoNo
ESP32-S3NoYes (BLE 5)No
ESP32-C3NoYes (BLE 5)No
ESP32-C6NoYes (BLE 5)No
ESP8266NoNoNo
If you try to compile this code for an ESP32-S3 or C3 board, the library throws an error or the board never shows up on the phone; the problem is the chip, not the code. With those boards you can still make wireless audio that plays over Wi-Fi, such as an internet radio, but they won't work as a Bluetooth speaker for a phone.

Choosing the output: I2S amp or built-in DAC?

The ESP32 can output sound in two ways. I2S is a digital audio bus: the audio travels to the amplifier digitally on a bit clock (BCLK), a channel clock (LRC) and a data line (DIN), and the conversion happens inside the amp. An I2S amp such as the MAX98357A carries both the DAC and a class-D amplifier on one board and delivers up to 3.2 W into a 4 Ω speaker at 5 V. The built-in DAC is the ESP32's 8-bit analog output on GPIO 25 and 26; it needs no extra board, but with only 256 steps of resolution you hear a clear hiss in quiet passages, and it can't drive a speaker on its own, so it needs an analog amp such as the PAM8403 after it. The table sums up which output suits which project.

OutputResolution and qualitySpeaker powerExtra partsBest for
MAX98357A (I2S amp, mono)16–32 bit, about 92 dB SNR, clean3.2 W / 4 Ω, 1.8 W / 8 Ω at 5 VNoneSingle-speaker portable box
2 × MAX98357A (stereo)Same, separate left and right2 × 3.2 WChannel select on the SD pinStereo desk speaker
PCM5102A (I2S DAC)32 bit, about 112 dB, cleanestLine level, no powerExternal amp or headphonesAUX into a hi-fi or car stereo
Built-in DAC (GPIO 25/26)8 bit, audible hissNo powerAnalog amp such as PAM8403Tests, toys, speech

Wiring: ESP32 and MAX98357A

MAX98357A pinESP32Note
VIN5V (VIN)2.5–5.5 V; at 3.3 V the power drops by half
GNDGNDCommon ground
BCLKGPIO 27Bit clock
LRCGPIO 26Left/right channel clock (WS)
DINGPIO 25Audio data
GAINUnconnectedFloating 9 dB; to GND 12 dB, to VIN 6 dB
SDUnconnectedFloating: mix of both channels (mono)
+ / −Speaker4–8 Ω, 3 W; neither terminal goes to ground

Step by step: your first Bluetooth speaker

  1. Make sure the ESP32 board package is installed in the Arduino IDE (version 2.x or 3.x both work).
  2. Download Phil Schatzmann's ESP32-A2DP and arduino-audio-tools libraries as ZIP files from their GitHub pages and install both via Sketch > Include Library > Add .ZIP Library.
  3. Select ESP32 Dev Module as the board, and choose Huge APP (3MB No OTA) under Tools > Partition Scheme.
  4. Wire the MAX98357A as in the table and connect a 4 Ω or 8 Ω speaker.
  5. Upload the code below and open the Serial Monitor at 115200 baud.
  6. Find "BlueGrays Hoparlor" in your phone's Bluetooth settings, pair it and start playing in any music app.
C++ (Arduino IDE)
1// ESP32 Bluetooth hoparlör: A2DP alıcı + MAX98357A I2S amfi
2// ESP32 Bluetooth speaker: A2DP sink + MAX98357A I2S amplifier
3// Gerekli kütüphaneler / required libraries: ESP32-A2DP + arduino-audio-tools (pschatzmann)
4#include "AudioTools.h"
5#include "BluetoothA2DPSink.h"
6
7I2SStream i2s;                  // I2S çıkışı / I2S output
8BluetoothA2DPSink a2dp_sink(i2s);
9
10void setup() {
11  Serial.begin(115200);
12
13  auto cfg = i2s.defaultConfig(TX_MODE);
14  cfg.pin_bck = 27;   // MAX98357A BCLK
15  cfg.pin_ws = 26;    // MAX98357A LRC
16  cfg.pin_data = 25;  // MAX98357A DIN
17  i2s.begin(cfg);     // Örnekleme hızını A2DP bağlantıda ayarlar / the sample rate is set by A2DP on connect
18
19  a2dp_sink.start("BlueGrays Hoparlor");  // Telefonda görünen ad / name shown on the phone
20  Serial.println(F("Telefondan eslestir / pair from your phone"));
21}
22
23void loop() {
24  delay(1000);  // Ses arka planda çalar / audio runs in the background
25}

There are only a few working lines in the code because the whole Bluetooth stack, SBC decoding and I2S output run in the background inside the library. The I2SStream object learns the pins; when the phone connects, A2DP tells the I2S output the 44.1 kHz sample rate automatically. The name in start() is the one shown in the phone's list; plain ASCII letters avoid garbled names on some phones. loop() can stay empty; later we'll put button, LED or display code here. The compiled program is about 1.3 MB and fills 99 percent of the flash on the default partition scheme, which is why the Huge APP option matters.

Speaker with the built-in DAC (and a PAM8403 amp)

If you don't have an I2S amp, you can use the ESP32's own DAC. The left channel comes out of GPIO 25 and the right channel out of GPIO 26; each gives a signal that swings between 0 and 3.3 V around a midpoint of about 1.65 V. Take this signal through a 1 µF capacitor and a 10 kΩ volume potentiometer to the L and R inputs of a 5 V analog amp such as the PAM8403, and tie the amp's GND to the ESP32's GND. Never connect a speaker straight to a GPIO pin: the pin only supplies a few milliamps and can be damaged.

C++ (Arduino IDE)
1// ESP32 Bluetooth hoparlör: dahili 8 bit DAC (GPIO 25 ve 26) + PAM8403 amfi
2// ESP32 Bluetooth speaker: built-in 8-bit DAC (GPIO 25 and 26) + PAM8403 amplifier
3#include "AudioTools.h"
4#include "BluetoothA2DPSink.h"
5
6AnalogAudioStream out;  // Dahili DAC: sol kanal GPIO 25, sağ kanal GPIO 26 / built-in DAC: left GPIO 25, right GPIO 26
7BluetoothA2DPSink a2dp_sink(out);
8
9void setup() {
10  Serial.begin(115200);
11  a2dp_sink.start("BlueGrays DAC");
12}
13
14void loop() {
15  delay(1000);
16}

This version doesn't use the I2S pins, so GPIO 25 and 26 are reserved for the DAC; if you also want a MAX98357A at the same time, move the I2S pins to other GPIOs. If the sound is quiet and hissy, turn the phone volume all the way up and set the level with the potentiometer; turning the volume down digitally on an 8-bit output lowers the resolution even more. It also works with ESP32 core 3.x; arduino-audio-tools picks the new DAC driver by itself.

Buttons, track title and auto reconnect

On a real speaker you want to skip tracks without taking the phone out of your pocket. Thanks to AVRCP, the ESP32 can send play, pause, next and previous commands to the phone, and the phone sends back the track title and artist. The code below has three buttons: GPIO 32 is play/pause, GPIO 33 is next track on a short press and volume up on a long press, and GPIO 14 is previous track on a short press and volume down on a long press. The LED on GPIO 2 lights while a phone is connected. With auto reconnect on, the box connects to the last paired phone by itself after you switch it off and on. GPIO 34 measures the battery voltage through a 100 kΩ + 100 kΩ divider.

C++ (Arduino IDE)
1// Taşınabilir ESP32 Bluetooth hoparlör: butonlar (oynat/duraklat, sonraki, önceki, ses),
2// bağlantı LED'i, şarkı adı, otomatik yeniden bağlanma ve pil gerilimi ölçümü
3// Portable ESP32 Bluetooth speaker: buttons (play/pause, next, previous, volume),
4// connection LED, track title, auto reconnect and battery voltage measurement
5#include "AudioTools.h"
6#include "BluetoothA2DPSink.h"
7
8const int BTN_PLAY = 32;   // Oynat / duraklat / play / pause
9const int BTN_NEXT = 33;   // Sonraki şarkı (uzun bas: ses +) / next track (long press: volume up)
10const int BTN_PREV = 14;   // Önceki şarkı (uzun bas: ses -) / previous track (long press: volume down)
11const int LED_PIN = 2;     // Bağlantı LED'i / connection LED
12const int BAT_PIN = 34;    // Pil gerilimi, 100k/100k bölücü / battery voltage, 100k/100k divider
13
14I2SStream i2s;
15BluetoothA2DPSink a2dp_sink(i2s);
16
17struct Button { int pin; bool last; unsigned long downAt; bool longDone; };
18Button buttons[3] = {{BTN_PLAY, HIGH, 0, false}, {BTN_NEXT, HIGH, 0, false}, {BTN_PREV, HIGH, 0, false}};
19
20void onMetadata(uint8_t id, const uint8_t *text) {
21  // Şarkı adı ve sanatçı Seri Monitör'e / track title and artist to the Serial Monitor
22  if (id == ESP_AVRC_MD_ATTR_TITLE) Serial.printf("Sarki / title: %s\n", text);
23  if (id == ESP_AVRC_MD_ATTR_ARTIST) Serial.printf("Sanatci / artist: %s\n", text);
24}
25
26void onConnection(esp_a2d_connection_state_t state, void *ptr) {
27  digitalWrite(LED_PIN, state == ESP_A2D_CONNECTION_STATE_CONNECTED ? HIGH : LOW);
28}
29
30float batteryVolts() {
31  // Bölücü gerilimi yarıya indirir: 4,2 V pil -> 2,1 V pin / the divider halves it: 4.2 V cell -> 2.1 V on the pin
32  return analogReadMilliVolts(BAT_PIN) * 2 / 1000.0;
33}
34
35void shortPress(int i) {
36  if (i == 0) {  // Ses akıyorsa duraklat, değilse oynat / pause if audio is streaming, else play
37    if (a2dp_sink.get_audio_state() == ESP_A2D_AUDIO_STATE_STARTED) a2dp_sink.pause();
38    else a2dp_sink.play();
39  }
40  if (i == 1) a2dp_sink.next();
41  if (i == 2) a2dp_sink.previous();
42}
43
44void longPress(int i) {
45  int v = a2dp_sink.get_volume();  // 0-127
46  if (i == 1) a2dp_sink.set_volume(min(v + 16, 127));
47  if (i == 2) a2dp_sink.set_volume(max(v - 16, 0));
48}
49
50void setup() {
51  Serial.begin(115200);
52  pinMode(LED_PIN, OUTPUT);
53  for (auto &b : buttons) pinMode(b.pin, INPUT_PULLUP);
54
55  auto cfg = i2s.defaultConfig(TX_MODE);
56  cfg.pin_bck = 27;
57  cfg.pin_ws = 26;
58  cfg.pin_data = 25;
59  i2s.begin(cfg);
60
61  a2dp_sink.set_avrc_metadata_attribute_mask(ESP_AVRC_MD_ATTR_TITLE | ESP_AVRC_MD_ATTR_ARTIST);
62  a2dp_sink.set_avrc_metadata_callback(onMetadata);
63  a2dp_sink.set_on_connection_state_changed(onConnection);
64  a2dp_sink.set_auto_reconnect(true);    // Son telefona yeniden bağlan / reconnect to the last phone
65  a2dp_sink.start("BlueGrays Kutu");
66  a2dp_sink.set_volume(80);              // Başlangıç sesi (0-127) / start volume (0-127)
67}
68
69void loop() {
70  for (int i = 0; i < 3; i++) {
71    Button &b = buttons[i];
72    bool s = digitalRead(b.pin);
73    if (s == LOW && b.last == HIGH) { b.downAt = millis(); b.longDone = false; }
74    if (s == LOW && !b.longDone && millis() - b.downAt > 600) { longPress(i); b.longDone = true; }
75    if (s == HIGH && b.last == LOW && !b.longDone && millis() - b.downAt > 30) shortPress(i);
76    b.last = s;
77  }
78
79  static unsigned long lastBat = 0;
80  if (millis() - lastBat > 30000) {  // 30 sn'de bir pil kontrolü / battery check every 30 s
81    lastBat = millis();
82    float v = batteryVolts();
83    Serial.printf("Pil / battery: %.2f V\n", v);
84    if (v < 3.3) Serial.println(F("Pil zayif, sarj et / battery low, recharge"));
85  }
86  delay(10);
87}
FunctionWhat it does
start("name")Starts Bluetooth and becomes visible under the given name
play() / pause()Sends a play or pause command to the phone
next() / previous()Skips to the next or previous track
set_volume(0–127) / get_volume()Sets or reads the volume; can stay in sync with the phone
set_auto_reconnect(true)Reconnects to the last paired device at start-up
set_avrc_metadata_callback()Calls your function when title, artist and album arrive
set_on_connection_state_changed()Calls your function on connect and disconnect

Sound quality, power and battery life table

Bluetooth Classic uses far more energy than BLE. The ESP32 alone draws 100–130 mA while music is streaming, and the amp's current depends on the volume. To estimate battery life, start with the battery's energy: a 2600 mAh 18650 cell holds 3.7 V × 2.6 Ah = 9.6 Wh, and with the 85 percent efficiency of the 5 V boost module about 8.2 Wh is usable. Divide that by the total power at 5 V. For example, if 260 mA is drawn at medium volume, the power is 5 V × 0.26 A = 1.3 W and the runtime is 8.2 ÷ 1.3 ≈ 6 hours. To try your own values, use the battery life calculator.

State (at 5 V)ESP32MAX98357A + 4 Ω speakerTotalApprox. runtime on a 2600 mAh 18650
Waiting to pair, no music60–80 mA2–3 mAabout 75 mA21 hours
Music, low volume110 mA40 mAabout 150 mA11 hours
Music, medium volume110 mA150 mAabout 260 mA6 hours
Music, loud (3 W peaks)110 mA450 mA averageabout 560 mA3 hours

A rechargeable portable speaker box

  • Power chain: 18650 cell → TP4056 charging module with protection (charges from USB) → on/off switch → a 5 V boost converter such as the MT3608 → ESP32 VIN and MAX98357A VIN.
  • Before fitting it, set the boost converter's output to exactly 5.0–5.1 V with a multimeter; choose a module that can deliver 1 A for loud peaks and put a 470 µF capacitor on its output.
  • To measure the battery voltage, connect a 100 kΩ + 100 kΩ divider to GPIO 34; warn below 3.3 V and shut down at 3.0 V so the cell isn't deep-discharged.
  • For the speaker, pick a 4 Ω, 3–5 W driver of 40–57 mm; a closed, sealed box makes the same speaker sound much fuller than in open air.
  • Aim for an internal volume of 0.3–0.5 litres and line the inside with felt or foam; a small bass port in the back panel adds low end.
  • Put the buttons on top and the charging socket and switch on the back; don't cover the ESP32 antenna with metal, or the range will drop.
TP4056 Li-ion charging module
TP4056 module: charges an 18650 cell from USB at 1 A; pick the version with a protection circuit.

Common problems and fixes

SymptomLikely causeFix
"Sketch too big" errorThe program is 1.3 MB and the default partition is too smallPartition Scheme: Huge APP (3MB No OTA)
Device doesn't show up on the phoneAn ESP32-S3/C3 is used, or the board never startedUse a classic ESP32, check the start-up message in the Serial Monitor
Connects but no soundBCLK, LRC or DIN pins mixed upCompare the pins with the table and code, measure 5 V on VIN
Sound drops out or cracklesWeak supply, long I2S wires or Wi-Fi onKeep wires under 10 cm, add a capacitor on the 5 V line, don't use Wi-Fi
Volume too lowLow GAIN or low phone volumeTie GAIN to GND for 12 dB, turn up the phone
ESP32 resets at high volumeThe amp's current peaks pull the voltage down1 A boost converter, 470–1000 µF capacitor, thicker supply wires
Strong hiss on the built-in DAC8-bit resolution and groundingTurn the phone up fully and reduce with the pot, or switch to an I2S amp
If you want stereo, use two MAX98357A boards and connect the BCLK, LRC and DIN pins of both to the same GPIOs. The SD pin selects the channel: an amp with SD tied straight to VIN plays the left channel, and an amp with about 220 kΩ between SD and VIN plays the right channel. The code doesn't change; adding a second amp and speaker widens the sound of the box.

Project ideas

  • Idea: Fit an ESP32 speaker inside an old radio or gramophone case for a retro-looking modern speaker.
  • Idea: Lights that flash with the music; read the audio data with set_stream_reader() and feed it to a WS2812B LED strip.
  • Idea: Put both a Bluetooth and a Wi-Fi radio mode in the same box; switch to internet radio with a button.
  • Idea: A desk speaker that shows the track title on a small OLED display.
  • Idea: A splash-resistant outdoor speaker with a big battery for cycling or camping.
  • Idea: Add Bluetooth to an old car stereo's AUX input with a PCM5102A.

An ESP32 Bluetooth speaker is one of the most rewarding first projects in audio electronics: with a few wires and a short sketch you listen to every song on your phone through a box you built yourself, then improve it step by step with buttons, a battery and lights. If you'd like to see basic ESP32 projects such as LEDs, buttons and buzzer melodies with circuit diagrams and ready-to-use code, have a look at the ESP32 projects in the BlueGrays app.

Frequently asked questions

Can an ESP32 be used as a Bluetooth speaker?

Yes, the classic ESP32 supports Bluetooth Classic and the A2DP profile, so it shows up on your phone like a real speaker. With the ESP32-A2DP library, a few lines of code and an I2S amp such as the MAX98357A, you can play music from your phone. The audio arrives as 44.1 kHz, 16-bit stereo.

Can I make a Bluetooth speaker with an ESP32-S3 or ESP32-C3?

No. A2DP needs Bluetooth Classic; the ESP32-S3, C3 and C6 only support Bluetooth Low Energy, and the ESP32-S2 has no Bluetooth at all. These boards can't appear as a speaker on a phone. For a Bluetooth speaker you need a classic ESP32 board based on the ESP32-WROOM-32 or WROVER.

Which amplifier should I use for an ESP32 Bluetooth speaker?

The most practical choice is the MAX98357A I2S amp: DAC and class-D amp on one board, 3.2 W into 4 Ω at 5 V, no extra parts. For cleaner sound with an external amp you can use the PCM5102A I2S DAC. If you use the built-in DAC you'll need an analog amp such as the PAM8403, but the 8-bit output has audible hiss.

How good is ESP32 Bluetooth audio quality?

The phone sends music with the SBC codec at up to about 328 kbit/s and the ESP32 outputs 44.1 kHz 16-bit stereo, the same level as ordinary Bluetooth speakers. The real difference comes from the output stage: with an I2S amp or DAC the sound is clean, while the built-in 8-bit DAC adds audible hiss. Advanced codecs such as aptX and LDAC aren't supported.

How long does an ESP32 Bluetooth speaker run on a battery?

With one 2600 mAh 18650 cell and a MAX98357A it runs about 6 hours at medium volume and over 10 hours at low volume. To calculate it, multiply the battery energy (3.7 V × 2.6 Ah = 9.6 Wh) by the boost converter efficiency and divide by the total power at 5 V. At high volume the runtime drops to around 3 hours.

Why does my ESP32 A2DP sketch say "Sketch too big"?

With the Bluetooth stack and audio libraries the program is about 1.3 MB and fills the default 1.2 MB app partition. Choosing Huge APP (3MB No OTA) under Tools > Partition Scheme in the Arduino IDE gives you 3 MB and fixes the problem. With this option over-the-air (OTA) updates aren't available.

See all projects
LED Blink with ESP32
ES-1Beginner

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.

4.7(38)
114
Using a Buzzer
AR-13Beginner

Using a Buzzer

Ever wondered how a doorbell or an alarm makes its sound? The Arduino makes a small buzzer beep at short intervals: beep, pause, beep, pause.

4.7(93)
180
ESP32 Melody Playback with Buzzer
ES-12Beginner
Premium

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).

4.8(31)
55
Control an LED from Your Phone via Bluetooth
AR-34Beginner
Premium

Control an LED from Your Phone via Bluetooth

A small rehearsal for switching the lights in your room on and off from your phone.

4.8(69)
91
Bluetooth Controlled Car with L298N
AR-80Advanced
Premium

Bluetooth Controlled Car with L298N

Turn your phone into a remote and drive your own robot car. The Arduino receives commands from a remote-control app on your phone through a Bluetooth module and spins the wheels with a motor driver…

5.0(39)
66
Turkish National Anthem with Buzzer
AR-15Intermediate

Turkish National Anthem with Buzzer

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

4.8(117)
275
Frequency Setting with Buzzer
AR-14Intermediate

Frequency Setting with Buzzer

You control the sound of a buzzer with four buttons: one starts the sound, one silences it, and the other two make it higher or lower.

4.6(69)
124