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

Software Preparation

Wiring Diagram

Connection 1

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

  1. Open Arduino IDE.
  2. Select the board as "FireBeetle2 ESP32-E".
  3. 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

  1. After uploading the code, open the Arduino IDE Serial Monitor (baud rate 115200) to check if the initialization is successful.
  2. Turn on the mobile phone Bluetooth and connect to the bluetoothAmplifier node.
  3. Play music on the phone; the speaker connected to the I2S amplifier will output audio.

Screenshots:

  • Select board and upload rate:
    Set upload rate
  • Copy code to Arduino IDE:
    Copy codes
  • Connect to Bluetooth:
    Connect 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?

TOP