ESP32-Won't open File

Hey, can't open my File. Connection to the SD-Card is there so the wiring should be ok.

I try to open, write and directly close the File. Even tested it, when I created the File from my PC.
MC => ESP32

Code:

#include <SPI.h>
#include <SD.h>

File thisFile;

void SD_Setup(int sd_Connection)
{
  if(SD.begin(sd_Connection))
  {
    Serial.println("Connected to SD");
  }
  else{
    Serial.println("Connection to SD failed");
  }
}

bool SD_WriteFile(String file, String data)
{
  // Open File for Write
  Serial.println("OF 1");
  thisFile = SD.open(file,FILE_WRITE);
  Serial.println("OF 2");

  if(!thisFile) return false;  // Return when File not opened

  // Write to File
  Serial.println("WF 1");

  thisFile.print(data);
  
  Serial.println("WF 2");

  thisFile.close();
  Serial.println("CF");
  return true;
}

Calling SD_Setup from the Setup and directly after that SD_WriteFile.


  // Setup SD-Card
  SD_Setup(SD_CardSelect);

  //Testing
  SD_WriteFile("Data.txt", "TestData");
  
  Serial.println("Started LUX, now prepare RUN");

This is the circuit:


EDIT: The Code returns out at the if(!thisFile) check

I have same issue, the same module with the same sd card works normally with my Arduino Nano.
I am using ESP-32 S3 Board T-Display from Lilygo.
I have following connection

// SS: 10 SPI_CS
// MOSI: 11 SPI_D
// SCK: 12 SPI_CLK
// MISO: 13 SPI_Q

The result

Initializing SD card...SD initialization done.

Card type:         SDHC
Volume type is:    FATCard size:  15548285.00
Total bytes: 15540256768
Used bytes: 3014656
error opening ECG_DATA.TXT

Oh, seems like I forgott to mark it as solved. My Problem was, that i didn't tell the ESP to check in the root location (same as all the tutorials I found).

I solved it by changing

  thisFile = SD.open(file,FILE_WRITE);

to

file = "/" + file;
  thisFile = SD.open(file,FILE_WRITE);

Can you confirm that?

1 Like

Oh, I actually figured that out too :slight_smile: thanks for answering tho.

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