Initializing SD card failure.

Hello, I am trying to make a basic micro SD card reader with Arduino.
However, the original micro SD card reader doesn’t have a voltage converter in it, so I used the resistor divider to convert 5v to 3.3v. Do I need a logic converter(5v to 3.3v)? The code I’m using is from the SD library, and initialization fails.

Here’s the code:

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

// set up variables using the SD utility library functions:
Sd2Card card;
SdVolume volume;
SdFile root;

// change this to match your SD shield or module;
// Arduino Ethernet shield: pin 4
// Adafruit SD shields and modules: pin 10
// Sparkfun SD shield: pin 8
const int chipSelect = 4;

void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }


  Serial.print("\nInitializing SD card...");

  // we'll use the initialization code from the utility libraries
  // since we're just testing if the card is working!
  if (!card.init(SPI_HALF_SPEED, chipSelect)) {
    Serial.println("initialization failed. Things to check:");
    Serial.println("* is a card inserted?");
    Serial.println("* is your wiring correct?");
    Serial.println("* did you change the chipSelect pin to match your shield or module?");
    return;
  } else {
    Serial.println("Wiring is correct and a card is present.");
  }

  // print the type of card
  Serial.print("\nCard type: ");
  switch (card.type()) {
    case SD_CARD_TYPE_SD1:
      Serial.println("SD1");
      break;
    case SD_CARD_TYPE_SD2:
      Serial.println("SD2");
      break;
    case SD_CARD_TYPE_SDHC:
      Serial.println("SDHC");
      break;
    default:
      Serial.println("Unknown");
  }

  // Now we will try to open the 'volume'/'partition' - it should be FAT16 or FAT32
  if (!volume.init(card)) {
    Serial.println("Could not find FAT16/FAT32 partition.\nMake sure you've formatted the card");
    return;
  }


  // print the type and size of the first FAT-type volume
  uint32_t volumesize;
  Serial.print("\nVolume type is FAT");
  Serial.println(volume.fatType(), DEC);
  Serial.println();

  volumesize = volume.blocksPerCluster();    // clusters are collections of blocks
  volumesize *= volume.clusterCount();       // we'll have a lot of clusters
  volumesize *= 512;                            // SD card blocks are always 512 bytes
  Serial.print("Volume size (bytes): ");
  Serial.println(volumesize);
  Serial.print("Volume size (Kbytes): ");
  volumesize /= 1024;
  Serial.println(volumesize);
  Serial.print("Volume size (Mbytes): ");
  volumesize /= 1024;
  Serial.println(volumesize);


  Serial.println("\nFiles found on the card (name, date and size in bytes): ");
  root.openRoot(volume);

  // list all files in the card with date and size
  root.ls(LS_R | LS_DATE | LS_SIZE);
}


void loop(void) {

}

abyun819: Hello, I am trying to make a basic micro SD card reader with Arduino. However, the original micro SD card reader doesn't have a voltage converter in it, so I used the resistor divider to convert 5v to 3.3v. Do I need a logic converter(5v to 3.3v)? The code I'm using is from the SD library, and initialization fails.

Yes, you need dedicated logic level shifter IC (HEF4050 or 74HC4050 which both work on 3.3V). With resistor based voltage divider, SPI transfer may become unreliable on higher speed after card initialization, but as well during initialization.

OK Thanks. :)

Do you think this might work?

http://www.waveshare.com/wiki/Logic_Level_Converter

Thank you

That is for bidirectional level shifting board based on BSS138 MOSFET (5V to 3.3V and vice versa).

You can use it. However, these recommended unidirectional level shifter, are mostly used with SPI, they are much cheaper and have their advantages. With upper board, you have to ensure that voltage difference is not out of spec for used MOSFET (for instance that cannot be used between 1.8V and 5V devices), otherwise you may damage device at lower level voltage side.