DIY music players are fun, we’ve built some before using an Arduino and a dedicated MP3 module. Now we’re building an interesting audio player using the ESP32 where you can play sound effects by simply connecting an extra speaker to the ESP32. Here we will use LM386 and a speaker with ESP32 to play music files. The audio output may not be large, but this application shows the ability of the ESP32 board to play audio files.

required components


LM386 Amplifier Module

8 ohm speaker


circuit schematic

The circuit diagram of the ESP32 music player is as follows-

To play sound on the ESP32, we need a speaker. The speakers are connected through the LM386 audio amplifier module. The Amplifier Module’s Vcc and GND pins are connected to the ESP32’s VIN and GND pins, and the Amplifier Module’s IN pin is connected to the ESP32’s GPIO 25 pin. GPIO 25 is one of 2 DAC (Digital to Analog Converter) pins.

The complete setup for the ESP32 music player looks like this:

LM386 is an audio amplifier IC, you can learn more about LM386 through the following link.

prepare audio file

To play audio sound on ESP32 board, we need audio files in .wav format, because ESP32 can only play audio files in .wav format.

So the first step is to get the file you want to play on the ESP32. After that, download and install the Audacity Audio Editor App. This application will be used to change the file type, sample rate and other properties.

Once installed, open the Audacity app, then go to File > Open and select the audio file you want to edit.

The first thing to change is the sample rate. For playing audio files on the ESP32, a sample rate of 8000 to 16000 would be fine because the ESP32 doesn’t have much memory. Therefore, change the Project Rate to 16000.

After that, navigate to File > Export > Export as WAV to save the audio file in .wav format.

Now in the next window, select WAV (Microsoft) as the file type, Unsigned 8-bit PCM as the encoding format and click save.

Now we need a Hex Editor application to generate Hex code for audio files. So, download and install Hex Editor (HxD) from the given link. After that, open the application and go to File > Open and open the .wav file. This will generate hex code for the .wav file.

Then use ctrl+A to select the complete code and go to Edit > Copy As > C to copy the code in C language format.

Then paste this code into the Arduino IDE.

code description

The complete code for the contactless ESP32 audio player is given at the end of the page. Here we will explain some important parts of the code. In this program we will use the XT DAC audio library from XTronical. The XT DAC audio library can be downloaded from here.

So, as usual, start the code by including all the required libraries. The SoundData.h file contains the hex code for the .Wav file to play.

#include "SoundData.h"#include "XT_DAC_Audio.h"

In the next line, create an object of type XT_Wav_Class, which is used by the DAC audio class to pass the .wav data as a parameter. DacAudio, the main player class object, where 25 is the DAC pin number to which the amplifier pins are connected.

XT_Wav_Class sound (sample); XT_DAC_Audio_Class DacAudio(25,0);

In the setup() function, initialize the serial monitor at a baud rate of 115200 for debugging.

void set() { serial.start(115200);

In the loop() function, fill the sound buffer with data and check if the sound is playing, if not, play the sound in a loop.

void loop() { DacAudio.FillBuffer(); if ( == false) DacAudio.Play(&Sound); Serial.println(DemoCounter++); }

Test the ESP32 Audio Player

Once the code is ready, connect the speaker to GPIO 25 of the ESP32 via an LM386 or any other amplifier module. Now connect the ESP32 to the laptop and upload the code. After uploading the code, the ESP32 starts playing audio. The sound quality can be changed using the potentiometer of the amplifier module.

#include “SoundData.h”
#include “XT_DAC_Audio.h”
XT_Wav_Class sound (sample);
XT_DAC_Audio_Class DacAudio(25,0);
uint32_t DemoCounter=0;
void set() {
void loop() {
if (sound. play == false)

Leave a Reply

Your email address will not be published.