SD card...initialization failed Adafruit MicroSD

Trying to get my new SD card going
No luck

I get fail using arduino SD/Cardinfo sketch at command
card.init(SPI_HALF_SPEED, chipSelect)

Browsing the forum only trick I found was this
Not sure if it's valid for my Arduino Uno ...
pinMode(10, OUTPUT);
digitalWrite(10, HIGH);

Also tried using chipSelect = 10 or 4 as CS on SD card

Following instructions from Adafruit

The card is workin fine on my desktop SD box. FAT32 format .... but shouldn't be format problem at all I reckon .... failing at card.init ??

Any idea how I can go forward with trouble shoot ?
Or is it a DOA I got .... ?

Not really so much code before fail ....

/*
  SD card test

 This example shows how use the utility libraries on which the'
 SD library is based in order to get info about your SD card.
 Very useful for testing a card when you're not sure whether its working or not.

 The circuit:
  * SD card attached to SPI bus as follows:
 ** MOSI - pin 11 on Arduino Uno/Duemilanove/Diecimila
 ** MISO - pin 12 on Arduino Uno/Duemilanove/Diecimila
 ** CLK - pin 13 on Arduino Uno/Duemilanove/Diecimila
 ** CS - depends on your SD card shield or module.
 		Pin 4 used here for consistency with other Arduino examples


 created  28 Mar 2011
 by Limor Fried
 modified 9 Apr 2012
 by Tom Igoe
 */
// include the SD library:
#include <SPI.h>
#include <SD.h>

// set up variables using the SD utility library functions:
Sd2Card card;
SdVolume volume;
SdFile root;

// change this to match your SD shield or module;
// Arduino Ethernet shield: pin 4
// Adafruit SD shields and modules: pin 10
// Sparkfun SD shield: pin 8
const int chipSelect = 10;

void setup()
{
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  pinMode(10, OUTPUT);
  digitalWrite(10, HIGH);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }


  Serial.print("\nInitializing SD card...");

  // we'll use the initialization code from the utility libraries
  // since we're just testing if the card is working!
  if (!card.init(SPI_HALF_SPEED, chipSelect)) {
    Serial.println("initialization failed. Things to check:");
    Serial.println("* is a card inserted?");
    Serial.println("* is your wiring correct?");
    Serial.println("* did you change the chipSelect pin to match your shield or module?");
    return;
  } else {
    Serial.println("Wiring is correct and a card is present.");
  }

BR Bosse

  pinMode(10, OUTPUT);
  digitalWrite(10, HIGH);

You need that on Uno/328P to make the SS pin an output for the SPI Master. If it's left as Input and it goes Low, the SPI goes into Slave mode,
If you use 10 for chip select to the board vs 4, then the SS as Master is taken care of in one shot.
If you have a Mega, it's different, one of 50-51-52-53, have to look it up.

Don't you need SPI.begin() in setup()?
You could also look SDFat.h library.

Wire the SD module to the Uno as shown here.

The card is workin fine on my desktop SD box. FAT32 format .... but shouldn't be format problem at all I reckon .... failing at card.init ??

There could still be a problem with the card or the format. The card is accessed in SDIO mode on the desktop box but in SPI mode on the Arduino.

Desktops are more forgiving of nonstandard SD card formats such as GPT based formats. The Arduino only accepts MBR based cards.

The only way to be sure an SD card has the standard format is to use the SD formatter.

This code will not help on Uno since Arduino SD libraries perform this initialization.

pinMode(10, OUTPUT);
  digitalWrite(10, HIGH);

This code is required when there is a second SPI device with pin 10 as chip select on a non 328 board.

Run the SD.h CardInfo example or the SdFat SdInfo example and post the output. The SdFat SdInfo example prints a useful error code.

Thanks for info
Turned out my problem was semibad +5 V like I could see when attaching my scope
So work fine now :slight_smile:
I tried both pin 4 and pin 10 both works

Ha det gött
Bosse

1 Like

hi all,
i am new to this and the forum, so a bit thank you for all your help in advance.
i had same problem as the author and after following all your solutions proposed, i still dont get it right.
i have tried the sample scketch cardinfo to check if it is working but always shows same screen with same thing:
{
Initializing SD card...initialization failed. Things to check:

  • is a card inserted?
  • is your wiring correct?
  • did you change the chipSelect pin to match your shield or module?
    }
    i did change the pin for the ss pin10, and set it as an output, i have 4.95 on spi pins apart from the csk which is 0v. is it normal?
    i have checked for continuity and every bit is ok, not sure what i am doing wrong, even i bought another sd card reader, but same thing happens.

i have tried with the nano and uno boards, and nothing....

i am really frustrated...., does any body have any idea what i can do?

thanks.

//UPDATE//
managed to use Sdfat sdinfo example and got the below:

cardBegin failed
SD errorCode: 0X2
SD errorData: 0XFF

Stress test example test:
BUF_SIZE 512
Type any character to start
error: sd.begin
SD errorCode: 0X2,0X57

Sdformartter results:
SD initialization failure!
Is the SD card inserted correctly?
Is chip select correct at the top of this program?
error: card.begin failed
SD error: 2,ff

it turns out i had 2 faulty sd card readers, but i dont understand, anyway i got another type mini sd card reader, and that one works fine.

hey guys,
I am using the the attached TFT Screen 3.5 inch with Arduino Mega.The LCD is working fine.

Now I am trying to load image from a 8 GB micro SD card (FAT 32) connecting all 4 pins of SD which are CS, MISO, MOSI, SCK to 50,51,52,53 respectively.I am getting initialization error and it is getting failed.

I have even tried this with Arduino Uno by connecting CS, MISO, MOSI, SCK to 10,11,12,13 respectively but still I am facing the same issue.

i have also tried using a different 4 GB FAT32 micro SD card just testing,bt still it failed

My code is as follows:

// include the necessary libraries
#include <SPI.h>
#include <SD.h>
#include <TFT.h>  // Arduino LCD library

// pin definition for the Uno
#define sd_cs  10
#define lcd_cs 7
#define dc     9
#define rst    8

// pin definition for the Mega
//#define sd_cs  10
//#define lcd_cs 40
//#define dc     27
//#define rst    41

// pin definition for the Leonardo
//#define sd_cs  8
//#define lcd_cs 7
//#define dc     0
//#define rst    1

TFT TFTscreen = TFT(lcd_cs, dc, rst);

// this variable represents the image to be drawn on screen
PImage logo;


void setup() {
 // initialize the GLCD and show a message
 // asking the user to open the serial line
 TFTscreen.begin();
 TFTscreen.background(255, 255, 255);

 TFTscreen.stroke(0, 0, 255);
 TFTscreen.println();
 TFTscreen.println(F("Arduino TFT Bitmap Example"));
 TFTscreen.stroke(0, 0, 0);
 TFTscreen.println(F("Open serial monitor"));
 TFTscreen.println(F("to run the sketch"));

 //Online fix
 // http://embedjournal.com/arduino-sd-card-initialization-failed/
 pinMode(sd_cs, OUTPUT); // change this to 53 on a mega  // don't follow this!!
 digitalWrite(sd_cs, HIGH);
 
 // initialize the serial port: it will be used to
 // print some diagnostic info
 Serial.begin(9600);
 while (!Serial) {
   // wait for serial line to be ready
 }

 // clear the GLCD screen before starting
 TFTscreen.background(255, 255, 255);

 // try to access the SD card. If that fails (e.g.
 // no card present), the setup process will stop.
 Serial.print(F("Initializing SD card..."));
 if (!SD.begin(sd_cs, 11, 12, 13)) {
   Serial.println(F("failed!"));
   return;
 }
 Serial.println(F("OK!"));

 // initialize and clear the GLCD screen
 TFTscreen.begin();
 TFTscreen.background(255, 255, 255);

 // now that the SD card can be access, try to load the
 // image file.
 logo = TFTscreen.loadImage("arduino.bmp");
 if (!logo.isValid()) {
   Serial.println(F("error while loading arduino.bmp"));
 }
}

void loop() {
 // don't do anything if the image wasn't loaded correctly.
 if (logo.isValid() == false) {
   return;
 }

 Serial.println(F("drawing image"));

 // get a random location where to draw the image.
 // To avoid the image to be draw outside the screen,
 // take into account the image size.
 int x = random(TFTscreen.width() - logo.width());
 int y = random(TFTscreen.height() - logo.height());

 // draw the image to the screen
 TFTscreen.image(logo, x, y);

 // wait a little bit before drawing again
 delay(1500);
}

Can anyone help me or give any tips for the same?

Thanks in advance

Not sure if this will help but here's my bitter experience with microsd boards.

I had similar issues and tried everything advised by the entire planet - level shifting, SDFormatter, check wiring,reduce spi speed,output CS pin high,strong language,etc. Even went down the retail-debugging route but that only helped Adafruit, Sparkfun, Catalex and countless ebay vendors.

Finally I realised I always tested my boards by running SDFormatter on the card first. So just for the hell of it, I created a dummy 0-byte file on a freshly-wiped card. Every single microsd board that I tossed away as junk worked after that without a hitch. Has anyone else come across this as well?

I guess I'm not the only one.
No luck too.

i spent ages failing to get my sd card working, and then installed an earlier version ( 1.0.8 ) of the SD library which fixed the issue for me.

Hi all
should be straight farward; unfortunately not. Someone can help? I cant get SD shield up to work.
I use ARDUINO DUE and 1GB ElitePRO SD card; formatted with SDFormatter tool. The SD.h ibrary is 1.1.1
The CS pin keeps staying at 3.3V. Is there any trival bug? Thanks

Not working code:

#include <SPI.h>
#include <SD.h>
#define CS 4

void setup() {
SPI.begin(CS);
SPI.setBitOrder (MSBFIRST);
SPI.setDataMode(SPI_MODE0,CS);
SPI.setClockDivider(CS,21);
Serial.begin(9600);

while (!Serial) {
}
Serial.print("Attempt to initialize SD card...");
if (!SD.begin(CS)) {
Serial.println("SD.begin() failed!");
return;
}
Serial.println("SD.begin()... done.");
}

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

try this setup:

void setup() {
  Serial.print("Initializing SD card...");
  pinMode(CS, OUTPUT);  //make sure that the default chip select pin is set to output
  digitalWrite(SDC_PIN, HIGH);
  delay(1);
  if (!SD.begin(CS[code])) {
    Serial.println("Card failed, or not present");
    return;
  }
  Serial.println("card initialized.");
}

Regards

Nitrof

I have faced this problem and just found a solution that works.

For me, it was a connection problem. I thought I needed to power the SD card reader using the 3.3 V pin in the Arduino. Instead, because it has a voltage regulator, it needed 5V as the input voltage.

As soon as I connected it to the 5V pin, the CardInfo example worked.

Good luck finding the issue in your circuit!

(The credit for this solution goes to Majenko from StackExchange. Link of page: arduino uno - SD card fails to initialize - Arduino Stack Exchange )

1 Like

I partially successfully tested the SdCard on the ArduinoZero / NanoARM ,
I connected the SdCard onto the 6pin SPI Bus on the end of the Board.
It sees the Card and Size,

  • but stops before returning the Files on the SdCards.

I tried Cards, checked on USB Card Reader, 1GB and 4GB Cards - each including a few files.

I used both: Examples.SD.CardInfo , Examples.SD.ListFiles programs from the IDE.

__MY_ArduinoZero-NanoARM_SD-Card_WIRING_SCH.png

The Micro SdCard Shield that I used :

__MY_ArduinoZero-NanoARM_SD-Card_WIRING_SCH.png

The Code that I used, for example :

// include the SD library:
#include <SPI.h>
#include <SD.h>

// set up variables using the SD utility library functions:
Sd2Card card;
SdVolume volume;

SdFile root;

// change this to match your SD shield or module;
// Arduino Ethernet shield: pin 4
// Adafruit SD shields and modules: pin 10
// Sparkfun SD shield: pin 8
// MKRZero SD: SDCARD_SS_PIN

const int SDchipSelect = 4;
//const int SDchipSelect = 10;
//const int SDchipSelect = 14;

void setup() 
{
  // Open serial communications and wait for port to open:
  //  Serial.begin(9600);
  SerialUSB.begin(9600);

  /// Note that even if you don't use the hardware SS pin (pin 10),  ///
  /// - it must be left as an output or the SD library won't work :   ///
  pinMode(      10          , OUTPUT);
  digitalWrite( 10          , HIGH  );

  /// SS pin must be set as Output and HIGH : ///
  pinMode(      chipSelect, OUTPUT);
  digitalWrite( chipSelect, HIGH  );
  
  while ( ! SerialUSB) 
  {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  SerialUSB.print("\nInitializing SD card...");

  // we'll use the initialization code from the utility libraries
  // since we're just testing if the card is working!
  if ( ! card.init(SPI_HALF_SPEED, chipSelect)) {
    SerialUSB.println("initialization failed. Things to check:");
    SerialUSB.println("* is a card inserted?");
    SerialUSB.println("* is your wiring correct?");
    SerialUSB.println("* did you change the chipSelect pin to match your shield or module?");
    return;
  } else {
    SerialUSB.println("Wiring is correct and a card is present.");
  }

  // print the type of card
  SerialUSB.print("\nCard type: ");
  switch (card.type()) 
  {
    case SD_CARD_TYPE_SD1:
      SerialUSB.println("SD1");
      break;
     
    case SD_CARD_TYPE_SD2:
      SerialUSB.println("SD2");
      break;
     
    case SD_CARD_TYPE_SDHC:
      SerialUSB.println("SDHC");
      break;
     
    default:
      SerialUSB.println("Unknown");
  }

  // Now we will try to open the 'volume'/'partition' - it should be FAT16 or FAT32
  if (!volume.init(card)) {
    SerialUSB.println("Could not find FAT16/FAT32 partition.\nMake sure you've formatted the card");
    return;
  }

  // print the type and size of the first FAT-type volume
  uint32_t volumesize;
  SerialUSB.print("\nVolume type is FAT");
  SerialUSB.println(volume.fatType(), DEC);
  SerialUSB.println();

  volumesize = volume.blocksPerCluster();    // clusters are collections of blocks
  volumesize *= volume.clusterCount();       // we'll have a lot of clusters
  volumesize *= 512;                            // SD card blocks are always 512 bytes
  SerialUSB.print("Volume size (bytes): ");
  SerialUSB.println(volumesize);
  SerialUSB.print("Volume size (Kbytes): ");
  volumesize /= 1024;
  SerialUSB.println(volumesize);
  SerialUSB.print("Volume size (Mbytes): ");
  volumesize /= 1024;
  SerialUSB.println(volumesize);

  SerialUSB.println("\nOpen Root: ");
  root.openRoot(volume);

  SerialUSB.println("\nFiles found on the card (name, date and size in bytes): ");
  // list all files in the card with date and size
  root.ls(LS_R | LS_DATE | LS_SIZE);

  root.close();

  Serial.println("done!");
}

@jlsilicon,
You posted the same question in 4 places. I've deleted the duplicates. Please don't do that again. Post once, have a little patience for an answer.

Do you have any idea - why the SdCards stats can be loaded , but stop at loading Files ?

Both cards were verified having Files on a SdCard Reader.

I even tried ReFormatting a Card in FAT16.

Program Results:

Initializing SD card...Wiring is correct and a card is present.

Card type: SD2

Volume type is FAT16

Volume size (bytes): 987938816
Volume size (Kbytes): 964784
Volume size (Mbytes): 942

Open Root:

Files found on the card (name, date and size in bytes):
  • Does not List Files
  • Does not Print out "Done"
  • just Hangs.

I also backed out SD from 1.1.1 to 1.08 - same problem.

Is there a quirk, with CD ChipDetect - needed for Files, but not for SD Card Properties ?

Well, I seem to have found a/the problem:

I am using the SerialUSB.print() instead of Serial.print().

The root.ls() in the lib , prints to Serial.print() , no choice.
So, I used the SD.listfiles example.

It still returns No Files though.

But, it does exit now.

//  SerialUSB.println("\nOpen Root: ");
//  root.openRoot(volume);
//
//  SerialUSB.println("\nFiles found on the card (name, date and size in bytes): ");
//  // list all files in the card with date and size
//  root.ls(LS_R | LS_DATE | LS_SIZE);
//
//  root.close();


  root2 = SD.open("/");

  printDirectory(root2, 0);

  root2.close();


  SerialUSB.println("\n- done!");
}


void loop(void) 
{
  ;
}


void printDirectory(File dir, int numTabs) 
{
  while (true) 
  {
    SerialUSB.println(".");

    File entry =  dir.openNextFile();
    if (! entry) 
    {
      // no more files
      break;
    }
    
    for (uint8_t i = 0; i < numTabs; i++) 
    {
      SerialUSB.print('\t');
    }
    SerialUSB.print(entry.name());
    if (entry.isDirectory()) 
    {
      SerialUSB.println("/");
      printDirectory(entry, numTabs + 1);
    } else {
      // files have sizes, directories do not
      SerialUSB.print("\t\t");
      SerialUSB.println(entry.size(), DEC);
    }
    entry.close();
  }

  SerialUSB.println("--");
}

Results now:

Initializing SD card...Wiring is correct and a card is present.

Card type: SD2

Volume type is FAT16

Volume size (bytes): 987938816
Volume size (Kbytes): 964784
Volume size (Mbytes): 942

Open Root:

Files found on the card (name, date and size in bytes):

.
--

- done !

But, where are the files ??

  • Both SD Cards return No Files.

Seems to be getting stuck looping on the first file (both Cards):

Initializing SD card...Wiring is correct and a card is present.

Card type: SDHC

Volume type is FAT16

Volume size (bytes): 3960733696
Volume size (Kbytes): 3867904
Volume size (Mbytes): 3777

Open Root:

Files found on the card (name, date and size in bytes):
FILE01.TXT
  • ???

Ok, Found it.

Typo, that I made in the Libs SdFile.cpp for root.ls() ,
I pasted an #endif inserted on same line as w++,
so it was ignored - yielding endless loop.

I added this, to fix the ArduinoZero/NanoArm print() to feed back to UsbProgrammer as SerialPort :

#ifdef SERIALUSB_E
SerialUSB.print(...);
#else
Serial.print(...);
#endif

Initializing SD card...Wiring is correct and a card is present.

Card type: SDHC

Volume type is FAT16

Volume size (bytes): 3960733696
Volume size (Kbytes): 3867904
Volume size (Mbytes): 3777

Open Root:

Files found on the card (name, date and size in bytes):
SDCARD4G.TXT  2017--19 18:38:46 10
FILE01.TXT    2017--19 18:38:50 0
FILE02.TXT    2017--19 18:39:00 0
FILE03.TXT    2017--20 00:11:54 0
DIR01/        2017--20 00:12:06

--

- done!
  • don't understand why the PrintDir() func doesn't work though ... :
void printDirectory(File dir, int numTabs) 
{
  while (true) 
  {
    SerialUSB.println(".");

    File entry =  dir.openNextFile();
    if (! entry) 
    {
      // no more files
      break;
    }
    
    for (uint8_t i = 0; i < numTabs; i++) 
    {
      SerialUSB.print('\t');
    }
    SerialUSB.print(entry.name());
    if (entry.isDirectory()) 
    {
      SerialUSB.println("/");
      printDirectory(entry, numTabs + 1);
    } else {
      // files have sizes, directories do not
      SerialUSB.print("\t\t");
      SerialUSB.println(entry.size(), DEC);
    }
    entry.close();
  }

  SerialUSB.println("--");
}

I had this same problem until I realised I had not soldered the 2x3 female header....

Once I had done that it worked perfectly! LED SPI should light up red when there's no sd card installed.

Ran CardInfo with the chipset set to 10 and it worked perfectly.