ESP32s can’t detect/mount microSD card - Fix

// Libraries for SD card
#include "FS.h"
#include <SD.h>
//#include "mySD.h"
#include <SPI.h>


// Define CS pin for the SD card module
#define SD_MISO     2
#define SD_MOSI     15
#define SD_SCLK     14
#define SD_CS       13
SPIClass sdSPI(VSPI);

String dataMessage;

void setup() {
  // Start serial communication for debugging purposes
  Serial.begin(115200);
 
  // Initialize SD card
  //SD.begin(SD_CS);  
  sdSPI.begin(SD_SCLK, SD_MISO, SD_MOSI, SD_CS);
  if(!SD.begin(SD_CS, sdSPI)) {
    Serial.println("Card Mount Failed");
    return;
  }
  Serial.println("1");
  uint8_t cardType = SD.cardType();
  if(cardType == CARD_NONE) {
    Serial.println("No SD card attached");
    return;
  }
  Serial.println("Initializing SD card...");
  if (!SD.begin(SD_CS)) {
    Serial.println("ERROR - SD card initialization failed!");
    return;    // init failed
  }
  Serial.println("2");
  // If the data.txt file doesn't exist
  // Create a file on the SD card and write the data labels
  File file = SD.open("/data1.txt");
  if(!file) {
    Serial.println("File doens't exist");
    Serial.println("Creating file...");
    writeFile(SD, "/data1.txt", "Reading ID, Date, Hour, Temperature \r\n");
  }
  else {
    Serial.println("File already exists");  
  }
  file.close();
  logSDCard();
  
}

void loop() {
  // The ESP32 will be in deep sleep
  // it never reaches the loop()
}

// Write the sensor readings on the SD card
void logSDCard() {
  //dataMessage = String(readingID) + "," + String(dayStamp) + "," + String(timeStamp) + "," + 
  //              String(temperature) + "\r\n";
  dataMessage = "Hello World \n";
  Serial.print("Save data: ");
  Serial.println(dataMessage);
  appendFile(SD, "/data1.txt", dataMessage.c_str());
}

// Write to the SD card (DON'T MODIFY THIS FUNCTION)
void writeFile(fs::FS &fs, const char * path, const char * message) {
  Serial.printf("Writing file: %s\n", path);

  File file = fs.open(path, FILE_WRITE);
  if(!file) {
    Serial.println("Failed to open file for writing");
    return;
  }
  if(file.print(message)) {
    Serial.println("File written");
  } else {
    Serial.println("Write failed");
  }
  file.close();
}

// Append data to the SD card (DON'T MODIFY THIS FUNCTION)
void appendFile(fs::FS &fs, const char * path, const char * message) {
  Serial.printf("Appending to file: %s\n", path);

  File file = fs.open(path, FILE_APPEND);
  if(!file) {
    Serial.println("Failed to open file for appending");
    return;
  }
  if(file.print(message)) {
    Serial.println("Message appended");
  } else {
    Serial.println("Append failed");
  }
  file.close();
}

This code has actually fixed my issue (ESP32s can’t detect/mount microSD card ). May someone can try it when they face it.

Similar one

Hi @caramel_beast

try this Lib : #include "SD_MMC.h" // SD Card ESP32

By default, the pin mapping for SPI is:

SPI MOSI MISO CLK CS
VSPI GPIO 23 GPIO 19 GPIO 18 GPIO 5
HSPI GPIO 13 GPIO 12 GPIO 14 GPIO 15

MicroSD Card Connections

The following pins are used to interface with the microSD card when it is on operation.

MicroSD card ESP32
CLK GPIO 14
CMD GPIO 15
DATA0 GPIO 2
DATA1 / flashlight GPIO 4
DATA2 GPIO 12
DATA3 GPIO 13

MicroSD_Pinout

RV mineirin

FYI Which pins to use for SD card acces? (SD/SPI vs HSPI/JTAG/SD vs VSPI/SPI-Mode) - ESP32 Forum

Hi @ruilviana ,
Thank you for your reply. I am using LILYGO TTGO T4 module. The pins I am using is right, which is assigned to the SD_MISO, SD_MOSI, SD_SCLK, SD_CS. The code is working for me. I have put this code in the community for someone who may face the same issue of not detecting SDcard in the future.