Hi everyone, need some help debugging a rather weird issue. My sketch (code below) is pretty simple. It uses the ESP32-AudioI2S library, FS, and SD libraries. What it does is reads an mp3 file from SD card, decodes it, and then plays it over I2S. I have a MAX98357A ingesting the I2S stream and converting it to audio for a speaker.
I got the sound file to play perfectly. But I'm having a very weird issue with the SD card mounting. The SD card seems to fail to mount on every other reset. I will reset it and mp3 file plays perfectly. Reset again and SD card mount fails. Reset one more time and mp3 file plays perfectly. This happens in an almost perfect sequence. I hit reset over 20 times in a row and this sequence of SD card mount fail, and then not fail, repeats over and over. It's bizarre to me and I can't figure out why. Was hoping one of you might be able to help. Thanks!!
I'm using the Arduino Nano ESP32 and Arduino IDE 2.2.2 if that helps.
#include "FS.h"
#include "SD.h"
#include "Audio.h"
// I2S pins configuration
int bClkPin = 11; // A4 on silk
int wClkPin = 12; // A5 on silk
int dOutPin = 13; // A6 on silk
Audio audio; // Create an audio object
void setup() {
Serial.begin(115200);
delay(2000); // 2seconds seems to be necessary for serial port to initialize and work
// Initialize SD
if (!SD.begin()) {
Serial.println("SD Card Mount Failed");
return;
}
File file = SD.open("/cardinal_chirp.mp3"); // Replace with your MP3 file path
if (!file) {
Serial.println("Failed to open file for reading");
return;
}
// Set I2S pins
audio.setPinout(bClkPin, wClkPin, dOutPin);
// Set volume (range from 0 to 21)
audio.setVolume(10); // Example: Set volume level to 10
// Start playing
if (audio.connecttoFS(SD, "/cardinal_chirp.mp3")) {
Serial.println("MP3 playback started");
} else {
Serial.println("Failed to start MP3 playback");
}
}
void loop() {
audio.loop(); // Keep the audio playback active
}