i use the example sketch of SD card presented in Arduino IDE. It runs well, but when i reboot my Adruino Mega by the reset button, the SD card initialization fails. To fix it, i need to remove the SD card, insert it in his socket and reboot the board again. Then, SD card initialization is OK. How can i update my code to try initialize the SD card until initialization is OK? Also, can you explain me where come this bug ?

See the example SD card setup on the Arduino Mega:

Serial.print("Initializing SD card...");

  if (!SD.begin(53)) {
    Serial.println("initialization failed!");
    while (1);
  }
  Serial.println("initialization done.");
  • It is probably not a bug, not all sd cards are compatible with the sd library. Are you resetting the arduino while the sd card is busy? Perhaps the socket on its module is not suitable for 5v. Perhaps you use a breadboard. Perhaps the wires are too long Perhaps the arduino mega has not enough voltage due to a cheap usb cable. There are many more things that can be wrong. – Jot Dec 6 at 14:04
  • is some other SPI device attached? – Juraj Dec 6 at 14:05
  • No other SPI device attached. I use usb cable and 20cm wires to link sd card to the arduino. I will try the work with an external 9V power supply and maybe see you soon... – Teddol Dec 6 at 14:21
  • After using an external power supply, initialization works well after 10 reboots !!! You're write Jot, using only the usb cable, voltage on the CS pin of SD card device is about 4.5V. With an external voltage high 5V for the arduino, CS pin of SD card device is 4.99V. I think that the problem came from here. Thanks ! – Teddol Dec 6 at 14:31
  • A sd card is 3.3v (or at least compatible with 3.3v). Can you show a photo of the module with the socket? Is there a chip on the module to convert the 3.3v signals from the sd card to the 5v signals of the arduino? – Jot Dec 6 at 15:13
up vote 0 down vote accepted

I just add an external 9V power supply to my Arduino Mega. The SD device is power by arduino 5V pinout.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.