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?