Example Code for ESP32 Play Music via Bluetooth
Enable ESP32-E Bluetooth function, create a Bluetooth node named bluetoothAmplifier, and use a phone with Bluetooth function to connect that node to play music. Users can learn how to interface the I2S digital amplifier with an ESP32 microcontroller, configure Bluetooth on the ESP32, and connect a speaker for audio output.
Hardware Preparation
- Firebeetle ESP32-E(DFR0654) x 1
- Speaker x1
- Phone with Bluetooth function x1
- Jumper wires x1 set
Software Preparation
- Arduino IDE
- Click the FireBeetle 2 ESP32-E Wikipage to find SDK installtion tutorial
- Download and install the I2S Ampilifer Library. (About how to install the library?)
Wiring Diagram

Description:
I2S Module VCC to ESP32-E 3V3
I2S Module GND to ESP32-E GND
I2S Module LRC to ESP32-E 26/D3
I2S Module BCLK to ESP32-E 25/D2
I2S Module DIN to ESP32-E 14/D6
I2S Module SPK+ to Speaker power +
I2S Module SPK- to Speaker power -
Other Preparation Work
- Open Arduino IDE.
- Select the board as "FireBeetle2 ESP32-E".
- Set the upload rate to 921600.
Sample Code
#include <DFRobot_MAX98357A.h>
DFRobot_MAX98357A amplifier;
void setup(void)
{
Serial.begin(115200);//Set serial rate to 115200
while( !amplifier.begin(/*btName=*/"bluetoothAmplifier", /*bclk=*/GPIO_NUM_25, /*lrclk=*/GPIO_NUM_26,/*din=*/GPIO_NUM_14) ){
Serial.println("Initialize failed !");//No I2S pin signal detected, init failed
delay(3000);
}
Serial.println("Initialize succeed!");//I2S pin signal detected, init succeeded
}
void loop(void)
{
delay(3000);
}
Result
- After uploading the code, open the Arduino IDE Serial Monitor (baud rate 115200) to check if the initialization is successful.
- Turn on the mobile phone Bluetooth and connect to the bluetoothAmplifier node.
- Play music on the phone; the speaker connected to the I2S amplifier will output audio.
Screenshots:
- Select board and upload rate:

- Copy code to Arduino IDE:

- Connect to Bluetooth:

Additional Information
- Ensure the speaker impedance is between 4Ω and 8Ω and the power rating is less than 3W to avoid damaging the amplifier.
- The I2S module's VCC must be connected to the ESP32-E's 3.3V pin to match the voltage level.
Was this article helpful?
