SD Card Initialization not working

Hello There,

I am currently trying to use an SD Card Reader on an arduino micro. But i get error messages about it not being able to initialize the SD Card.

here is my code:

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

const uint8_t sd_cs_pin = 4;

void setup() {
  Serial.begin(9600);
  while(!Serial); // wait for USB to get connected

  while(!SD.begin(sd_cs_pin)) {
    Serial.print(millis());
    Serial.println(": Failed to get SD Card");
  }
}

void loop() {}

my wiring looks like this:


When the SD Card is not plugged in, it gives me that error roughly every second. when it is, it gives it to me alround 50 times per second. i am deducing from that that i wired everything correctly, since i get different results when the card is plugged in / not plugged in.

also, the SD Card is formatted as FAT16.

Might be that you forget to add
#include <SPI.h>
The SD Card is addresses as a SPI

nope, not it. tried, still same problem.

I don't know this area very well but I have seen positive things written about an alternative library SDFAT https://docs.arduino.cc/libraries/sdfat/

sure there is, do you think it will work better?

i will definitely try though, thanks.

What size SD card?

Edit: Check your wiring. Are you certain the SCK, MISO and MOSI are correct.

Your pic isn’t clear.

sorry for unclear image. you can technically see what connects to what by looking at the colors but yeah...

yep, i checked the wiring, by re-wiring the thing multiple times.

i tested with 1GB / 4GB sd cards.

also: works now using the other library mentoined by @6v6gt.

I prefer SdFat library. Allows the use of long filenames. SD library is still 8.3 filename limited.

Did you try SD.h using D10 as the CS pin?

Edit: Or if you want to use D4 for some reason, you still have to have D10 configured as an output. SdFat probably does this for you, but SD.h probably does not. Google's AI provides this wisdom, which may evey be right for once:

Even if not using D10 as the Chip Select (CS) pin for your SD card, D10 still needs to be configured as an output when using the Arduino SD library.

"This is because D10 (the SS pin on many Arduino boards) is part of the SPI (Serial Peripheral Interface) communication protocol that the SD library utilizes. The SPI communication relies on the SS pin being in an output mode to function correctly, even if it's not actively being used to select a specific device. The SD library often implicitly expects this configuration for proper operation of the underlying SPI hardware.

Therefore, including pinMode(10, OUTPUT); in your setup() function is a recommended practice when working with the SD library, regardless of which pin you designate as your SD card's CS pin in SD.begin()."

But if you use D10 as the CS, it's automatically set as an output.