IOT

DIY ESP32 Music Audio MP3 Player with OLED Display

This project will teach you how to construct your own ESP32 Audio Music MP3 Player. The straightforward Do-It-Yourself Music Player project requires a Customized ESP32 Board and an Audio Expansion Shield. The Audio expansion board based on the UDA1334ATS Audio Amplifier IC, a Digital-to-Analog Converter IC, aids in the encoding and decoding of the Audio Signal. The custom board can assist you in implementing audio functionalities for ESP applications that use an I2S audio library.

The ESP32 music player operates on a 5V Supply and may be powered by either a 5V DC Adapter or a Power Bank. The device is extremely portable and small. Simply transfer the.mp3 or.wav files to the SD card. The SD Card can then be placed into the Audio expansion Shield’s SD Card slot. After code upload, the Audio Player is ready for use. The finest feature of this ESP32 MP3 Audio Music player is its 3.5mm Audio Jack, which allows you to utilize headphones or a speaker. In addition, there is a 3D casing for this project in which all the electronic components can be housed.

Hardware Required

S.N.COMPONENTS NAMEDESCRIPTION
1Customized ESP32 BoardMakePython ESP32 Board
2Audio Expansion BoardMakePython Audio Expansion
33D CasingESP32 Audio Fixture Kit
4SpeakersSpeaker with 3.5mm Audio Jack
55V Power SupplyPower Bank

Customized ESP32 Board

Here, we need a customized ESP32 Board. So I purchased an ESP32 Board specially designed for this project, i.e. MakePython ESP32 Board from MakerFabs.

MakePython ESP32 Board utilises the same ESP32 Chip manufactured by espressif. This board contains an extra 1.3″ I2C OLED Display, which gives it an edge over the standard ESP32 Board.

MakePython ESP32 is a robust, generic Wifi+ BLE MCU module that enables programming in both C/C++ and Python. It is simple to learn and use, as it is breadboard-compatible and the pins are explained in depth. Arduino IDE is utilized to program MakePython ESP32.

MakePython ESP32 Pins

The complete pin description and detail about this board can be found here: MakePython ESP32 Description.

Audio Expansion Board

The Audio extension board is based on the UDA1334ATS audio DAC, allowing ESP projects to integrate audio functionality. It interfaces with ESP32 through I2S and outputs audio of rather high quality. It could be an MP3 Audio file with ESP32 decoding support.

There are two navigator buttons suited for apps like MP3 players: Previous/Next/Confirm and Audio adjusting. The presence of a Micro-SD slot and audio jack on the board makes it suitable for usage as a genuine MP3 player.

Features

  • NXP Low Power Audio DAC: uDA1334
  • Integrated Digital Filter Plus DAC
  • Supports Sample Frequencies from 8 to 100 kHz
  • Automatic System Clock Versus Sample Rate Detection
  • Low Power Consumption
  • I2S-bus and LSB-justified Format Compatible
  • Digital de-emphasis for 44.1 kHz Sampling Rate
  • Support Micro SD Card

3D Casing

The 3D Casing for ESP32 Music Audio MP3 Player is essential to give the project a professional appearance. It also makes the device extremely portable and compact.

The 3D Casing was created with 3D Modeling software. The STL files can be used to print this three-dimensional casing. Here is where the STL files for 3D printing may be downloaded:3D Case STL File.

Assembling & Preparing The Hardware

Once all the constituent components are available, the ESP32 Music Player hardware must be prepared and assembled. We will begin by installing the Audio Expansion Board’s enclosure.

Then the ESP32 Board can be inserted into the expansion shield.

The front and base side cover will then be attached to the board.

Then, we will use the screw to tighten the knots and bolts.

Then, the stand will be assembled by connecting the two distinct 3D-printed sections at right angles. And they are screwed as well.

And at last we will connect the stand and Cuboid part into a single unit.

Therefore, our final device is now complete. We must now attach the speaker to the circuit board. To accomplish this, connect the speaker’s Audio jack to the 3.5mm audio connector on the Audio Expansion Board.

ESP32 MP3 Player

Required Arduino Library Installation

Before beginning the coding portion of the project, we must install the required libraries. The necessary libraries are listed below.

1. ESP32-audioI2S Library

This library plays MP3 and WAV files from an SD card using external hardware and I2S. Decoders for HELIX-mp3 and -aac are also included. AudioI2S is compatible with PCM1502A and UDA1334A (Adafruit I2S Stereo Decoder Breakout Board). It additionally plays icy-streams and GoogleTTS. The library compiles easy using Arduino IDE.

Download: ESP32-audioI2S Library

2. OLED Libraries

We need two OLED Display Libraries. Adafruit SSD1306 is a library based on SSD1306 drivers for our Monochrome OLEDs. The Adafruit GFX Library is the primary graphics library for all of our displays, offering a standard set of graphics primitives (points, lines, circles, etc.).

Download: SSD1306 Library & Adafruit GFX Library

Source Code/Program

After libraries have been added to the Arduino IDE, the following code can be copied and uploaded to the ESP32 Board.

While uploading the program, please rotate the switch next to the 3.5mm Audio interface to the Audio socket.

#include “Arduino.h”
//#include “WiFiMulti.h”
#include “Audio.h”
#include “SPI.h”
#include “SD.h”
#include “FS.h”
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

//SD Card
#define SD_CS 22
#define SPI_MOSI 23
#define SPI_MISO 19
#define SPI_SCK 18

//Digital I/O used //Makerfabs Audio V2.0
#define I2S_DOUT 27
#define I2S_BCLK 26
#define I2S_LRC 25

//SSD1306
#define MAKEPYTHON_ESP32_SDA 4
#define MAKEPYTHON_ESP32_SCL 5
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)

Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

//Button
const int Pin_vol_up = 39;
const int Pin_vol_down = 36;
const int Pin_mute = 35;

const int Pin_previous = 15;
const int Pin_pause = 33;
const int Pin_next = 2;

Audio audio;

struct Music_info
{
String name;
int length;
int runtime;
int volume;
int status;
int mute_volume;
} music_info = {“”, 0, 0, 0, 0, 0};

String file_list[20];
int file_num = 0;
int file_index = 0;

void setup()
{
//IO mode init
pinMode(Pin_vol_up, INPUT_PULLUP);
pinMode(Pin_vol_down, INPUT_PULLUP);
pinMode(Pin_mute, INPUT_PULLUP);
pinMode(Pin_previous, INPUT_PULLUP);
pinMode(Pin_pause, INPUT_PULLUP);
pinMode(Pin_next, INPUT_PULLUP);

//Serial
Serial.begin(115200);

//LCD
Wire.begin(MAKEPYTHON_ESP32_SDA, MAKEPYTHON_ESP32_SCL);
// SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C))
{ // Address 0x3C for 128×32
Serial.println(F(“SSD1306 allocation failed”));
for (;;)
; // Don’t proceed, loop forever
}
display.clearDisplay();
logoshow();

//SD(SPI)
pinMode(SD_CS, OUTPUT);
digitalWrite(SD_CS, HIGH);
SPI.begin(SPI_SCK, SPI_MISO, SPI_MOSI);
SPI.setFrequency(1000000);
if (!SD.begin(SD_CS, SPI))
{
Serial.println(“Card Mount Failed”);
lcd_text(“SD ERR”);
while (1)
;
}
else
{
lcd_text(“SD OK”);
}

//Read SD
file_num = get_music_list(SD, “/”, 0, file_list);
Serial.print(“Music file count:”);
Serial.println(file_num);
Serial.println(“All music:”);
for (int i = 0; i < file_num; i++)
{
Serial.println(file_list[i]);
}


//Audio(I2S)
audio.setPinout(I2S_BCLK, I2S_LRC, I2S_DOUT);
audio.setVolume(21); // 0…21
open_new_song(file_list[file_index]);
print_song_time();
}

uint run_time = 0;
uint button_time = 0;

void loop()
{
audio.loop();
if (millis() – run_time > 1000)
{
run_time = millis();
print_song_time();
display_music();
}

if (millis() – button_time > 300)
{
//Button logic
if (digitalRead(Pin_next) == 0)
{
Serial.println(“Pin_next”);
if (file_index < file_num – 1)
file_index++;
else
file_index = 0;
open_new_song(file_list[file_index]);
print_song_time();
button_time = millis();
}
if (digitalRead(Pin_previous) == 0)
{
Serial.println(“Pin_previous”);
if (file_index > 0)
file_index–;
else
file_index = file_num – 1;
open_new_song(file_list[file_index]);
print_song_time();
button_time = millis();
}
if (digitalRead(Pin_vol_up) == 0)
{
Serial.println(“Pin_vol_up”);
if (music_info.volume < 21)
music_info.volume++;
audio.setVolume(music_info.volume);
button_time = millis();
}
if (digitalRead(Pin_vol_down) == 0)
{
Serial.println(“Pin_vol_down”);
if (music_info.volume > 0)
music_info.volume–;
audio.setVolume(music_info.volume);
button_time = millis();
}
if (digitalRead(Pin_mute) == 0)
{
Serial.println(“Pin_mute”);
if (music_info.volume != 0)
{
music_info.mute_volume = music_info.volume;
music_info.volume = 0;
}
else
{
music_info.volume = music_info.mute_volume;
}
audio.setVolume(music_info.volume);
button_time = millis();
}
if (digitalRead(Pin_pause) == 0)
{
Serial.println(“Pin_pause”);
audio.pauseResume();
button_time = millis();
}
}

if (Serial.available())
{
String r = Serial.readString();
r.trim();
if (r.length() > 5)
{
audio.stopSong();
open_new_song(file_list[0]);
print_song_info();
}
else
{
audio.setVolume(r.toInt());
}
}
}

void open_new_song(String filename)
{
music_info.name = filename.substring(1, filename.indexOf(“.”));
audio.connecttoFS(SD, filename);
music_info.runtime = audio.getAudioCurrentTime();
music_info.length = audio.getAudioFileDuration();
music_info.volume = audio.getVolume();
music_info.status = 1;
Serial.println(“**********start a new sound************”);
}

void display_music()
{
int line_step = 24;
int line = 0;
char buff[20];
;
sprintf(buff, “%d:%d”, music_info.runtime, music_info.length);

display.clearDisplay();

display.setTextSize(2); // Normal 1:1 pixel scale
display.setTextColor(SSD1306_WHITE); // Draw white text

display.setCursor(0, line); // Start at top-left corner
display.println(music_info.name);
line += line_step;

display.setCursor(0, line);
display.println(buff);
line += line_step;

sprintf(buff, “V:%d”,music_info.volume);

display.setCursor(0, line);
display.println(buff);
line += line_step;

display.setCursor(0, line);
display.println(music_info.status);
line += line_step;

display.display();
}

void logoshow(void)
{
display.clearDisplay();

display.setTextSize(2); // Normal 1:1 pixel scale
display.setTextColor(SSD1306_WHITE); // Draw white text
display.setCursor(0, 0); // Start at top-left corner
display.println(F(“ESP32”));
display.setCursor(0, 20); // Start at top-left corner
display.println(F(“MUSIC”));
display.setCursor(0, 40); // Start at top-left corner
display.println(F(“PLAYER”));
display.display();
delay(2000);
}

void lcd_text(String text)
{
display.clearDisplay();

display.setTextSize(2); // Normal 1:1 pixel scale
display.setTextColor(SSD1306_WHITE); // Draw white text
display.setCursor(0, 0); // Start at top-left corner
display.println(text);
display.display();
delay(500);
}

void print_song_info()
{
Serial.println(“***********************************”);
Serial.println(audio.getFileSize());
Serial.println(audio.getFilePos());
Serial.println(audio.getSampleRate());
Serial.println(audio.getBitsPerSample());
Serial.println(audio.getChannels());
Serial.println(audio.getVolume());
Serial.println(“***********************************”);
}

void print_song_time()
{
//Serial.println(audio.getAudioCurrentTime());
//Serial.println(audio.getAudioFileDuration());
music_info.runtime = audio.getAudioCurrentTime();
music_info.length = audio.getAudioFileDuration();
music_info.volume = audio.getVolume();
}

int get_music_list(fs::FS &fs, const char *dirname, uint8_t levels, String wavlist[30])
{
Serial.printf(“Listing directory: %s\n”, dirname);
int i = 0;

File root = fs.open(dirname);
if (!root)
{
Serial.println(“Failed to open directory”);
return i;
}
if (!root.isDirectory())
{
Serial.println(“Not a directory”);
return i;
}

File file = root.openNextFile();
while (file)
{
if (file.isDirectory())
{
}
else
{
String temp = file.name();
if (temp.endsWith(“.wav”))
{
wavlist[i] = temp;
i++;
}
else if (temp.endsWith(“.mp3”))
{
wavlist[i] = temp;
i++;
}
}
file = root.openNextFile();
}
return i;
}

//**********************************************
// optional
void audio_info(const char *info)
{
Serial.print(“info “);
Serial.println(info);
}
void audio_id3data(const char *info)
{ //id3 metadata
Serial.print(“id3data “);
Serial.println(info);
}

void audio_eof_mp3(const char *info)
{ //end of file
Serial.print(“eof_mp3 “);
Serial.println(info);
file_index++;
if (file_index >= file_num)
{
file_index = 0;
}
open_new_song(file_list[file_index]);
}
void audio_showstation(const char *info)
{
Serial.print(“station “);
Serial.println(info);
}
void audio_showstreaminfo(const char *info)
{
Serial.print(“streaminfo “);
Serial.println(info);
}
void audio_showstreamtitle(const char *info)
{
Serial.print(“streamtitle “);
Serial.println(info);
}
void audio_bitrate(const char *info)
{
Serial.print(“bitrate “);
Serial.println(info);
}
void audio_commercial(const char *info)
{ //duration in sec
Serial.print(“commercial “);
Serial.println(info);
}
void audio_icyurl(const char *info)
{ //homepage
Serial.print(“icyurl “);
Serial.println(info);
}
void audio_lasthost(const char *info)
{ //stream URL played
Serial.print(“lasthost “);
Serial.println(info);
}
void audio_eof_speech(const char *info)
{
Serial.print(“eof_speech “);
Serial.println(info);
}

ESP32 Music Player Test & Demo

After the code has been uploaded to the ESP32 Board, the Device can be tested by playing music. But first, copy a few of MP3 files onto the Micro-SD Card and place it into the Audio Expansion SD-Card Slot.

ESP32 Based MP3 Player

Now your ESP32 MP3 Player is complete. Thus, it is time to play music. The Speaker stereo or earphone should then be inserted into a 3.5mm audio jack. The power bank can be used to power both the ESP32 Audio Player Board and the Speaker.

After powering up, the device will begin playing music.

ESP32 Music Player

The OLED Display panel displays the song’s name, the duration of the song, and the amount of time it has been playing.

You can move the lower side switch left and right to advance or rewind through songs. Press the button inward to pause. Likewise, the left switch is Volume Control & Mute. You may move it up to increase the volume, and down to decrease it. Additionally, you can press in to mute.

Conclusion

Hope this blog helps you to understand how to design DIY ESP32 Music Audio MP3 Player with OLED Display. We, MATHA ELECTRONICS will come back with more informative blogs.

Leave a Reply

Your email address will not be published.