10-22-2024, 01:12 AM
Code:
/*
ESP32 SD I2S Music Player
esp32-i2s-sd-player.ino
Plays MP3 file from microSD card
Uses MAX98357 I2S Amplifier Module
Uses ESP32-audioI2S Library - https://github.com/schreibfaul1/ESP32-audioI2S
*/
// Include required libraries
#include "Arduino.h"
#include "Audio.h"
#include "SD.h"
#include "FS.h"
// microSD Card Reader connections
#define SD_CS 13
#define SPI_MOSI 12
#define SPI_MISO 10
#define SPI_SCK 11
// I2S Connections
#define I2S_DOUT 8
#define I2S_BCLK 7
#define I2S_LRC 6
// Create Audio object
Audio audio;
void setup() {
// Set microSD Card CS as OUTPUT and set HIGH
pinMode(SD_CS, OUTPUT);
digitalWrite(SD_CS, HIGH);
// Initialize SPI bus for microSD Card
SPI.begin(SPI_SCK, SPI_MISO, SPI_MOSI);
// Start Serial Port
Serial.begin(115200);
// Start microSD Card
if(!SD.begin(SD_CS))
{
Serial.println("Error accessing microSD card!");
while(true);
}
// Setup I2S
audio.setPinout(I2S_BCLK, I2S_LRC, I2S_DOUT);
// Set Volume
audio.setVolume(50);
// Open music file
audio.connecttoFS(SD,"/music.mp3");
}
void loop()
{
audio.loop();
}
arduino ino file download:
AS-mp3.zip (Size: 760 bytes / Downloads: 24)
BIN file (you can use esp32 download tool download to ESP32-S3 with address 0x0 then directly to use) download:
AS-mp3.ino.merged.zip (Size: 878.75 KB / Downloads: 27)
arduino IDE compile setting: