Go Down

Topic: SD Card Initialization Help (Read 5464 times) previous topic - next topic

bryant507

Apr 01, 2013, 07:33 am Last Edit: Apr 01, 2013, 07:35 am by bryant507 Reason: 1
I use Arduino IDE 1.0.4 and sdfatlib20130313.zip from http://code.google.com/p/sdfatlib/downloads/list.
My board is APM2.5 MEGA 2560
I do it from http://arduino.cc/forum/index.php/topic,65391.0.html Reply #7.
But it doesn't work.

Quote
Enter the chip select pin number: 12

SD initialization failed.
Do not reformat the card!
Is the card correctly inserted?
Is chipSelect set to the correct value?
Is there a wiring/soldering problem?
errorCode: 0x1, errorData: 0xff


I wiring  MISO -- PIN 50
           MOSI -- PIN 51
           SCK   -- PIN 52
           CS     -- PIN12
and pinMode(53, OUTPUT);
Can you help me?Thx!

SurferTim

#1
Apr 01, 2013, 11:30 am Last Edit: Apr 01, 2013, 11:53 am by SurferTim Reason: 1
Did you reformat the card? I use a SanDisk 2GB card, and it needed nothing. I just stuck it in, and it worked.

Have you tried the standard SD library as a test?
Code: [Select]
#include <SD.h>

void setup() {
 Serial.begin(9600);

 Serial.print("Starting SD...");
 if(!SD.begin(12)) Serial.println("failed");
 else Serial.println("ok");
}

void loop() {
}

If you have any other devices connected to the Mega, disconnect them for this test. Insure the SD card slave select is connected to D12.

edit: How are you powering that SD module? If you are using the 3.3v bus on the Arduino, there is not enough power available on that bus to power a SD card. That bus is limited to about 50ma, and the newer SD cards require about 100ma to read/write.

bryant507

I reformated the scard by computer.Fat32.
I used a SanDisk 2GB card.
and use the standard SD library
Code: [Select]
// Quick hardware test
#include <SdFat.h>
// Test with reduced SPI speed for breadboards.
// Change spiSpeed to SPI_FULL_SPEED for better performance
// Use SPI_QUARTER_SPEED for even slower SPI bus speed
const uint8_t spiSpeed = SPI_HALF_SPEED;
//------------------------------------------------------------------------------
// Normally SdFat is used in applications in place
// of Sd2Card, SdVolume, and SdFile for root.
Sd2Card card;
SdVolume volume;
SdFile root;

// Serial streams
ArduinoOutStream cout(Serial);

// input buffer for line
char cinBuf[40];
ArduinoInStream cin(Serial, cinBuf, sizeof(cinBuf));

// SD card chip select
int chipSelect;

void cardOrSpeed() {
  cout << pstr(
    "Try another SD card or reduce the SPI bus speed.\n"
    "The current SPI speed is: ");
  uint8_t divisor = 1;
  for (uint8_t i = 0; i < spiSpeed; i++) divisor *= 2;
  cout << F_CPU * 0.5e-6 / divisor << pstr(" MHz\n");
  cout << pstr("Edit spiSpeed in this sketch to change it.\n");
}

void reformatMsg() {
  cout << pstr("Try reformatting the card.  For best results use\n");
  cout << pstr("the SdFormatter sketch in SdFat/examples or download\n");
  cout << pstr("and use SDFormatter from www.sdcard.org/consumer.\n");
}

void setup() {
  Serial.begin(9600);
  while (!Serial) {}  // wait for Leonardo
 
  cout << pstr(
    "\nSD chip select is the key hardware option.\n"
    "Common values are:\n"
    "Arduino Ethernet shield, pin 4\n"
    "Sparkfun SD shield, pin 8\n"
    "Adafruit SD shields and modules, pin 10\n");
}

bool firstTry = true;
void loop() {
  // read any existing Serial data
  while (Serial.read() >= 0) {}

  if (!firstTry) cout << pstr("\nRestarting\n");
  firstTry = false;

  cout << pstr("\nEnter the chip select pin number: ");
  cin.readline();
  if (cin >> chipSelect) {
    cout << chipSelect << endl;
  } else {
    cout << pstr("\nInvalid pin number\n");
    return;
  }
  if (!card.init(spiSpeed, chipSelect)) {
    cout << pstr(
      "\nSD initialization failed.\n"
      "Do not reformat the card!\n"
      "Is the card correctly inserted?\n"
      "Is chipSelect set to the correct value?\n"
      "Is there a wiring/soldering problem?\n");
    cout << pstr("errorCode: ") << hex << showbase << int(card.errorCode());
    cout << pstr(", errorData: ") << int(card.errorData());
    cout << dec << noshowbase << endl;
    return;
  }
  cout << pstr("\nCard successfully initialized.\n");
  cout << endl;

  uint32_t size = card.cardSize();
  if (size == 0) {
    cout << pstr("Can't determine the card size.\n");
    cardOrSpeed();
    return;
  }
  uint32_t sizeMB = 0.000512 * size + 0.5;
  cout << pstr("Card size: ") << sizeMB;
  cout << pstr(" MB (MB = 1,000,000 bytes)\n");
  cout << endl;

  if (!volume.init(&card)) {
    if (card.errorCode()) {
      cout << pstr("Can't read the card.\n");
      cardOrSpeed();
    } else {
      cout << pstr("Can't find a valid FAT16/FAT32 partition.\n");
      reformatMsg();
    }
    return;
  }
  cout << pstr("Volume is FAT") << int(volume.fatType());
  cout << pstr(", Cluster size (bytes): ") << 512L * volume.blocksPerCluster();
  cout << endl << endl;

  root.close();
  if (!root.openRoot(&volume)) {
    cout << pstr("Can't open root directory.\n");
    reformatMsg();
    return;
  }
  cout << pstr("Files found (name date time size):\n");
  root.ls(LS_R | LS_DATE | LS_SIZE);

  if ((sizeMB > 1100 && volume.blocksPerCluster() < 64)
    || (sizeMB < 2200 && volume.fatType() == 32)) {
    cout << pstr("\nThis card should be reformatted for best performance.\n");
    cout << pstr("Use a cluster size of 32 KB for cards larger than 1 GB.\n");
    cout << pstr("Only cards larger than 2 GB should be formatted FAT32.\n");
    reformatMsg();
    return;
  }
  // read any existing Serial data
  while (Serial.read() >= 0) {}
  cout << pstr("\nSuccess!  Type any character to restart.\n");
  while (Serial.read() < 0) {}
}

It doesn't work.
And I used your code.
Code: [Select]
#include <SD.h>

void setup() {
  Serial.begin(9600);

  Serial.print("Starting SD...");
  if(!SD.begin(12)) Serial.println("failed");
  else Serial.println("ok");
}

void loop() {
}

But it doesn't work, too.

There is not other devices connected to the Mega, only SD card.The SDcard CS is connected to PIN 12(OUT 1).
And I used the 5V to power the SDcard.

SurferTim

Can you post a link to that SD card?


bryant507


Can you post a link to that SD card?


What do you mean? I connect the SDcard CS to PIN 12,which is Mega2560 OUT1.

bryant507

It is the APM2.5 Mega2560 SCH.

SurferTim

I have heard on this forum that some of the SD card modules with the voltage divider circuit rather than a true logic level converter can cause problems. As I recall, it is a signal rise and fall time challenge with the larger, newer uSD cards due to the rc component of the circuit. Maybe fat16lib or someone else can add more to that.

Mine has a logic level converter.


SurferTim

Yes, those resistors (R1-R4) in the bottom picture. Unless my eyes are failing (that is possible..I'm old!), those particular resistors are 10Kohm. That will add a considerable rise and fall time delay.

bryant507


Yes, those resistors (R1-R4) in the bottom picture. Unless my eyes are failing (that is possible..I'm old!), those particular resistors are 10Kohm. That will add a considerable rise and fall time delay.



So....How to solve it? Must I change the SD module or...?

SurferTim

I would see if fat16lib or someone else has more to add to this thread. It may or may not be that problem. It might help other users if you posted the make, model, and size of the SD card you have in the module, and anything you have done to it, like reformatting. Most of that can be corrected.

If it is the new rc component of that circuit, you will probably need to get another type of SD module. To insure the best performance, get one with a logic level converter, not the voltage divider.

bryant507



reformatting:
Put the SD card to computer,open My Computer on the desktop,see the SDcard, right click it.Select the formatting.

fat16lib

This looks like an SD socket that is in many ebay ads.

This card has 10k resistors connected between 3.3V and CS, SCK, MISO, and MOSI.   This may not work with many SD cards when the SPI is driven by 5V signals.

Here is an example add with circuit diagram http://www.ebay.com/itm/SD-Card-Reader-Module-for-Arduino-ARM-Read-and-Write-/200659790559?pt=LH_DefaultDomain_0&hash=item2eb8416adf.

bryant507


This looks like an SD socket that is in many ebay ads.

This card has 10k resistors connected between 3.3V and CS, SCK, MISO, and MOSI.   This may not work with many SD cards when the SPI is driven by 5V signals.

Here is an example add with circuit diagram http://www.ebay.com/itm/SD-Card-Reader-Module-for-Arduino-ARM-Read-and-Write-/200659790559?pt=LH_DefaultDomain_0&hash=item2eb8416adf.


My SD card is driven by 5V power.It is connected with 5V,not 3.3V.But it is still not work.

Go Up