Go Down

Topic: Problem initializing SD card (Read 7498 times) previous topic - next topic

jenjan87

I am new working with arduino. I have just added the micro SD shield from sparkfun.com. When I use the sample sketches from the arduino library or from the sparkfun website, it keeps showing that the SD card was not initialized. I formatted the microSD card, changed the CS pin to 8 (as noted on the product website) and tried to run either of the sketches to read/write to the SD card, but it still does not work. Any suggestions?

cyclegadget


  As a new member, I can tell you that your question and details are good but, you need to put the code you are using in your post.

Code: [Select]
Use the # symbol and paste it  [code]"here"[code][/code][/code]
Good links: Eagle tutorial= http://www.youtube.com/playlist?list=PLDE1858BD83D19C70
General Arduion tutorials = http://tronixstuff.wordpress.com
http://www.gammon.com.au/forum/bbshowpost.php?bbtopic_id=123

jenjan87

here is the sample code from arduino that i used:

/*
  SD card read/write

This example shows how to read and write data to and from an SD card file    
The circuit:
* SD card attached to SPI bus as follows:
** MOSI - pin 11
** MISO - pin 12
** CLK - pin 13
** CS - pin 4

created   Nov 2010
by David A. Mellis
updated 2 Dec 2010
by Tom Igoe

This example code is in the public domain.
   
*/

#include <SD.h>

File myFile;

void setup()
{
  Serial.begin(9600);
  Serial.print("Initializing SD card...");
  // On the Ethernet Shield, CS is pin 4. It's set as an output by default.
  // Note that even if it's not used as the CS pin, the hardware SS pin
  // (10 on most Arduino boards, 53 on the Mega) must be left as an output
  // or the SD library functions will not work.
   pinMode(10, OUTPUT);
   
  if (!SD.begin(4)) {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done.");
 
  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  myFile = SD.open("test.txt", FILE_WRITE);
 
  // if the file opened okay, write to it:
  if (myFile) {
    Serial.print("Writing to test.txt...");
    myFile.println("testing 1, 2, 3.");
   // close the file:
    myFile.close();
    Serial.println("done.");
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }
 
  // re-open the file for reading:
  myFile = SD.open("test.txt");
  if (myFile) {
    Serial.println("test.txt:");
   
    // read from the file until there's nothing else in it:
    while (myFile.available()) {
       Serial.write(myFile.read());
    }
    // close the file:
    myFile.close();
  } else {
     // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }
}

void loop()
{
   // nothing happens after setup
}


fat16lib

You need to change the 4 to an 8 in this line:
Code: [Select]
if (!SD.begin(4)) {

Undo whatever you did before to change CS to 8.

jenjan87

i did that, i just forgot to do that when i copied and pasted the code onto this thread. but when i run the code in the serial monitor, it always says "initialization failed" ...

Grumpy_Mike

Quote
i just forgot to do that when i copied and pasted the code onto this thread

You also forgot to follow the advice in reply #1

jenjan87

yes, i realize that. im not up here often, this is my first time. and im really more concerned with fixing the problem rather than how the code looks in the forum.

fat16lib

The Arduino SD.h library is a wrapper for an old version of my SdFat library.  The wrapper doesn't provide much info so it would be better if you ran a new version of SdFat to diagnosis the problem.

If possible download new version of SdFat here http://code.google.com/p/sdfatlib/downloads/list

Put the SdFat folder in your libraries folder and run the QuickStart sketch which is located in the SdFat/examples/QuickStart folder.  It will ask you to enter the chip select pin number. Enter 8.

The output will be somthing like this:
Quote
SD initialization failed.
Is the card correctly inserted?
Is chipSelect set to the correct value?
Is there a wiring/soldering problem?

Or this:
Quote
Card successfully initialized.

Card size: 2033 MB (MB = 1000000 bytes)

Can't find a valid FAT16/FAT32 partition.
Try reformatting the card.  For best results use
the SdFormatter sketch in SdFat/examples or download
and use SDFormatter from www.sdcard.org/consumer.

Post a copy of the output.

Also run the SdInfo sketch.  You will need to edit this line at the start of the sketch so it is like this:
Code: [Select]
const uint8_t SdChipSelect = 8;

If it fails it will output something like this
Quote
card.init failed
SD errorCode: 0X1
SD errorData: 0X0


Please post the output.

Grumpy_Mike

Quote
and im really more concerned with fixing the problem rather than how the code looks

OK but be aware that the forum software will scramble your code for some lines so it is not just a matter of looking pretty.

Glad to see that you are so selfish as you are not concerned what others think. Best of luck with finding someone to help you.

jenjan87

i followed your instructions. It still failed. I've read that sometimes the type of SD card can matter. Is this true, because now I have a generic SD card. There are no soldering or wiring problems. Here is the output:


fat16lib

The card can not be see on the SPI bus so there is a fundamental hardware problem.

Which model Arduino are you using?  Is it a Mega by any chance?

How many SD cards have you tried?

jenjan87

Im using Arduino Pro 3.3v 8MHz w/ ATmega328.

I've only tried one SD card, the one they had on the sparkfun website.

fat16lib

Since everthing is from Sparkfun, you should contact their support. 

jenjan87

ok, i'll give it a try. thanks.

Go Up