Cant open file in SD card

Hi, here is my code to use SD card module with esp32 via arduino IDE. The hardware connections used are default ones.

#include <SD.h>
String fileName;
File dataFile;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  while (!Serial);
  delay(5000);
  Serial.println("Starting the setup babe");
  //SD card initialization
  if (!SD.begin(5)) {
    Serial.println("SD card initialization failed!");
    while (1);
  }
  Serial.println("SD card is initialized");
  SDfile_GenCopy();

}

void loop() {
  // put your main code here, to run repeatedly:

}

void SDfile_GenCopy(){
  
  fileName = "nomnom.csv"; //CSV format
  dataFile = SD.open(fileName, FILE_WRITE);

  // Check if the file opened successfully
  if (dataFile) {
    Serial.println("File created: " + fileName);
  } 
  
  else {
    Serial.println("Error opening file: " + fileName);
  }
  dataFile.close();

}

Can you provide any insight as to why its showing this in output serial monitor?

Starting the setup babe

SD card is initialized

Error opening file: nomnom.csv

Welcome to the forum

Bearing in mind that the ESP32 is a 3.3V device, what voltage does the SD card module require and how is it being powered ?

Could you run the CardInfo example and post the output?

Is your card an SD or SDHC card? Is it formatted as FAT32?

Hi guys thank you so much but I have already been able to solve the problem. I was running a micro sd card module with esp32 from its V5 pin. There is also an option for the sd card module to run via 3.3v.

Now the problem was solved by just using "/" at the beginning of the string of the file name.
fileName ="/"+ fileName; is the only line I added before opening the file and it worked.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.