Initializing SD card... initialization failed

#include <LiquidCrystal.h>
#include "DHT.h"
#include <SPI.h>
#include <SD.h>

#define DHTPIN 2     // Digital pin connected to the DHT sensor

#define DHTTYPE DHT11   // DHT 11
//#define DHTTYPE DHT22   // DHT 22  (AM2302), AM2321
//#define DHTTYPE DHT21   // DHT 21 (AM2301)

const int rs = 8, en = 7, d4 = 6, d5 = 5, d6 = 4, d7 = 3;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);


File myFile;

void setup() {
  // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);
  // Open serial communications and wait for port to open:
  pinMode(10, OUTPUT);
  digitalWrite(10, HIGH);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  Serial.print("Initializing SD card...");
  if (!SD.begin(10)) {
    Serial.println("initialization failed!");
    while (1);

  Serial.println("initialization done.");
  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  myFile ="test.txt", FILE_WRITE);
  // if the file opened okay, write to it:


void loop() {
  // Wait a few seconds between measurements.

  // Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  int h = dht.readHumidity();
  // Read temperature as Celsius (the default)
  float t = dht.readTemperature();
  // Read temperature as Fahrenheit (isFahrenheit = true)
  float f = dht.readTemperature(true);

  // Check if any reads failed and exit early (to try again).
  if (isnan(h) || isnan(t) || isnan(f)) {
    Serial.println(F("Failed to read from DHT sensor!"));

  // Photoresistor
  int value = analogRead(A0);
  int l = (value / 1023) * 100000;

  lcd.setCursor(0, 0);
  lcd.print(F("T: "));
  lcd.print(F("C "));
  lcd.setCursor(1, 1);
  lcd.print(F("       Hum: "));
  lcd.println(F("% "));
  lcd.setCursor(0, 1);
  lcd.print("L: ");

  if (myFile) {
    Serial.print("Writing to SD as text.txt");

    while (1) {
      Serial.print("Humedad: ");
      myFile.print("Humedad: ");
      Serial.print("Temperatura: ");
      myFile.print("Temperatura: ");
      Serial.print("Luz: ");
      myFile.print("Luz: ");
    // close the file:
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");


sorry I posted all the code, but the problem I´m having is that I start my arduino, go to my serial monitor an well it displays this "Initializing SD card... initialization failed" so what I do is remove the SD card from the module, then reinsert it, run the serial monitor again and it works, every time, has anyone had this problem and know how to fix it?

This is my schematic of the whole circuit

Thought resolution was better, the configuration I'm using is GND, N/A, GPIO 10, 11,13 and 12

Have you tried the SD card alone with an example file?

@Jaime_Rodriguez_Flores, please edit your post, select all code and click the </> button to apply code tags and next save your post. It makes it easier to read, easier to copy and prevents the forum software from incorrect interpretation of the code.

You might try adding a couple seconds delay before trying to initialize the SD card. SD cards require some "boot" time after power is brought up.

Thank you, this was my first question so now I know

I tried it but same result :confused:

Yes, I tried every module separately before compiling them into one program, but I'll probably try again

Maybe its an issue with the SD board. Do you have two SPI devices in your project? If so the issue is with the SD cards/boards.

I found the SD Cards (including Sparkfun and AdaFruit) do not tristate the MISO pin of the SD board.

More detailed explanation:

ON every SPI slave board (ie any SPI sensor or SD or RTC etc) should "disconnect" the MISO pin when CS tells the SD board to not use the bus. However the SD board does not "release" the MISO pin so it remains an output when the processor is trying to talk to other SPI devices.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.