I am struggling to get my microSD card reader to work with my HTCC-ABO2S (GPS-6052) board.
The microSD card reader I am using works perfectly with an ESP32 and mostly identical code. I am able to read and write files off the SD card using the CubeCell board, but it will not write data to a file.
My pin setup is currently:
MISO – 26 (MISO0)
MOSI – 25 (MOSI0)
SCK – 27 (CLK0)
CS – 41 (GPIO11)
I have also tried using the other MISO, MOSI, and CLK pins with no success. I would actually prefer to use these, as I need the others for LoRa, but I’ll cross that bridge when I get to it.
All assistance and/or suggestions are much appreciated.
UPDATE:
Below are 2 very unusual things that I have noticed while trying to get this to work…
-
dataFile = SD.open(filePath, FILE_APPEND);
does not compile unless I switchFILE_APPEND
toFILE_WRITE
. This shouldn’t realistically be a huge problem, but is very unusual. - If I run
dataFile = SD.open(filePath, FILE_WRITE);
followed byif(dataFile){}
, the if statement will always returnfalse
. This is quite odd, and I’m not exactly sure what it indicates. If I instead runif (!SD.exists(filePath))
, it will returntrue
as expected.
Updated code:
//#include "FS.h" // Will not compile with FS.h, this may or may not be causing problems
#include "SPI.h"
#include "SD.h"
bool fileLooping = false;
String filePath = "/TEST_0.csv";
int fileConstant = 0;
File dataFile;
const int chipSelect = 41;
/* WAIT FUNCTION */
inline void wait(unsigned long const& t) {
unsigned long sTime = millis();
while (millis() - sTime <= t);
}
void setup() {
Serial.begin(115200); // Open serial
while (!Serial) {} // Wait for serial
pinMode(chipSelect,OUTPUT);
digitalWrite(chipSelect,LOW);
Serial.print("Initializing SD card...");
if (!SD.begin(chipSelect)) { // This works, SD initializes just fine
Serial.println("Card failed, or not present");
while(1){}
}
Serial.println("card initialized.");
while(fileLooping == true){
if (!SD.exists(filePath)) { // CHECK IF CSV FILE ALREADY EXISTS OR NOT (This works)
dataFile = SD.open(filePath, FILE_WRITE); // WRITE CSV FILE TO SD CARD
wait(1000);
if (!dataFile) { // Always returns "failed to write," even though it actually does write the file
Serial.println("FAILED TO WRITE " + filePath + " TO SD CARD. CONTINUING REGARDLESS.");
dataFile.close(); //CLOSE FILE
return;
} else {
Serial.println(filePath + " SUCCESSFULLY WRITTEN TO SD CARD.");
dataFile.close(); //CLOSE FILE
fileLooping = false;
break;
}
} else {
Serial.println(filePath + " ALREADY EXISTS ON SD CARD. TRYING AGAIN WITH DIFFERENT FILE NAME.");
fileConstant++;
filePath = "/TEST_" + String(fileConstant) + ".csv"; // Try again with different file name until file name is unique on SD card (This works)
}
}
}
void loop() {
wait(1000);
String dataString = "Test,1,2";
wait(1000);
if (!SD.exists(filePath)) {
Serial.println("!!!!! UNABLE TO OPEN " + filePath + " TO PRINT DATA !!!!!");
} else {
dataFile = SD.open(filePath, FILE_WRITE); // OPEN THE FILE
if (dataFile) {
dataFile.println(dataString); // WRITE DATA
dataFile.close(); // CLOSE THE FILE
Serial.println("CSV HEADERS SUCCESSFULLY WRITTEN TO " + filePath);
} else {
Serial.println("!!!!! ERROR PRINTING DATA TO " + filePath + " !!!!!"); // Always prints this & does not write data to file
}
}
}