How to play MP3’s and other file formats on your ESP32 from an SD Card. All you need to know to make a simple music player. Code shown in video is below.
Links:
Github page for the library used : https://github.com/schreibfaul1/ESP32-audioI2S
Affiliate links for the items shown: Clicking these costs you the same price but gives me a small commission, please consider supporting this channel by using them 🙂
MAX98357A : https://amzn.to/3eovdrd
(You will need two of these for stereo sound)
ESP32 : https://amzn.to/2Xzhc3k
Breadboards: https://amzn.to/2THZTvy
Speakers : https://amzn.to/2zBhK0F
16Gb SanDisk SD card : https://amzn.to/2XH1sLA
SD Card Reader, note this is the only one I could quickly find that said it supported 3.3v and it’s for the normal size SD cards not Micro, you could always by a micro one and do my hack
https://amzn.to/2ApOppK
The Demo Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
// will play many audio file formats, mp3,aac,flac etc. // See github page : https://github.com/schreibfaul1/ESP32-audioI2S #include "Arduino.h" #include "Audio.h" #include "SD.h" #include "FS.h" // Digital I/O used #define SD_CS 5 #define SPI_MOSI 23 // SD Card #define SPI_MISO 19 #define SPI_SCK 18 #define I2S_DOUT 25 #define I2S_BCLK 27 // I2S #define I2S_LRC 26 Audio audio; void setup() { pinMode(SD_CS, OUTPUT); digitalWrite(SD_CS, HIGH); SPI.begin(SPI_SCK, SPI_MISO, SPI_MOSI); Serial.begin(115200); if(!SD.begin(SD_CS)) { Serial.println("Error talking to SD card!"); while(true); // end program } audio.setPinout(I2S_BCLK, I2S_LRC, I2S_DOUT); audio.setVolume(15); // 0...21 audio.connecttoFS(SD,"/Amaze.mp3"); } void loop() { audio.loop(); } |
Thank you very much for this post.
Working good with .mp3 files.
But not working with .wav files.
What can be the reason?
Sorry for late reply, real life work has been very busy and I missed this comment. I imagine it’s the library that has a problem. Although WAV’s are the simplest files to play. I did have issues with AAC files. I would try different WAV’s, i.e. different sample rates and stereo/mono and see what happens for them.
Is there any way to pull the “audio.connecttoFS(SD,”/Amaze.mp3″);” part out of the setup block and into its own function? Basically so I can have multiple files that play on different button presses. Something like this:
Yes, just put it where you want it to play. I am working on a new library to make all this simpler. Out sometime after Christmas
Hi, I have tried the same setup and code for single speaker with MAX98357A Mono amplifier. But only noise is coming out. I have tried both .mp3 and .wav file. But it’s not working. I have tested micro sd card module which is working fine. Could you help how to play mp3 using single speaker with single MAX98357A module ?
If I remember rightly this happened to me, it was just a wiring issue, are you sure that all the wires that must be connected are and that they go to the correct ground or vcc. Some connections even though not going to the esp32 must be connected as shown.
Thanks a lot XTronical. It was connectivity issue. Also, I have connected gain pin to ground to reduce noise signal. I have one more query, have you ever tried playing mp3 file from sd card using ESP32 touch pins using the same code/library ? I tried but no sound is coming. In case, if you know, please help me to resolve the issue.
Sorry, I’ve not tried this. NOt sure what the problem could be
ok but thanks for help and such a nice explanation !
Your very welcome 🙂
After play the song i move on to next task, but it remain in a loop audio.loop(). How it exit after play the song?
Can you post your code?
Hi I getting Error compiling this code,
Audio audio; was not name a type,
can you share the Audio library that used at this code ??
Thanks in advance
If you look just under the you tube video (very near top of article) you’ll find the link to the library used on Github, it’s under a heading called links.
Hi, I had tried the same code and same connection, but there is no sound coming out. I am connecting one 4 ohm speaker to max98357a. May I know is there any requirement on the file size for mp3 ?
In addition it shows — E (53) psram: PSRAM ID read error: 0xffffffff — in the serial monitor.
I have not come across that error, it’s to do with accessing the external RAM, could be a configuration error or are you using an ESP32 with a camera or something, see this page that discusses this error
https://github.com/espressif/esp-idf/issues/6063
One speaker only per output connection to the 98357a is correct. There are no particular restrictions on file size of the mp3. It should handle mp3’s of many megs (10’s of minutes) without issue if not longer. I find when I was first setting up the 98357 that you had to ensure all wires are connected exactly as shown or it may not work