PlatformIO Community

Old SD-Card Project does not work anymore. Problem with the Arduino/PlatformIDCore or Lib?

Hello,
I just tryed to flash an old project which interacts with a SD-Card which was 100% working but now it does not work anymore. I found some posts about that it may happens due to a core upgrade. (https://issueexplorer.com/issue/espressif/arduino-esp32/5701). How do I know if thats the problem or something else?

This is the error message:
Card Mount Failed
or
The physical drive cannot work
Card Mount Failed

The correct formating is FAT32, isn´t it?
The card does perform otherwise normal.
I have that problem on the PlatformIO with VSCode IDE and on Arduino IDE. I already tryed an other ESP32-DevKit and an other MicroSD Cardreader,

I try to get this example code working now:

ESP32: Guide for MicroSD Card Module Arduino | Random Nerd Tutorials and I connected the Pins like on the tutorial.

With the lib LITTLEFS installed.

Do I need to do an upgrade so I can use the integrated LittleFS?

My PlatformIO is on:5.2.3
Thats what I getting when I do pio update:

Updating platformio/contrib-piohome           3.4.0 @ ~3.4.0                     [Up-to-date]
Updating platformio/tool-scons                4.40200.0 @ ~4.40200.0             [Incompatible 4.40300.1]

Platform Manager
================
Platform espressif32
--------
Updating platformio/espressif32               3.4.0                              [Up-to-date]
Updating platformio/toolchain-xtensa32        2.50200.97 @ ~2.50200.0            [Up-to-date]
Updating platformio/toolchain-xtensa32s2      1.80400.210211 @ ~1.80400.0        [Up-to-date]
Updating platformio/toolchain-esp32s2ulp      1.22851.191205 @ ~1.22851.0        [Up-to-date]
Updating platformio/toolchain-riscv-esp       1.80400.0 @ ~1.80400.0             [Up-to-date]
Updating espressif/toolchain-xtensa-esp32s2   8.4.0+2021r1 @ 8.4.0+2021r1        [Up-to-date]
Updating espressif/toolchain-riscv32-esp      8.4.0+2021r1 @ 8.4.0+2021r1        [Up-to-date]
Updating platformio/framework-espidf          3.40301.0 @ ~3.40301.0             [Up-to-date]
Updating platformio/tool-esptoolpy            1.30100.210531 @ ~1.30100.0        [Up-to-date]
Updating platformio/tool-openocd-esp32        2.1000.20210721 @ ~2.1000.0        [Up-to-date]
Updating platformio/tool-mkspiffs             2.230.0 @ ~2.230.0                 [Up-to-date]
Updating platformio/tool-cmake                3.16.4 @ ~3.16.0                   [Up-to-date]
Updating platformio/tool-ninja                1.7.1 @ ^1.7.0                     [Up-to-date]

Platform espressif32
--------
Updating git+https://github.com/platformio/platform-espressif32.git 3.4.0+sha.973c5bc                  [Up-to-date]
Updating platformio/toolchain-xtensa32        2.50200.97 @ ~2.50200.0            [Up-to-date]
Updating platformio/toolchain-xtensa32s2      1.80400.210211 @ ~1.80400.0        [Up-to-date]
Updating platformio/toolchain-esp32s2ulp      1.22851.191205 @ ~1.22851.0        [Up-to-date]
Updating platformio/toolchain-riscv-esp       1.80400.0 @ ~1.80400.0             [Up-to-date]
Updating espressif/toolchain-xtensa-esp32s2   8.4.0+2021r1 @ 8.4.0+2021r1        [Up-to-date]
Updating espressif/toolchain-riscv32-esp      8.4.0+2021r1 @ 8.4.0+2021r1        [Up-to-date]
Updating platformio/framework-espidf          3.40301.0 @ ~3.40301.0             [Up-to-date]
Updating platformio/tool-esptoolpy            1.30100.210531 @ ~1.30100.0        [Up-to-date]
Updating platformio/tool-openocd-esp32        2.1000.20210721 @ ~2.1000.0        [Up-to-date]
Updating platformio/tool-mkspiffs             2.230.0 @ ~2.230.0                 [Up-to-date]
Updating platformio/tool-cmake                3.16.4 @ ~3.16.0                   [Up-to-date]
Updating platformio/tool-ninja                1.7.1 @ ^1.7.0                     [Up-to-date]

Platform espressif8266
--------
Updating platformio/espressif8266             3.2.0                              [Up-to-date]
Updating platformio/toolchain-xtensa          2.100300.210717 @ ~2.100300.0      [Up-to-date]
Updating platformio/framework-arduinoespressif8266 3.30002.0 @ ~3.30002.0             [Up-to-date]
Updating platformio/tool-esptool              1.413.0 @ <2                       [Up-to-date]
Updating platformio/tool-esptoolpy            1.30000.201119 @ ~1.30000.0        [Up-to-date]
Updating platformio/tool-mkspiffs             1.200.0 @ ~1.200.0                 [Up-to-date]
Updating platformio/tool-mklittlefs           1.203.210628 @ ~1.203.0            [Up-to-date]


Library Manager
===============
Library Storage: /home/djpx/.platformio/lib

Is there a way to fix that by upgrading?

Thank you for reading I hope someone can help.

This is impossible in the standard configuration – the post says that starting with Arduino-ESP32 2.0, SD functions are broken. PlatformIO currently lags behind (https://github.com/platformio/platform-espressif32/issues/619) and uses the 1.0.6 version, which per that post does not have that bug.

You can always roll-back the platform version (which is linked to the used Arduino core version) per documentation and the release list. For example, to test the platform version that uses the core version before 1.0.6, use

platform = espressif32@3.1.1

instead of platform = espressif32 in the platformio.ini.

All PlatformIO can do right now for the internal filesystem is SPIFFS, not LittleFS (https://github.com/platformio/platform-espressif32/issues/570). This has however nothing to do with the SD card library which uses FAT32. The example

you linked does not use LittleFS.

Can you post your current full platformio.ini and source code for verification?

Sorry for the late response I was sick in the bed. The problem is that I do not know the version anymore which I had back then. I may did mixed some things, my old project used first SPIFFS and then LittleFS for that. The initial tutorial which I had linked seems that it had changed.

I think I may found the error it seems the SD-Card reader is missing one pin which is used in the (Ardoino) example.
This explains why it compiles without error but does not recognize the SD Card right. The odd thing is that one this website: ESP32: Guide for MicroSD Card Module Arduino | Random Nerd Tutorials they use only 6 pins too and it is the same code which is used in the arduino SD example.

That is my platformio.ini:
    ; PlatformIO Project Configuration File
;
;   Build options: build flags, source filter
;   Upload options: custom upload port, speed and extra flags
;   Library options: dependencies, extra library storages
;   Advanced options: extra scripting
;
; Please visit documentation for the other options and examples
; https://docs.platformio.org/page/projectconf.html

[env:esp32dev]
platform = espressif32
board = esp32dev
framework = arduino
monitor_speed = 115200
lib_deps = lorol/LittleFS_esp32@^1.0.6

And that is the code I used for trying the example:

/*
 * Connect the SD card to the following pins:
 *
 * SD Card | ESP32
 *    D2       -
 *    D3       SS
 *    CMD      MOSI
 *    VSS      GND
 *    VDD      3.3V
 *    CLK      SCK
 *    VSS      GND
 *    D0       MISO
 *    D1       -
 */
#include "FS.h"
#include "SD.h"
#include "SPI.h"

void listDir(fs::FS &fs, const char * dirname, uint8_t levels){
    Serial.printf("Listing directory: %s\n", dirname);

    File root = fs.open(dirname);
    if(!root){
        Serial.println("Failed to open directory");
        return;
    }
    if(!root.isDirectory()){
        Serial.println("Not a directory");
        return;
    }

    File file = root.openNextFile();
    while(file){
        if(file.isDirectory()){
            Serial.print("  DIR : ");
            Serial.println(file.name());
            if(levels){
                listDir(fs, file.path(), levels -1);
            }
        } else {
            Serial.print("  FILE: ");
            Serial.print(file.name());
            Serial.print("  SIZE: ");
            Serial.println(file.size());
        }
        file = root.openNextFile();
    }
}

void createDir(fs::FS &fs, const char * path){
    Serial.printf("Creating Dir: %s\n", path);
    if(fs.mkdir(path)){
        Serial.println("Dir created");
    } else {
        Serial.println("mkdir failed");
    }
}

void removeDir(fs::FS &fs, const char * path){
    Serial.printf("Removing Dir: %s\n", path);
    if(fs.rmdir(path)){
        Serial.println("Dir removed");
    } else {
        Serial.println("rmdir failed");
    }
}

void readFile(fs::FS &fs, const char * path){
    Serial.printf("Reading file: %s\n", path);

    File file = fs.open(path);
    if(!file){
        Serial.println("Failed to open file for reading");
        return;
    }

    Serial.print("Read from file: ");
    while(file.available()){
        Serial.write(file.read());
    }
    file.close();
}

void writeFile(fs::FS &fs, const char * path, const char * message){
    Serial.printf("Writing file: %s\n", path);

    File file = fs.open(path, FILE_WRITE);
    if(!file){
        Serial.println("Failed to open file for writing");
        return;
    }
    if(file.print(message)){
        Serial.println("File written");
    } else {
        Serial.println("Write failed");
    }
    file.close();
}

void appendFile(fs::FS &fs, const char * path, const char * message){
    Serial.printf("Appending to file: %s\n", path);

    File file = fs.open(path, FILE_APPEND);
    if(!file){
        Serial.println("Failed to open file for appending");
        return;
    }
    if(file.print(message)){
        Serial.println("Message appended");
    } else {
        Serial.println("Append failed");
    }
    file.close();
}

void renameFile(fs::FS &fs, const char * path1, const char * path2){
    Serial.printf("Renaming file %s to %s\n", path1, path2);
    if (fs.rename(path1, path2)) {
        Serial.println("File renamed");
    } else {
        Serial.println("Rename failed");
    }
}

void deleteFile(fs::FS &fs, const char * path){
    Serial.printf("Deleting file: %s\n", path);
    if(fs.remove(path)){
        Serial.println("File deleted");
    } else {
        Serial.println("Delete failed");
    }
}

void testFileIO(fs::FS &fs, const char * path){
    File file = fs.open(path);
    static uint8_t buf[512];
    size_t len = 0;
    uint32_t start = millis();
    uint32_t end = start;
    if(file){
        len = file.size();
        size_t flen = len;
        start = millis();
        while(len){
            size_t toRead = len;
            if(toRead > 512){
                toRead = 512;
            }
            file.read(buf, toRead);
            len -= toRead;
        }
        end = millis() - start;
        Serial.printf("%u bytes read for %u ms\n", flen, end);
        file.close();
    } else {
        Serial.println("Failed to open file for reading");
    }


    file = fs.open(path, FILE_WRITE);
    if(!file){
        Serial.println("Failed to open file for writing");
        return;
    }

    size_t i;
    start = millis();
    for(i=0; i<2048; i++){
        file.write(buf, 512);
    }
    end = millis() - start;
    Serial.printf("%u bytes written for %u ms\n", 2048 * 512, end);
    file.close();
}

void setup(){
    Serial.begin(115200);
    if(!SD.begin()){
        Serial.println("Card Mount Failed");
        return;
    }
    uint8_t cardType = SD.cardType();

    if(cardType == CARD_NONE){
        Serial.println("No SD card attached");
        return;
    }

    Serial.print("SD Card Type: ");
    if(cardType == CARD_MMC){
        Serial.println("MMC");
    } else if(cardType == CARD_SD){
        Serial.println("SDSC");
    } else if(cardType == CARD_SDHC){
        Serial.println("SDHC");
    } else {
        Serial.println("UNKNOWN");
    }

    uint64_t cardSize = SD.cardSize() / (1024 * 1024);
    Serial.printf("SD Card Size: %lluMB\n", cardSize);

    listDir(SD, "/", 0);
    createDir(SD, "/mydir");
    listDir(SD, "/", 0);
    removeDir(SD, "/mydir");
    listDir(SD, "/", 2);
    writeFile(SD, "/hello.txt", "Hello ");
    appendFile(SD, "/hello.txt", "World!\n");
    readFile(SD, "/hello.txt");
    deleteFile(SD, "/foo.txt");
    renameFile(SD, "/hello.txt", "/foo.txt");
    readFile(SD, "/foo.txt");
    testFileIO(SD, "/test.txt");
    Serial.printf("Total space: %lluMB\n", SD.totalBytes() / (1024 * 1024));
    Serial.printf("Used space: %lluMB\n", SD.usedBytes() / (1024 * 1024));
}

void loop(){

}

Well this pinout just double-declares the VSS <–> GND connection.

Is your error resolved now? What exactly did you change in HW?

Yes that is correct since it was soldered directly on an SD Card adapter. (SD card - Wikipedia). It has 2 ground pins. That was before I had the adapter.

I got it now working again it was because of 2 separate problems.
First problem: The module does need 5V not 3,3 V so after that the first problem was solved.
Second problem: PlatformIO still had the old SD lib installed which was already integrated in the core and there was difference in the implementation or it used some different libs which weren´t there any more. So after I deleted the old lib and changed the VCC pin it worked again with the sample code.
And the old projects works now again :smile:
Thank you for your time.