SD Card Info sketch not working

Hello, having some trouble debugging the SDCard reader/writer boards with a NodeMCUv3.

My platform.ini file looks like:

[env:nodemcuv2]
platform = espressif8266
board = nodemcuv2
framework = arduino
monitor_port                = COM6
upload_resetmethod          = nodemcu
upload_port                 = COM6

My initial code looks like:

#include <Arduino.h>
#include <SPI.h>
#include <SD.h>
File myFile;
void setup() {
  Serial.begin(9600);
  while (!Serial) {
    ;
  }

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

  if (!SD.begin(D1)) {
    Serial.println("initialization failed!");
    while (1);
  }

  Serial.println("initialization done.");
  myFile = SD.open("test.txt", FILE_WRITE);

  if (myFile) {
    Serial.print("Writing to test.txt...");
    myFile.println("This is a test file :)");
    myFile.println("testing 1, 2, 3.");
    for (int i = 0; i < 20; i++) {
      myFile.println(i);
    }
    myFile.close();
    Serial.println("done.");
  } else {
    Serial.println("error opening test.txt");
  }
}
void loop() {
}

Which outputs

Initializing SD card...initialization failed!

Over and over again.

So I found this sketch to try to help me diagnose what’s wrong with my SD card:

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

Sd2Card card;
SdVolume volume;
SdFile root;
const int chipSelect = 4;

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


  Serial.print("\nInitializing SD card...");
  if (!card.init(SPI_HALF_SPEED, chipSelect)) {
    Serial.println("initialization failed. Things to check:");
    Serial.println("* is a card inserted?");
    Serial.println("* is your wiring correct?");
    Serial.println("* did you change the chipSelect pin to match your shield or module?");
    while (1);
  } else {
    Serial.println("Wiring is correct and a card is present.");
  }

  Serial.println();
  Serial.print("Card type:         ");
  switch (card.type()) {
    case SD_CARD_TYPE_SD1:
      Serial.println("SD1");
      break;
    case SD_CARD_TYPE_SD2:
      Serial.println("SD2");
      break;
    case SD_CARD_TYPE_SDHC:
      Serial.println("SDHC");
      break;
    default:
      Serial.println("Unknown");
  }

  if (!volume.init(card)) {
    Serial.println("Could not find FAT16/FAT32 partition.\nMake sure you've formatted the card");
    while (1);
  }

  Serial.print("Clusters:          ");
  Serial.println(volume.clusterCount());
  Serial.print("Blocks x Cluster:  ");
  Serial.println(volume.blocksPerCluster());

  Serial.print("Total Blocks:      ");
  Serial.println(volume.blocksPerCluster() * volume.clusterCount());
  Serial.println();

  uint32_t volumesize;
  Serial.print("Volume type is:    FAT");
  Serial.println(volume.fatType(), DEC);

  volumesize = volume.blocksPerCluster();
  volumesize *= volume.clusterCount();
  volumesize /= 2;
  Serial.print("Volume size (Kb):  ");
  Serial.println(volumesize);
  Serial.print("Volume size (Mb):  ");
  volumesize /= 1024;
  Serial.println(volumesize);
  Serial.print("Volume size (Gb):  ");
  Serial.println((float)volumesize / 1024.0);
  Serial.println("\nFiles found on the card (name, date and size in bytes): ");
  root.openRoot(volume);
  root.ls(LS_R | LS_DATE | LS_SIZE);
}

void loop(void) {
  
}

But I can’t get this to compile. I get a bunch of errors such as:

src\main.cpp:50:1: error: 'Sd2Card' does not name a type
 Sd2Card card;
 ^
src\main.cpp:51:1: error: 'SdVolume' does not name a type
 SdVolume volume;
 ^
src\main.cpp:52:1: error: 'SdFile' does not name a type
 SdFile root;
 ^
src\main.cpp: In function 'void setup()':
src\main.cpp:73:8: error: 'card' was not declared in this scope

I have tried adding various libraries into lib_deps, such as greiman/SdFat and arduino-libraries/SD@^1.2.4, but it appears these are for the atmega chipset and I get platform not supported errors during compile.

Can anyone help me figure out what’s going wrong, and where?

Okay, figured out the SDCard issue. Apparently the library for NodeMCU is touchy when it comes to the SD Card. Even though it was FAT32, the library couldn’t handle it for some reason. It looks like the developer has a new v2 beta version of the library.

Formatting with just Windows was not enough.

I formatted the card using this tool: SD Memory Card Formatter | SD Association and everything started working immediately.

I figured this out by running this code on PlatformIO:

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

File myFile;
void setup() {
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

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

  bool initFailed = false;

  if (!SD.begin(D4, SD_SCK_MHZ(1))) {
    initFailed = true;
    Serial.println("initialization failed!");
    Serial.println();
  }
  delay(500);

  Serial.print("Card type:         ");
  Serial.println(SD.type());
  Serial.print("fatType:           ");
  Serial.println(SD.fatType());
  Serial.print("size:              ");
  Serial.println(SD.size());


  Serial.println();
  if (initFailed) {
    delay(1000);
    while (1); // Soft reset
  }

  Serial.println("initialization done.");
  myFile = SD.open("test.txt", FILE_WRITE);

  if (myFile) {
    Serial.print("Writing to test.txt...");
    myFile.println("This is a test file :)");
    myFile.println("testing 1, 2, 3.");
    for (int i = 0; i < 20; i++) {
      myFile.println(i);
    }
    myFile.close();
    Serial.println("done.");
  } else {
    Serial.println("error opening test.txt");
  }
}
void loop() {
// nothing happens after setup
}

And noticed that the Card Type was 3, but it had no size being reported.

1 Like

Thanks for sharing. I came across this thread today while having the same issue with a D1 Mini V4.0.0. The software worked when compiled with the Arduino IDE but not when using Platformio. I formatted the card with the same utility and then it worked ok with no changes to the software.