I have a Mega 2560 and an Ethernet shield. The Ethernet part works. I put in a Micro SD card in the socket and I tried both the datalogging and the ReadWrite example. I always get the message -
Initializing SD card...initialization failed!
I had the card formatted FAT32 previously, so I reformatted it FAT16 and used the "default" allocation size (64K).
The card works perfectly in every computer I put it in.
So my questions are:
Do I have to format the card first in a PC?
If so, what is the correct format?
What else am I doing wrong?
I read on another post that the SD memory had to be formatted FAT16 and smaller than 2GB, so I set the partition size to be 1GB and formatted it as FAT16.
STILL no luck! Could it be that the pin declaration file is wrong for my Mega 2560?
The datalogger example of the SD library has a bug. that makes the sketch not work with a Mega board and an Ethernet shield. It leaves the Ethernet chip selected, and this disrupts operations on the SPI port. (The same bug appears in the other SD example sketches.)
For Mega boards, you can add a line to the setup() function to make sure that the Wiznet chip is not selected.
Open the datalogger sketch and put a digitalWrite(10, HIGH); statement right after the pinMode statement:
void setup()
{
Serial.begin(9600);
Serial.print("Initializing SD card...");
// For Mega boards with an Ethernet shield, make sure the Wiznet
// chip is not selected:
pinMode(10, OUTPUT);
digitalWrite(10, HIGH); // davekw7x: If it's low, the Wiznet chip corrupts the SPI bus
// see if the card is present and can be initialized:
if (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
// don't do anything more:
return;
}
Serial.println("card initialized.");
}
Note that this change does not affect operation of '328 boards. The pinMode and digitalWrite statements are both unnecessary, but do not cause any problems.
Note, finally, that you do not (that's not) have to reformat your FAT32 cards. The Arduino SD library is a "wrapper" for the very excellent SdFat library. The SdFat library works perfectly for me for my smallest FAT16 card (8 Megabytes) to my largest FAT32 card (8 Gigabytes). It's true that FAT12 cards do have to be reformatted to FAT16, but that's not a very big deal (in my opinion).
Regards,
Dave
Footnote:
I do not have a Mega2560 board. My tests were carried out with a Mega1280 and the new Ethernet Shield. I don't know why the Arduino developers chose not to include more of the SdFat library examples. For me, SdFatInfo has been the single most helpful debugging tool for testing different hardware designs.. The others SdFat example sketches are extremely helpful in understanding basic operations on SD cards and their files.