Monday , January 23 2017
Home / Embedded Systems / Arduino / Workaround for “SD Card Initialization Failed!” in Arduino

Workaround for “SD Card Initialization Failed!” in Arduino


Arduino sd card

I was working on a project that involved the use of a SD (Secure Digital) card to log data into a text file. I chose Arduino as it  had a vary stable FAT (File Allocation Table) library. I had an Arduino Mega at my disposal and built a resistive network to step down the logic levels of the Arduino SPI bus at 5v to the SD card’s at 3v3. I was getting voltage levels that were withing the absolute maximum ratings of the SD card.

I have some experience with Arduino in the past (all good ones), and I expected things to work out of the box. But to my surprise I was mistaken. When I burned one of the example sketch to check the Card Info, I got this error “SD card initialization failed”.

Then after some research I discovered that, the Arduino SD library is just a colorful wrapper for the SDfatlib, and the actual library has much more options and can be used to debug properly. When I tested the quick start sketch form the newly installed library, I got this error message,

One after the other I tried all their example sketches without any luck. Then as usual I suspected my resistive network, especially after reading this “Is there a wiring/soldering problem?”. Built another one and then another! Yet the problem persisted.

I read in some forum posts that the resistive network introduces a latency in the SPI bus and could be the reason for the problem. So I decided to buy a low cost W5100 Ethernet shield which has a SD card slot (Image of which is featured in the heading of this post).

Most of the Ethernet shields have SD card slot, it would be pointless buying one without it. But again the problem was not solved. It took me a while to find out the solution.

The solution to this problem, is that you have to let digital Pin 10 as output (for the SD library to work) and put out a logic HIGH by adding “digitalWrite(10,HIGH);”. For Arduino Mega you have to do exactly the same ignore pin 53 completely though the comment asks you to change it to 53.  

So after making the change, the CardInfo sketch should look like this.

Good news is that you can add this line to all the code and it works perfectly fine (atleast for me). I hope this solves the “SD Card Initialization Failed” problem. If you notice any other problem please leave a comment and I will get back to you. Subscribe to our posts and newsletters to stay updated with the upcoming series of this tutorial.

About Siddharth

Siddharth is a Firmware Engineer, techie, and a movie-buff. His interests include, Programming, Embedded Systems, Linux, Robotics, CV, Carpentry and a lot more. At times, you could see some of his sunday projects converge on release quality. You get to know him on the following social channels.

Check Also

c2000_lauchpad

Interfacing 16×2 LCD with C2000 launchpad

  The C2000 Launchpad is an inexpensive evaluation module based on Piccolo family microcontrollers from …

  • Ganesh

    Can you exaplin how a sd card is interfaced with pic DIYform without a breakout board 🙂

    • siddharth

      Hi, Ganesh. It is possible to work with SD card without using any breakout board. You have to buy one of those big SD card slots and a MicroSD to SD card adapter. The bigger SD card slot is also a SMT component, but it’s pads are big enough to be soldered on a standard 0.1 inch spacing GP board. But you have to be really careful while inserting and removing the card.

  • Savard

    Hello Siddharth,

    I have tried following your advice but having difficulty making the code on the SD card still work.
    This is the code I have been using but unfortunately it is sending an error message.

    while (!card.init(SPI_HALF_SPEED, (10,11,12,13))) {

    I’m wondering if you could offer advice on this effort. Warm regards

    • Hi Savard, Can you provide the exact error message you are getting? Is it the conventional initialization failed error? Provide details on your logic level shift circuit as in is it resistive network or a bidirectional logic shift IC. Try using the SDfat lib for better debugging.

      Also try some other SD card. Sometimes not all cards work with Arduino lib. Use 2 GB cards as they are old capable of working with FAT16 file system. Quite frankly, there are lots of things that can go wrong with SD cards. Sometimes it may be the wiring problems, sometimes it may be a loose contact in the SD card and its slot. Try all possible debugging techniques.

      Good luck!

  • Savard

    Hi Siddharth,

    The code that we are using is below. While the error message is still the conventional initialization failed error.

    while (!card.init(SPI_HALF_SPEED, (10,11,12,13))) {    Serial.println("initialization failed. Things to check:");    Serial.println("* is a card is inserted?");    Serial.println("* Is your wiring correct?");    Serial.println("* did you change the chipSelect pin to match your shield or module?");  }

    I also used 2GB cards formatting to the FAT system. And still unable to get this to work properly.

    I’m hoping if there is a way to connect with you (via skype)? Your insights will be incredibly helpful to help confirm that the hardware and file setups are appropriate? It would be wonderful if you would be able to assist. Thank you!

    • Hey,

      I would be glad to help you. Please use the contact section or drop an email to siddharth[at]embedjournal[dot]com with your contact details. Please don’t use the comment section to reveal personal information.

      Regards
      Siddharth.

  • Hey, i was almost lost there. thank you very much!

    • You are welcome. I’m glad this post helped you.

  • dede44

    thanks a lot, I spent lot of time trying to the solution for my Mega…
    you’re the one !

  • Joy

    Hello Siddharth,
    Thanks for your post. Recently I have started a project in which I need to store data extracted from a sensor in a SD card using Arduino MEGA2560 board. For this without any knowledge I have started working with SD Module of LC Studio (http://www.lctech-inc.com/Hardware/Default.aspx?Id=38dab293-e293-4022-9a8b-9c5abf4b9fe5) but failed to initialize the sd card everytime and every possible way and also found on the internet that this module is a buggy and faulty product.
    So I have decided to switch to ethernet shield and use its microsd slot for storing data. But after seeing your post some questions have popped up on my mind (maybe as i’m a newcomer in arduino world). In the picture I can see the shield is directly fixed on the MEGA board where the pins of the shield doesn’t come in contact with 50-53 pins. AFAIK, for mega the SPI communications is done on the pins 50-52. But for Uno and other classic boards, #10-13 pins do these jobs.
    So my question is isn’t is required to connect #50-52 pins of MEGA board to its #11-13 pins for proper operation (even if I ignore connecting #53 pin) ? If this required can you please upload a photo of complete connection diagram of this shield to work with MEGA board??
    Thanks in advance (I also apologize for my huge comment :3 )

    • Hey Joy,

      There is a 6 pin ICSP connector in the board which is pulled up to the shield as well. It so happens that the SPI pins are available there too.

      This makes the other set of SPI Pins available for the application circuit. I really don’t know if it all fell into place or they planned to have a mega version while making the UNO. I hope that answers your question.

  • Eugene Ankrah

    I am building a temperature data logger with the arduino uno, the Ethernet shield with sd card slot, the real time clock and the 20×4 LCD. I add the <strong>digitalWrite(10, HIGH)after my pinMode(10,HIGH) line of code. It gives me an error. saying “expected primary-expression before ‘<'token. How do I declare strong in the program? The program I have is that the SD card can.t be initialize. Can I get help with that?

    • Eugene Ankrah, <strong> and </strong> are actually HTML syntax for bold text. There was a problem with the code display and the HTML tag got added to the code. I have updated the code in the post. Please copy it again or remove <strong> from the arduino sketch and the error will be gone.

  • Hi , Siddharth !

    From the beginning I want to say that I am a total noob in this area. I have a Mega 2560 R3 board and a very cheap SD card , which I keep trying to make them work together. Just as you said, everything I try from the SD library result in error. After reading your article (thanks for the tip with “digitalWrite(10,HIGH);”), I thought to give it a shot: I’ve changed in every sketch of the SD library “SD.begin(4)” with “SD.begin(53)” or (as appropriate) “const int chipSelect=4;” with “const int chipSelect=53;”. To my surprise, all examples of the SD library worked this time, without even having to change “pinMode(10,OUTPUT);” in “pinMode(53,OUTPUT);” or add the “digitalWrite(10,HIGH);” line. I’m sure there is an explanation for this behavior, but this is beyond my knowledge in this area, because, as I said in the beginning, I’m a total noob. There may be side effects of this hack, but I’ve tested only the SD library examples. I really can’t tell if this information can help you, but I felt the need to share it with you.

    I wish you all the best!

  • Peter Bauer

    Hi Siddharth, this two lines was it. I was searching a lot of files without effort and then it was so easy. Thus It is normal to declare and configurate the outputs. Thanks a lot. Pete

    • You are most welcome. I’m glad I was able to help 🙂

  • vamizrak

    I am a novice with arduino and it happened to purchase a cheap SD module for testing.
    After a day’s troubleshooting and frustration I gave up. The next morning I started over again and it came to my mind to use short cables since my module doesn’t plug into the arduino directly.
    What a surprise….!!! everything worked fine…

    SD module used: http://cdn.instructables.com/F6V/F1OJ/H5ENQ08B/F6VF1OJH5ENQ08B.LARGE.jpg

    I hope this helps.

  • I have searched THIS problem a lot of time (a half year or longer …).
    I change the borad, the card, modified different libraries and so on.
    …and the solution is so “simple” 😮 !
    Now works correct everytime.

    Thx
    full

    • I’m glad that you found this article useful!

      Good luck!

  • IT WORKS GREAT, thank you for this suggestion!

  • how can i put a button to retry this sd card code im useing below

    pinMode(10, OUTPUT);

    if (!SD.begin(10)) {
    lcd.print(“Sd Card Failed!”); //pass or fail if fail Retry or Skip (Skip USB Only not memu for pics)

    lcd.setCursor(1, 1);
    lcd.print(“Skip”);// if buron pres go down to Resetting Motors (skip use only usb only for pc
    lcd.print(” — “);
    lcd.print(“Retry”);

    // retry sd card agean if find said Sd Card Pass if not find go back to skip or retey screen

    return;
    }
    root = SD.open(“/”);

    printDirectory(root, 0);

    lcd.print(“Sd Card Pass!”);

    • Simple, Put the SD init code into a function. then have two button. One to skip SD init and one to retry. Call or don’t call the function based on which button was pressed. You could use the same button for for both with some software logic. Eg. Single click for skip and double click for retry.

      • ok i know hove to do int but they normal at the top for the sketch my code

        const int menu = A0; // sd retry and Menu and burn (menu file delete)
        const int Select = A1; // sd skip and Select and Cancel

        and do function sill get mistake when u them and never use software logic

        here the full code i done
        stepbystepprojects.co.uk/blu_ray_laser_cnc_machine__v0_4_ino.ino

  • Hi Siddharth,

    Congratulations for your website. I’m working to a sd card shield for arduino and I got a problem too. I will make the change you provided here as soon as possible. But just a question, when you do pinMode(10, output) and then digitalWrite(10,HIGH) you just set the pin 10 to high and did not turns the pull up resistor ON. To put ON “pull up” resistor the pin should be set to input. I’m not sure, what do you think?

    • Hi Oliveira,

      You are correct. The Arduino’s pins has to be INPUT to enable the pull up resistor.
      Sorry.. I didn’t word it properly (have changed it now). The point is to turn apply a logic HIGH signal to that pin.

      Thanks!

  • POHU

    Hi Siddharth, I have a relative problem: I try to follow this tutorial http://robotic-controls.com/learn/projects/simple-security-system, and I get “Can’t access SD card. Do not reformat. No card, wrong chip select pin, or SPI problem? SD errorCode: 0X1,0X0” So, I start to check my Adafruit Micro SD Card Breakout Board(https://learn.adafruit.com/adafruit-micro-sd-breakout-board-card-tutorial/library), which is fine. I double check the wire stuff still not figure it out.

    • I thought about your issue. unfortunately, I don’t have anything that can help you.

  • Pingback: Arduino Mega Sd Card Problem()

  • Leo

    Hello Siddharth, thank you very much for your article!

    I am a novice, and in my case, the SD card would only work perfect after a power-down, but never after a reset. (button on the board)
    Your article (pin-10) was definitely the solution. You may publish this on the http://arduino.cc/en/Tutorial/ReadWrite as well?

    Thanks and good luck,

    Leo

    • Thanks Leo, are we allowed to make edits to the official arduino page? I don’t think so. SD cards are power hungry, you will be surprised that such a small component can actually draw so much current. My guess is that your reset button is draining some capacitor (10uF or higher) which at start up was supplying the sudden surge. What board are you using? do you happen to have a schematic? if so send me an email with it.

  • Sudip

    #include

    #include

    const int CS=10;

    void setup()

    {

    Serial.begin(9600);

    pinMode(CS,OUTPUT);

    digitalWrite(CS,HIGH);

    if(!SD.begin(CS))

    {

    Serial.println(“SD CARD INTIALIZATION FAILED”);

    return;

    }

    Serial.println(“Sd card intialization done”);

    }

    void loop()

    {

    }

    SIR But Card is not initializing…..

  • Aswathy sreekumar

    Initializing SD card

    * is a card is inserted?

    * Is your wiring correct?

    * did you change the chipSelect pin to match your shield or module?

    this was the output obtained????
    wht to do????

  • David

    Excellent tip. Thanks!

  • Vjeko

    Thank you verry much! This solved problem with my Mega! I was constantly changing it everywhere to pin 53 and I it never come to my mind to try to leave it as pin 10. Thank you.

  • Kostas Pratilas

    didnt work for my uno!

  • Jatinder Pal Singh (JPS Bhulla

    Wonderful…. It worked like charm. added one line to my sdcard webserver project and its completed now. Thank you Sidharth Bhai….I am using Arduino UNO R3 with Wiznet Ethernet Card

  • jamaliah

    Hi sir
    I had tried using your tips. Unfortunately, the result is failed. What should I do sir. I used Arduino uno with sparkfun microSD Shield. I really appreciate if u can help me.

    • Blue_Pie_Ninja

      you don’t need to do this for a Uno

  • Alejandro Santiago

    Yes yes yes… I was having troubles… at first I try only the sd card and all works perfectly, when I add a 5vcc bus to feed a dth11 and such start to have problems… So I tougth was the voltage variatiosn… good stuff this two lines works like a charm!

  • Amey

    hey mate,
    I am using Arduino Mega and 3.5 Inch LCD Screen.As per the attached image my SD_CS is pin 53 for Arduino Mega.But Still I am getting a error that SD card initialization failed.

    Another thing is I am using a 8 GB micro SD card and I had read in few other forums that it is advisable to use 2 GB or less.I have yet to try this.

    Can you suggest and give me some tips on this?

  • Shrikant janawlekar

    Hi, I have a query. Is the CS pin absolutely necessary. I am falling one pin short on my PIC 18F25J50. If I can simply tie this pin to ground then I won’t have to go for a higher pin count uc. Any help/suggestion is welcome.

  • Shrikant janawlekar

    Hi, I have a query. Is the CS pin of micro sd card absolutely necessary. I am falling one
    pin short on my PIC 18F25J50. If I can simply tie this pin to ground
    then I won’t have to go for a higher pin count uc. Any help/suggestion
    is welcome.

  • Sarvesh Khandelwal

    i have used the above described method but still the SD card gives initialization failed error. Also, if I stop the code in between and one of my files doesn’t get closed, it again starts giving initialization failed error. Is there any way around it?

  • Pat

    Thanks for the idea in your article

    Using Arduino Mega, 128 Gb micro SD card and Cisesco 2.8″ Touch LCD sheild ( rebranded by Maplin )

    In order to get ‘cardinfo’ to detect the card more than once ( and avoid subsequent ‘initialization failed’ messages ) the requirement is to include Tft.TFTinit(); in the ‘cardinfo’ sketch, eg :

    #include
    #include
    #include // add this and the following 2 lines
    #include
    #include


    void setup() {
    Tft.TFTinit();//init TFT add this line
    // Open serial communications and wait for port to open:
    Serial.begin(9600);
    while (!Serial) {

    etc

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

    are NOT required with the Arduino Mega when NOT using the SPI Software library and using ‘TFTv2.h’

    Hope this helps

Keep in touch with the current trends!
Did you like this article? Sign up and get our latest posts delivered to your inbox!
  We hate spam and never share your details.