My Arduino Mega won't write to my SD Card using the spark fun riser board. Pin 8 is wired to Pin 53 on Mega.
From all the examples I've looked at, it seems like I'm doing this correctly. But still I can't write a file on the SD Card. Why won't this work? Anything I should Try? Please Help!!
#include <SD.h>
// SDCARD Variables
File myFile;
const int chipSelect = 8;
void setup()
{
Serial.begin(115200);
///////// SD CARD SETUP ////////////////
pinMode(10, OUTPUT);
// see if the card is present and can be initialized:
if (!SD.begin(chipSelect)) { // SEEMS TO FAIL HERE!!
Serial.println("Card failed, or not present"); // don't do anything more:
return;
}
else {
Serial.println("SD card initialized."); // THIS LINE ALWAYS PRINTS
}
// if the file opened okay, write to it:
if (myFile) {
Serial.println("Writing CSV Header Data");
myFile.println("Write this data onto the SD Card");
// close the file:
myFile.close();
Serial.println("done.");
} else {
// if the file didn't open, print an error:
Serial.println("error opening MYFILE.CSV");
}
}
What's a sparkfun riser board? Provide links for all the hardware you're using. And use code tags! (the little hash [#] button in the form is providing them)
I had a similar problem that turned out to be setting up the select pin correctly. Unfortunately I don't remember all the details, but I notice you have:
pinMode(10, OUTPUT);
if (!SD.begin(chipSelect))
with chipSelect defined as 8. Maybe the same value should be used in both places...the 10 should also be 8.
Also, in the SD/DumpFile example, they say:
// On the Ethernet Shield, CS is pin 4. Note that even if it's not
// used as the CS pin, the hardware CS pin (10 on most Arduino boards,
// 53 on the Mega) must be left as an output or the SD library
// functions will not work.
So I wonder if maybe the 8 & 10 should be 53 for the Mega? I do remember that this example didn't work out of the box; they aren't consistent with the CS pin in the code. I'm reasonably sure getting them consistent was the key for me, but I don't recall what value I used in the end.