Skip to content
Hibiiro

HIBIIRO

How to Use an SPI MicroSD Card Reader Module with Arduino

Introduction

I picked up a batch of inexpensive SPI MicroSD card adapter modules (around 24 JPY each unsoldered) and tested reading and writing files from an Arduino Uno.

Parts & Wiring (SPI)

Using Arduino IDE 2.3.4 and the built-in SD library (v1.3.0), connect the module to the Arduino’s hardware SPI pins: CS → D10, MOSI → D11, MISO → D12, and SCK → D13. (You can highlight all SPI pins on your board using our Interactive MCU Pinout Viewer.) Note: Check whether your module has an onboard 5V-to-3.3V regulator (`VCC = 5V`) or requires `3.3V` directly.

Arduino Uno to SPI MicroSD module wiring diagram

Checking the Card with the CardInfo Example

Before writing a file, run the SD library’s CardInfo example. It checks the card and reports the volume information over the serial connection.

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

Sd2Card card;
SdVolume volume;
SdFile root;


const int chipSelect = 10;

void setup() {
  Serial.begin(9600);
  while (!Serial) {
    ; 
  }


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

  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?");
    Serial.println("Note: press reset button on the board and reopen this Serial Monitor after fixing your issue!");
    while (1);
  } else {
    Serial.println("Wiring is correct and a card is present.");
  }

  Serial.println();
  Serial.print("Card 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");
  }

  if (!volume.init(card)) {
    Serial.println("Could not find FAT16/FAT32 partition.\nMake sure you've formatted the card");
    while (1);
  }

  Serial.print("Clusters:          ");
  Serial.println(volume.clusterCount());
  Serial.print("Blocks x Cluster:  ");
  Serial.println(volume.blocksPerCluster());

  Serial.print("Total Blocks:      ");
  Serial.println(volume.blocksPerCluster() * volume.clusterCount());
  Serial.println();

  uint32_t volumesize;
  Serial.print("Volume type is:    FAT");
  Serial.println(volume.fatType(), DEC);

  volumesize = volume.blocksPerCluster();    
  volumesize *= volume.clusterCount();     
  volumesize /= 2;                           
  Serial.print("Volume size (KB):  ");
  Serial.println(volumesize);
  Serial.print("Volume size (MB):  ");
  volumesize /= 1024;
  Serial.println(volumesize);
  Serial.print("Volume size (GB):  ");
  Serial.println((float)volumesize / 1024.0);

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

  root.ls(LS_R | LS_DATE | LS_SIZE);
  root.close();
}

void loop(void) {
}

Open the Serial Monitor at 9600 baud to see the card information:

CardInfo output in the Arduino Serial Monitor

Creating a Directory, Writing & Reading a File

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

Sd2Card card;
SdVolume volume;
SdFile root;
File myFile;

const int chipSelect = 10;
const char info[] = "Hibiiro is an indie maker blog and tool site.";

void setup() {
  Serial.begin(9600);
  while (!Serial) { ; }

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

  if (!card.init(SPI_HALF_SPEED, chipSelect) || !SD.begin(chipSelect)) {
    Serial.println("SD initialization failed!");
    while (1);
  }

  if (!volume.init(card)) { 
    Serial.println("Could not find FAT16/FAT32 partition.");
    while (1);
  }

  Serial.println("SD card initialized.");

  if (!SD.exists("hibiiro")) {
    SD.mkdir("hibiiro");
  } 

  if (!SD.exists("hibiiro/hibiiro.txt")) {
    myFile = SD.open("hibiiro/hibiiro.txt", FILE_WRITE);
    myFile.write(info);
    myFile.close();
  } else {
    myFile = SD.open("hibiiro/hibiiro.txt", FILE_READ);
    while (myFile.available()) {
      Serial.write(myFile.read());
    }
    myFile.close();
  }

  Serial.println("");
  root.openRoot(volume);
  root.ls(LS_R | LS_DATE | LS_SIZE);
}

void loop() {}
Serial monitor output showing created file on MicroSD card