DIY Bluetooth Speaker Using Esp32 With Controls

by Brian_Imenje in Circuits > Gadgets

169 Views, 5 Favorites, 0 Comments

DIY Bluetooth Speaker Using Esp32 With Controls

IMG_20250101_102410.jpg

Are you ready to turn your ESP32 into the ultimate Bluetooth audio experience? This project combines the power of the ESP32's Bluetooth capabilities with your creativity to craft a custom, portable speaker that rivals off-the-shelf options.

Whether you're a maker, music lover, or tech enthusiast, this guide will walk you through creating a Bluetooth speaker that connects wirelessly to your devices and delivers crystal-clear sound. With a sleek design, customizable features, and a dash of DIY fun, your ESP32 Bluetooth speaker will impress your ears and your friends.

Let’s dive in and transform your ESP32 into a musical masterpiece! 🎶

Supplies

images.jpg
download (4).jpg
download (3).jpg
download (2).jpg
IMG_20250101_102654.jpg

What You’ll Need

Here’s what you need to start jamming:

  1. ESP32 microcontroller: The brains of the operation.
  2. MAX98357A DAC amplifier module: Converts digital audio signals into amplified sound.
  3. Speaker: 4Ω or 8Ω, 3W recommended for best results.
  4. Jumper wires: To connect everything neatly.
  5. 10μF capacitor (optional): For power smoothing.
  6. Micro-USB cable: To program and power the ESP32.
  7. 5 pushbuttons: For music control.
  8. Screwdrivers, tape, and a box (optional): Create a cool enclosure.

How It Works.

Your ESP32 will act as a Bluetooth receiver, decoding audio signals. The MAX98357A will then amplify these signals and drive your speaker to produce sound. This setup is simple yet powerful—perfect for a DIY sound system!

Gather the Tools and Materials

Make sure all components are on hand. Double-check your speaker rating (4Ω or 8Ω, 3W). Lay out your jumper wires and capacitor, if you're using one.

Circuit Wiring.

download (1).jpg
esp32_pinout.jpeg


  1. Power Connections:
  2. VIN (ESP32) to VIN (MAX98357A): Provide power to the amplifier. Ensure the voltage matches the MAX98357A's requirements (typically 5V).
  3. GND (ESP32) to GND (MAX98357A): Common ground connection.
  4. Audio Signal Connections:
  5. BCLK (MAX98357A) to GPIO 27 (ESP32): Bit clock signal (controls the timing of audio data).
  6. LRC (MAX98357A) to GPIO 14 (ESP32): Left-right clock signal (determines which channel the audio data belongs to).
  7. DIN (MAX98357A) to GPIO 26 (ESP32): Digital audio data input.
  8. Control buttons:
  9. Connect one side of all the buttons to Ground.
  10. Connect the other side of the buttons to GPIO pins(esp32)ie. [5, 18,19,21,22]
  11. Optional Pins:
  12. SD (Shutdown): Connect to a GPIO pin to control the amplifier's power state. Leave it floating if not used.
  13. GAIN: Leave unconnected or follow the datasheet to adjust the gain.
  14. Optional Capacitor:
  15. Place the 10μF capacitor between VIN and GND on the MAX98357A to reduce noise.

Install the Necessary Libraries.

https://github.com/pschatzmann/arduino-audio-tools.git

https://github.com/pschatzmann/ESP32-A2DP.git

The above 2 libraries are required to run the project. For the Arduino IDE, you can simply download the zip file and install the libraries to the IDE.

For PlatformIO you can either add to the lib_deps as shown:

lib_deps =
https://github.com/pschatzmann/ESP32-A2DP.git
https://github.com/pschatzmann/arduino-audio-tools.git

or just git-clone the repositories to the lib folder of your project.

Coding the Esp32.

#include <Arduino.h>
#include "AudioTools.h"
#include "BluetoothA2DPSink.h"

// Button GPIO pins
#define BUTTON_PLAY_PAUSE 5
#define BUTTON_PREVIOUS 18
#define BUTTON_NEXT 19
#define BUTTON_VOLUME_UP 21
#define BUTTON_VOLUME_DOWN 22

#define I2S_LRC 14
#define I2S_BCLK 27
#define I2S_DIN 26

// I2S and Bluetooth sink
I2SStream i2s;
BluetoothA2DPSink a2dp_sink(i2s);

// Button states
bool lastPlayPauseState = HIGH;
bool lastPreviousState = HIGH;
bool lastNextState = HIGH;
bool lastVolumeUpState = HIGH;
bool lastVolumeDownState = HIGH;

// Playback state
bool isPlaying = false;

void setup() {
Serial.begin(115200);

// Configure buttons
pinMode(BUTTON_PLAY_PAUSE, INPUT_PULLUP);
pinMode(BUTTON_PREVIOUS, INPUT_PULLUP);
pinMode(BUTTON_NEXT, INPUT_PULLUP);
pinMode(BUTTON_VOLUME_UP, INPUT_PULLUP);
pinMode(BUTTON_VOLUME_DOWN, INPUT_PULLUP);

// Configure I2S pins
auto cfg = i2s.defaultConfig();
cfg.pin_bck = I2S_BCLK; // BCLK pin
cfg.pin_ws = I2S_LRC; // LRC pin
cfg.pin_data = I2S_DIN; // DIN pin
i2s.begin(cfg);

// Start Bluetooth sink
a2dp_sink.start("ESPEAKER");
a2dp_sink.set_volume(64);
}

void loop() {
// Read button states
bool currentPlayPauseState = digitalRead(BUTTON_PLAY_PAUSE);
bool currentPreviousState = digitalRead(BUTTON_PREVIOUS);
bool currentNextState = digitalRead(BUTTON_NEXT);
bool currentVolumeUpState = digitalRead(BUTTON_VOLUME_UP);
bool currentVolumeDownState = digitalRead(BUTTON_VOLUME_DOWN);
Serial.println(a2dp_sink.get_volume());
// Handle Play/Pause button
if (currentPlayPauseState == LOW && lastPlayPauseState == HIGH) {
Serial.println(" Play/Pause Button Pressed");
if (isPlaying) {
a2dp_sink.pause(); // Call pause method
isPlaying = false;
Serial.println(" Paused");
} else {
a2dp_sink.play(); // Call play method
isPlaying = true;
Serial.println(" Playing");
}
}
lastPlayPauseState = currentPlayPauseState;


// Handle Previous Track button
if (currentPreviousState == LOW && lastPreviousState == HIGH) {
Serial.println("Previous Track Button Pressed");
a2dp_sink.previous(); // Go to the previous track
}
lastPreviousState = currentPreviousState;

// Handle Next Track button
if (currentNextState == LOW && lastNextState == HIGH) {
Serial.println("Next Track Button Pressed");
a2dp_sink.next(); // Go to the next track
}
lastNextState = currentNextState;

// Handle Volume Up button
if (currentVolumeUpState == LOW && lastVolumeUpState == HIGH) {
Serial.println("Volume Up Button Pressed");
a2dp_sink.volume_up(); // increment volume by one
}
lastVolumeUpState = currentVolumeUpState;

// Handle Volume Down button
if (currentVolumeDownState == LOW && lastVolumeDownState == HIGH) {
Serial.println("Volume Down Button Pressed");
a2dp_sink.volume_down(); // decrement volume by one
}
lastVolumeDownState = currentVolumeDownState;

// Short delay to debounce buttons
delay(50);
}

Downloads

Upload the Code.

  1. Upload the code above by pressing the upload button in your editor.
  2. For PlatformIO you will have to build it first before uploading.

Testing the Setup

  1. Power up the ESP32 using a micro-USB cable.
  2. On your phone or computer, search for a Bluetooth device named ESPEAKER.
  3. Pair with it and play some music.

🎉 Boom! Your speaker should now sing your favorite tunes!

Troubleshooting

  1. If you get a compilation size error you can in fix it the following ways:
  2. In the Arduino IDE go to the tools menu and under partitions select a larger partition size[Huge App].
  3. In PlatformIO just add a partition table CVS file to the root of the project and tell platform io to use it by adding the following line to the platformio.ini file.

Downloads

Optional Enhancements.

Build an Enclosure:

  1. Use a 3D-printed case, or a wooden box, or repurpose an old toy to house your project.
  2. Add cutouts for the speaker, ESP32, and wires.

LED Indicators:

  1. Add an RGB LED to indicate Bluetooth status (e.g., blinking for pairing, steady for connection).

Battery Power:

  1. Add a rechargeable LiPo battery and charging circuit (like TP4056) to make it portable.

Light Show:

  1. Synchronize RGB LEDs with music to create a mini light show!
And Most importantly ANYTHING can be a bluetooth speaker as long as all the components fit
- me probably.
GodSpeed Creators.