[RESOLVED] How can I "reboot" an SD Card?

Is there a way to get an SD Card to "reboot" itself so that "SD.begin(SS);" can connect to it again?

When I upload my updated sketch into Arduino, the SD Card stops working. Then I press the reset switch, and it still won't work.

I have to either power-down the Arduino and then power up again; or, I can unplug the SD Card and plug it back in again. Either way, when I press the reset button after that, the SD Card works again. But that's using "human intervention", and I want the SD Card to wake up automatically.

I don't find a procedure in the SD Library to accomplish this. And even disconnecting the 5V supply wire to the card holder (which the AVR could easily do) does not get the SD Card working again.

Is there a command I can send the card through the SPI data wires, to tell it to let go and start over?

No answer? Well, maybe I wasn't specific enough. So here's an example you can try yourself:

#include <SPI.h>
#include <SD.h>


void clearSD()
{
  byte sd = 0;
  digitalWrite(SS, LOW);
  while (sd != 255)
  {
    sd = SPI.transfer(255);
    Serial.print("sd=");
    Serial.println(sd);
  }
  digitalWrite(SS, HIGH);
}

void setup()
{
  Serial.begin(9600);
  pinMode(SS,OUTPUT);
  SPI.begin();
  delay(10);
  boolean b;
  // *** SD Card **
  b = SD.begin(SS);
  if (!b)
  {
    delay(100);
    clearSD();
    delay(100);
    b = SD.begin(SS);
  }
  if (b)
    Serial.println("SD Card started.");
  else
    Serial.println("SD Card failed to start!");
  Serial.println();  
}

void loop() {};

Most likely, the SD Card will correctly start as soon as the script installs (but you can't see the response yet). So press the reset button, and on the Serial Monitor window, you'll see:

sd=255
SD Card failed to start!

Tap the reset button again, and you'll see the same response:

sd=255
SD Card failed to start!

Now remove and reinsert the SD Card (a split-second out it all the time it takes). And again, tap the reset button, and you'll see:

SD Card started.

Now Tap the reset button again, one more time, and you'll again see:

sd=255
SD Card failed to start!

The StackOverflow.com page titled Initializing SD card in SPI issues suggests (right under the heading "4 Answers") to send the number "255" to the card, over and over, until it sends "255" back, meaning you've successfully cleared it.

So that's what I'm doing in this sketch. And although the card responds with a "255" on the very first reply, nothing changes for me. The problem persists.

Why does briefly removing the card work to reset everything?
What spi code can I send the card that will do the same job as briefly removing it?

I have no problem with that sketch. It works fine after the upload and also works when I then push the reset button two or three times in a row. The listfiles sketch in the SD/examples also works.
I'm using an Adafruit uSD breakout board on a NANO. Only thing I can think of is that you check all connections and make sure that they are secure.

Pete

Thank you for actually doing the test, and letting me know the results!

In that case, it must be the model card I'm using. I've read that some SD Cards act a bit differently from others, some putting up a fuss that others don't. And the card I'm using is the cheapest around. (You get what you pay for.) I doubt it would be the card holder, as I think it just passes info straight through to the card.

Your test really helps me, by seriously narrowing things down. Like, now I know there isn't a card-reset code, or they would all require it.

I'll find another solution.

Again, I really thank you for taking my request seriously.

el_supremo:
I have no problem....

Pete

Today, I got an SD Card by SanDisk, and another by Kingston, and found they both work fine, just as you experienced with your Arduino setup. So I'm convinced the make/model SD Card I was using (which has no brand name on it at all) is itself the problem.

Again, thank you for enabling me to figure this out.

Thanks a lot for your posts CosmickGold, I had the same problem as you and I didn't find any solution until now. Now I know I can invest a little bit more with confidence that it'll work fine.

ST

It has been nearly a week since my last post above, and during that time, I still had trouble occasionally with some SD Cards not being recognized. But a few minutes ago, I found an answer that works!

After setting my sketch to continuously try to "boot" a troublesome SD Card, I noticed that with each attempt, my display dimmed a little. Obviously, each attempt was pulling a lot of power.

I had previously learned lot of card holders don't convert very well between the 5.0 volts the USB connection supplies, and the 3.3 volts the SD Card uses, and so send the AVR back a rather poor signal, well below the 5.0 volts the AVR wants but normally enough to "make do". So I figured this visible voltage drop might be reducing the signal even more, below the critical level, below what would "make do" for the AVR.

So I placed an electrolytic capacitor across the SD Card holder's ground and 5-volt terminals; and to my delight, it started working, every time!

Last step was to determine what size capacitor was actually needed. Turns out 100 mfd won't do the job, but 200 mfd does. So I decided on 1000 mfd as the final solution, to be well above critical point.

Hope this helps someone. :slight_smile: