Weird Issue with SD Card Mount

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
}

My guess would be that you need to control the CS (SS) pins of both devices. When you have more than one device on the SPI bus, the CS pin is used to signal which device to communicate with. CS pins should be set to output and held high and when the program needs to communicate eith a device, its corresponding CS pin will be brought low \for the duration of the communication.

I have not looked in detail at the libraries to see how the CS pin is managed, but my guess is that both devices might be trying to respond to the SPI bus at the same time.

Hello, sorry what do you mean by both devices? There is only one device on the SPI bus and that is the SD card. Maybe I had confused you?

Sorry, my bad. I thought that the MAX98357A audio device was also connected via the SPI bus, but it isn't.

I say this with a grain of salt, but it seems to help.

After a reset, but not after a long time with power off, the CS pin can come up in the wrong state. In my case, the SD.begin() would fail in one reboot after another.

Now I explicitly set the state of the CS pin right before SD.begin(). The digitalWrite line seems to be important.

  pinMode(SD_CS, OUTPUT);
  digitalWrite(SD_CS, HIGH);

Again, a grain of salt because I still occasionally get a failure after reboot, but it is much less frequent now.