SD card works in setup but not in loop

Hello, I'm trying to get a hw 125 micro sd card reader to work with the spi framework. I'm allready using the default pins for a spi display. After trying around for quite a long time, i was finally able to get it to work in the setup on my az delivery esp32 wroom devboard. However, as soon as i try to use it in the loop, the sd card is not detected anymore. How can i fix this? Here the relevant code:

#define SDA_PIN 21
#define SCL_PIN 22
#define backlight 2
#define SD_CS 27
#define SD_MOSI 33
#define SD_MISO 25
#define SD_SCK 32

File myFile;

SPIClass SDspi(VSPI);

void setup()
{
    Serial.begin( 9600 ); /* prepare for possible serial debug */

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

    // Set the time manually to 12:00:00 on 1st January 2022
  setTime(12, 0, 0, 1, 1, 2022);

 SDspi.begin(SD_SCK, SD_MISO, SD_MOSI, SD_CS);
   pinMode(SD_CS, OUTPUT);

// sd karte initializiert

   if (!SD.begin(SD_CS, SDspi )) {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization sd done.");

  // open the file for write at end like the "Native SD library"
  myFile = SD.open("/test.txt", FILE_WRITE);
  myFile.println("Hallo");
  myFile.close(); 
}
void loop()
{
// Open the file for write at end
    myFile = SD.open("/test.txt", FILE_WRITE);
    delay(10);

    // If the file opened okay, write to it
    if (myFile) {
      myFile.println(pressure);
      myFile.print("; ");
      myFile.print(temperature);
      myFile.print("; ");
      myFile.print(humidity);
      myFile.print(hour());
      myFile.print(":");
      myFile.print(minute());
    myFile.print(":");
    myFile.print(second());
    myFile.print(" ");
    myFile.print(day());
    myFile.print("/");
    myFile.print(month());
    myFile.print("/");
    myFile.print(year());
      myFile.close(); // Close the file
    } else {
      // If the file didn't open, print an error
      Serial.println("error opening test.txt");
    }
}

Again, the setup works, also every other piece of code seems to work. But from the myFile part in the loop i get this error:

[ 10388][E][sd_diskio.cpp:199] sdCommand(): Card Failed! cmd: 0x11
[ 10689][E][sd_diskio.cpp:199] sdCommand(): Card Failed! cmd: 0x0d
[ 10689][E][sd_diskio.cpp:621] ff_sd_status(): Check status failed
[ 10999][E][sd_diskio.cpp:199] sdCommand(): Card Failed! cmd: 0x00
[ 11303][E][sd_diskio.cpp:199] sdCommand(): Card Failed! cmd: 0x00
[ 11607][E][sd_diskio.cpp:199] sdCommand(): Card Failed! cmd: 0x00
[ 11607][E][vfs_api.cpp:332] VFSFileImpl(): fopen(/sd/test.txt) failed
error opening test.txt

I don't know what to do, I don't get why just adding a sd card has to feel like such a unsolveable challenge. thanks for your help

You have an incomplete sketch. What are the sources you copied?

I didn't want to write my complete code, since the sd businesslogic is only a small part

Try to write a minimum compilable sketch for your devices that can repeat the errors.

Your code does not compile.
I think there is a need to include a library.

Maybe SD.h????

And

sketch.ino: In function 'void setup()':
sketch.ino:23:3: error: 'setTime' was not declared in this scope
setTime(12, 0, 0, 1, 1, 2022);

And

sketch.ino: In function 'void loop()':
sketch.ino:49:22: error: 'pressure' was not declared in this scope
myFile.println(pressure);

sketch.ino:51:20: error: 'temperature' was not declared in this scope
myFile.print(temperature);

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