[SOLVED] Open file fails on SD-card with ESP32

I connected an SD-card to my ESP32 WROOM 38 pins.

I can access the card, read the disc information, but can't open a file.

Here my code

`/*
PROGRAMNAME:
Name         SD_card_01.ino
Version:     01
Author:      xxx
Created:     4-5-2015
Edited by:
Modified:    06-04-2022

WHAT DOES THIS PROGRAM:
Demo initialize, open file, write to file, delete file, check if file exists

INFO:
https://docs.arduino.cc/learn/programming/sd-guide
https://www.arduino.cc/reference/en/libraries/sd/
https://www.mischianti.org/2021/03/28/how-to-use-sd-card-with-esp32-2/

HARDWARE:
-  ESP32 WROOM, 38 pins mounted in CV-logger
-  SD card attached to SPI bus as follows:
   MOSI - pin P23
   MISO - pin P19 + 10k puillup to .3 V
   CLK  - pin P18
   CS   - pin P5

TESTED ON:
IDE 1.8.19 and ESP32 
 */

//====================================================================
//Inlcude libraries
#include <SD.h>  //Version 1.2.4
#include <SPI.h>

const int chipSelect = 5;

//void printDirectory(File dir, int numTabs);

File myFile;
int n;
// set up variables using the SD utility library functions:
//Sd2Card card;   //Removed 20220406
//SdVolume volume;
//SdFile root;

char chrFilename[] = "00000000.csv";

//====================================================================
void setup(){
// Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
      ; // wait for serial port to connect. Needed for native USB port only
      }

  Serial.print("Initializing SD card...");

  if (!SD.begin(chipSelect)) {
    Serial.println("initialization failed!");
    while (1);
    }
  Serial.println("initialization done.");  int i;

  // print the type of card
  Serial.println();
  Serial.print("Card type:         ");
  switch (SD.cardType()) {
    case CARD_NONE:
      Serial.println("NONE");
      break;
    case CARD_MMC:
      Serial.println("MMC");
      break;
    case CARD_SD:
      Serial.println("SD");
      break;
    case CARD_SDHC:
      Serial.println("SDHC");
      break;
    default:
      Serial.println("Unknown");
    }

  // print the type and size of the first FAT-type volume
  uint32_t volumesize;
  Serial.print("Volume type is:    FAT");
  //Serial.println(SDFS.usefatType(), DEC);
 
  Serial.print("Card size:  ");
  Serial.println((float)SD.cardSize()/1000);
 
  Serial.print("Total bytes: ");
  Serial.println(SD.totalBytes());
 
  Serial.print("Used bytes: ");
  Serial.println(SD.usedBytes());
 
  //File dir =  SD.open("/");
  //printDirectory(dir, 0);
  
  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  myFile = SD.open("test.txt", FILE_WRITE);
  
   // if the file opened okay, write to it:
  if (myFile) {
      Serial.print("Writing to test.txt...");
      myFile.println("testing 1, 2, 3.");
      // close the file:
      myFile.close();
      Serial.println("done.");
      } else {
      // if the file didn't open, print an error:
      Serial.println("error opening test.txt");
      }
 
  // re-open the file for reading:
  myFile = SD.open("test.txt");
  if (myFile) {
      Serial.println("test.txt:");
   
      // read from the file until there's nothing else in it:
      while (myFile.available()) {
          Serial.write(myFile.read());
          }
      // close the file:
      myFile.close();
      } else {
      // if the file didn't open, print an error:
      Serial.println("error opening test.txt");
      }
}
 
void loop() {
  // nothing happens after setup
}
     `

@ArduinoStarter1, your topic has been moved to a more suitable location on the forum. Installation and Troubleshooting is not for problems with (nor for advise on) your project :wink: See About the Installation & Troubleshooting category.

I use append instead of open.

appendFile(SD, "/data.txt", toFile.c_str() + '\n');

Is the call to the append function.


void appendFile(fs::FS &fs, const char * path, const char * message) {
  //log_i("Appending to file: %s\n", path);
  File file = fs.open(path, FILE_APPEND);
  if (!file)
  {
    log_i("Failed to open file for appending");
    return;
  } else {
    file.print(message);
    file.close();
  }
}

the append function.

If the file has not been created open does not create the file. On the other hand, with append, if the file does not exist, creates the file then opens the file.

It seemed that my SD-card was corrupt.
I formatted the SD-card and the problem disappeared.

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