ESP32 Tutorial 39/55 - Using ESP32 as Bluetooth Music Player | SunFounder's ESP32 IoT Learning kit

ESP32 Tutorial 39/55 - Using ESP32 as Bluetooth Music Player | SunFounder's ESP32 IoT Learning kit

In this tutorial, we will explore how to utilize the ESP32 as a Bluetooth music player. The ESP32 is a powerful microcontroller that integrates Wi-Fi and Bluetooth capabilities, making it an excellent choice for IoT applications. We will be using a Bluetooth A2DP sink to stream audio to the ESP32, which can then be output through its built-in DAC.

audio_module

To achieve this, we will set up the ESP32 with the necessary libraries and configurations to handle Bluetooth audio streaming. By the end of this tutorial, you will have a working Bluetooth audio player that can be controlled from your smartphone or other Bluetooth-enabled devices. For a more detailed explanation, be sure to check out the video (in video at 00:00).

speaker_pic

Hardware Explained

The main components for this project include the ESP32 microcontroller and the BluetoothA2DPSink library. The ESP32 is equipped with a built-in Digital-to-Analog Converter (DAC) that allows audio output without any additional hardware. The BluetoothA2DPSink library simplifies the process of setting up a Bluetooth audio sink, enabling you to receive audio streams over Bluetooth.

The ESP32 has multiple GPIO pins that can be configured for various purposes, including audio output. In our setup, we will use GPIO25 for the left and right audio channels. The library takes care of managing the Bluetooth connection and audio streaming, making it easier to focus on the application rather than the underlying Bluetooth protocols.

Datasheet Details

Manufacturer Espressif Systems
Part number ESP32-WROOM-32
Logic/IO voltage 3.3 V
Supply voltage 3.0–3.6 V
Output current (per channel) Up to 40 mA
PWM frequency guidance 1 KHz - 40 KHz
Thermal limits 125 °C
Package QFN48
Notes / variants Various configurations available

 

  • Common GPIO pins can be used for audio output.
  • Ensure proper power supply (3.3 V) to avoid damage.
  • Keep the firmware updated for improved Bluetooth performance.
  • Use external antennas for enhanced Bluetooth range.
  • Be aware of the DAC output limitations; use external DACs for higher quality audio.

Wiring Instructions

esp32-39_bluetooth_audio_wiring

To wire the ESP32 for Bluetooth audio playback, start by connecting the ESP32 to a power source. Use a USB cable to connect it to your computer, which will provide the necessary power. Next, connect the audio output pins: connect the left audio channel to GPIO25 and the right audio channel to the same GPIO25 pin. You can use jumper wires for these connections.

Since the ESP32 has a built-in DAC, you do not need any additional components for audio output. However, ensure that the ground connection is common between the ESP32 and any external audio equipment you might use. This setup will allow you to stream audio directly to the ESP32 via Bluetooth.

Code Examples & Walkthrough

The following code snippet initializes the Bluetooth A2DP sink and sets up the I2S configuration for audio playback.


// ==> Example to use built in DAC of ESP32

#include "BluetoothA2DPSink.h"

BluetoothA2DPSink a2dp_sink;

void setup() {
  const i2s_config_t i2s_config = {
    .mode = (i2s_mode_t)(I2S_MODE_MASTER | I2S_MODE_TX | I2S_MODE_DAC_BUILT_IN),
    .sample_rate = 44100,
    .bits_per_sample = (i2s_bits_per_sample_t)16,
    .channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT,
  };

  a2dp_sink.set_i2s_config(i2s_config);
  a2dp_sink.start("ESP32_Bluetooth");
}

In the setup() function, we first configure the I2S settings, specifying the mode as master and enabling the built-in DAC. The sample rate is set to 44100 Hz, which is standard for audio playback. The Bluetooth A2DP sink is then started with a specified name, allowing it to be recognized by other Bluetooth devices.

The loop() function remains empty, as the Bluetooth library handles the streaming in the background. You can check the complete code loaded below the article for further details.

Demonstration / What to Expect

Once the setup is complete and the code is uploaded, power on the ESP32 and search for the device named "ESP32_Bluetooth" on your Bluetooth-enabled device. Upon connecting, any audio played on the connected device should stream through the ESP32. You can expect high-quality audio output through the DAC as long as the connections are correct and the power supply is stable.

Common pitfalls include incorrect wiring of the audio output pins and inadequate power supply. Make sure the ESP32 is powered properly and that the ground connections are secure. If you encounter any issues, check the video for troubleshooting tips (in video at 00:00).

Video Timestamps

  • 00:00 Start
  • 2:12 Introduction
  • 5:22 Wiring explained
  • 7:51 Arduino Code explained
  • 10:37 Selecting ESP32 board and COM port in Arduino ide
  • 12:19 Playing music using ESP32 from mobile phone
  • 13:30 Connecting more powerful amplifier to ESP32

图像

esp32-39_bluetooth_audio_wiring
esp32-39_bluetooth_audio_wiring
speaker_pic
speaker_pic
audio_module
audio_module
840-ESP32 Tutorial 39/55- Arduino code for playing audio via bluetooth
语言: C++
已复制!

文件📁

没有可用的文件。