ESP32-PRO with arduino: using an SD card

Started by valhalla, November 21, 2024, 09:48:27 AM

Previous topic - Next topic

valhalla

I'm trying to use the SD card slot on the ESP32-PRO; I'm using version 3.0.7 of the ESP package (but I've also had the same issues with previous versions).

This is the basic minimal code I'm using for these tests

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

File logfile;

void setup() {
    Serial.begin(115200);
    Serial.println("Starting");
}

void loop() {
    Serial.println("Starting loop");
    // I'm starting the SD in the loop so that I can see the serial prints in the monitor
    if (!logfile) {
        if (!SD.begin()) {
            Serial.println("SD could not begin, do we have any error?");
        } else {
            logfile = SD.open("test.log", FILE_WRITE);
        }
    }
    if (logfile) {
        logfile.write('t');
        logfile.flush();
        Serial.println("Wrote to test.log");
    }
    delay(10000);
}

And, with Core Debug Level: Error they result in the following message:

08:21:29.645 -> Starting loop
08:21:29.942 -> [202764][E][sd_diskio.cpp:200] sdCommand(): Card Failed! cmd: 0x00
08:21:29.942 -> [202770][E][sd_diskio.cpp:761] sdcard_mount(): f_mount failed: (3) The physical drive cannot work
08:21:30.273 -> [203079][E][sd_diskio.cpp:200] sdCommand(): Card Failed! cmd: 0x00
08:21:30.273 -> SD could not begin, do we have any error?

From file:///home/valhalla/Downloads/ESP32-PRO_Rev_B1-3.pdf I believe that the SD CS pin is connected to pin 13, so I also tried to change the above code to SD.begin(13), but I had the same error, and the same happened by adding the following code to esplicitely select the pins:

SPI.begin(14, 2, 15, 13);
SPI.setDataMode(SPI_MODE0);

any hints on what I could be doing wrong?