Hello all,
For some reason my MKR Zero is not writing to the SD card. It will create a file but when I go look at said file on a computer there is zero bytes written to it, and no text....
So I'm using a MKR Zero with 16 GB SanDisk microSd formatted Fat32
Here is the code I've been testing
I've tried a couple different chip select values and none seem to work (4, SS1, SDCARD_SS_PIN) with no success. I'm a bit stumped... hopefully one of you guys can point me in right direction
#include <SPI.h>
#include <SD.h>
#include <Wire.h>
const int chipSelect = SDCARD_SS_PIN;
File logfile;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
delay(2000);
if (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
// don't do anything more:
//return; //uncomment if want setup to stop if no SD available
}
if (SD.begin(chipSelect)) {
Serial.println("card initialized.");
}
// create a new file
char filename[] = "LOGGER00.TXT";
for (uint8_t i = 0; i < 100; i++) {
filename[6] = i / 10 + '0';
filename[7] = i % 10 + '0';
if (! SD.exists(filename)) {
// only open a new file if it doesn't exist
logfile = SD.open(filename, FILE_WRITE);
break;
}
}
if (! logfile) {
Serial.println("couldnt create file");
}
else {
Serial.print("Logging to: ");
Serial.println(filename);
logfile.println("This is a test file");
// if ECHO_TO_SERIAL
Serial.println("This is a test file");
}
if (!logfile.println() ) {
Serial.println("error with write header");
}
pinMode(LED_BUILTIN, OUTPUT);
Serial.println("System ready...");
}
void loop() {
// put your main code here, to run repeatedly:
logfile;
digitalWrite(LED_BUILTIN, HIGH);
delay(2000);
logfile.print("Hello World");
Serial.println("Hello World");
if(!logfile.println()){
Serial.println("SD printing failed");
}
delay(2000);
digitalWrite(LED_BUILTIN, LOW);
delay(2000);
}
As far as debugging goes heres what I've got in the serial monitor:
with SD card in the Arduino, continuous prints of Hello World,
with SD card pulled out of Arduino when on: "Hello World" followed by "SD printing failed"