How to use the “MicroSD Card Adapter” with the Arduino Uno

Pin layout of MicroSD Card Adapter and Arduino Uno.

In this tutorial, it is shown how to read and write SD cards utilizing the “MicroSD Card Adapter” module and Arduino Uno. An major advantage of the module is that it can be used with the SD library that comes with the Arduino IDE. The SD library makes initialization, reading, and writing of the card very easy.

List of materials:
– Arduino Uno [Search on Aliexpress | Amazon]
– Jumper wires [Search on Aliexpress | Amazon]
– MicroSD Card Adapter [Search on Aliexpress | Amazon]
– MicroSD card [Search on Aliexpress | Amazon]

How to connect the “MicroSD Card Adapter” module to the Arduino Uno?

Schematic view of pin layout.

The module comes with a voltage regulator. Therefore, the Arduino’s 5V and 3.3V pin can be used for the voltage supply. The module communicates via SPI (Serial Peripheral Interface) to the Arduino Uno.  The following table shows the complete pin layout.

MicroSD Card Adapter Pin Arduino Uno Pin
CS 4
SCK 13
MOSI 11
MISO 12
VCC 5V
GND GND

How to program the RFID reader?
As mentioned before, reading and writing an SD card is very simple when the standard SD library of the Arduino IDE is used. Make sure to use the latest version of the SD library (Sketch -> Include Library -> Manage Libraries -> Search for “SD”). For example, in my case, version 1.1.0 did not work with the module. Fortunately, version 1.1.1 did work without any problems. Moreover, the SD card must be formatted as FAT16 or FAT32. If something does not work as expected, a good start for debugging is always to upload CardInfo example of the library (File -> Examples -> SD -> CardInfo) to the Arduino and read the messages of the serial monitor.
In this tutorial’s code, a random number between 0 and 9 is written to an SD card. In particular, the number is written to a file named “file.txt”. Next, the content of “file.txt” is read. At the end of the loop function, a delay of 5 seconds is added. Please note that when the Arduino is started, it is checked whether a file named “file.txt” exists (see setup function). If so, the file is deleted.

// (c) Michael Schoeffler 2016, http://www.mschoeffler.de
#include <SD.h> //Load SD library

int chipSelect = 4; //chip select pin for the MicroSD Card Adapter
File file; // file object that is used to read and write data

void setup() {
  Serial.begin(9600); // start serial connection to print out debug messages and data

  pinMode(chipSelect, OUTPUT); // chip select pin must be set to OUTPUT mode
  if (!SD.begin(chipSelect)) { // Initialize SD card
    Serial.println("Could not initialize SD card."); // if return value is false, something went wrong.
  }

  if (SD.exists("file.txt")) { // if "file.txt" exists, fill will be deleted
    Serial.println("File exists.");
    if (SD.remove("file.txt") == true) {
      Serial.println("Successfully removed file.");
    } else {
      Serial.println("Could not removed file.");
    }
  }
}

void loop() {
  file = SD.open("file.txt", FILE_WRITE); // open "file.txt" to write data
  if (file) { 
    int number = random(10); // generate random number between 0 and 9
    file.println(number); // write number to file
    file.close(); // close file
    Serial.print("Wrote number: "); // debug output: show written number in serial monitor
    Serial.println(number);
  } else {
    Serial.println("Could not open file (writing).");
  }

  file = SD.open("file.txt", FILE_READ); // open "file.txt" to read data
  if (file) {
    Serial.println("--- Reading start ---"); 
    char character; 
    while ((character = file.read()) != -1) { // this while loop reads data stored in "file.txt" and prints it to serial monitor 
      Serial.print(character);
    } 
    file.close();
    Serial.println("--- Reading end ---"); 
  } else {
    Serial.println("Could not open file (reading).");
  } 
  delay(5000); // wait for 5000ms
}

If everything works correctly, the serial monitor should show a similar output as shown in the following screenshot:

Serial monitor output while using MicroSD Card Adapter.

Video Tutorial

Leave a Reply

Your email address will not be published. Required fields are marked *