Go Down

Topic: Cant get SD card working (Read 15859 times) previous topic - next topic

Charles Linquist

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?

Grumpy_Mike

#1
Jan 21, 2011, 12:41 pm Last Edit: Jan 21, 2011, 12:41 pm by Grumpy_Mike Reason: 1
It could be the card, some SD cards especially the lower capacity ones won't work on some of the shields around. Try another card of at least 2G.

Charles Linquist

I'll try another, but the card I was using was a 4GB  Transcend brand

Charles Linquist

OK, I just bought a 16GB Class 10 micro SD card.  I get the same message -

Initializing SD card...Card failed, or not present


I could probably hook up a logic analyzer,  but before I do, is there anything else?

Charles Linquist

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?

davekw7x

#5
Jan 22, 2011, 04:41 pm Last Edit: Jan 22, 2011, 05:29 pm by davekw7x Reason: 1
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:
Code: [Select]

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.

Charles Linquist

Thanks, Dave!  That worked.


Go Up