Unable to initialize SD card

Hello,

I am not able to initialize the SD card. Can't find figure out what the issue is. It fails at the condition

if (!SD.begin(D8)) {
    Serial.println("initialization failed!");
    return;
  }

I am using D1 Mini V2 board and standard SD card module. Pin configuration attached with images below.

Connections are -

GND => GND
VCC => 3.3V
MISO => D6
MOSI => D7
SCK => D5
CS => D8

Code is -

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

File myFile;

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

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

  if (!SD.begin(D8)) {
    Serial.println("initialization failed!");
    return;
  }
  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 = SD.open("test.txt", FILE_WRITE);

  // if the file opened okay, write to it:
  if (myFile) {
    Serial.print("Writing to test.txt...");
    myFile.println("testing 1, 2, 3.");
    // close the file:
    myFile.close();
    Serial.println("done.");
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }

  // re-open the file for reading:
  myFile = SD.open("test.txt");
  if (myFile) {
    Serial.println("test.txt:");

    // read from the file until there's nothing else in it:
    while (myFile.available()) {
      Serial.write(myFile.read());
    }
    // close the file:
    myFile.close();
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }
}

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

board.PNG

module.PNG

You're initializing incorrectly. Take a look at the CardInfo example sketch in the Arduino IDE.

You didn't show us the top of the SD module, but if it has a voltage regulator then you probably need to supply 5V to the SD module's Vcc pin. You're applying 3.3V at the input of a 3.3V regulator. I guess it might just pass it through, but it might not.

The module will also have a xxx125 or similar level shifting chip which normally converts the 5V SPI lines coming from the processor to the 3.3V the SD card needs to see. But since your D1 Mini is outputting 3.3V already, that level shifter isn't needed. But I think it doesn't hurt anything for it to be there.

If you don't have 5V available, you should look for a plain microSD breakout module that doesn't have the regulator or level shifter. You can just connect the SPI lines directly to the D1 Mini.

ShermanP:
You didn't show us the top of the SD module, but if it has a voltage regulator then you probably need to supply 5V to the SD module's Vcc pin. You're applying 3.3V at the input of a 3.3V regulator. I guess it might just pass it through, but it might not.

The module will also have a xxx125 or similar level shifting chip which normally converts the 5V SPI lines coming from the processor to the 3.3V the SD card needs to see. But since your D1 Mini is outputting 3.3V already, that level shifter isn't needed. But I think it doesn't hurt anything for it to be there.

If you don't have 5V available, you should look for a plain microSD breakout module that doesn't have the regulator or level shifter. You can just connect the SPI lines directly to the D1 Mini.

This worked out really well. Thanks!

Changed Vcc => 5V and it started working.

The issue is now solved. Thanks again.