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

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

  1. Tizian

    Hello,
    at the moment I´m working at a projekt with an Arduino Pro Mini. I´m also using the SD-Card Adapter you´ve showed.
    Is it possible for you to send me the Fritzing-file of the SD-Card Adapter you created? That would be great.

    Thanks in advance.

    Yours sincerely
    Tizian

    Reply
  2. A. Amador

    Hi, I´m working in a project too but I´m working with a PT100 and a RTD converter, a “MAX31865”, and saving my temperature value´s in a Micro SD Card and I can´t find the Micro SD Adapter file for fritzing and in my research I find this publication, would it be possible to you to send me the fritzing file ? it would be very helpful.

    Thanks in advance

    Reply
      1. S.Oliveira

        Hi Mr. Schöffler,
        could you also send me the fritzing file for the MicroSD Card-Module I’m currently working on my Master Thesis about this Topic and would really appreciate it. Thank you.

        Reply
    1. Adina

      I am in the same situation, I couldn’t find the micro sd adapter file for fritzing and I find this publication.
      Can you help me too?

      Thanks

      Reply
  3. Daniel Lachance

    Good day, Michael.

    Great tutorial and if works great. Thank you very much. I would require the SD Card Fritzing file too, please! Hahaha! Thank you for writing back to me. I would have another question for you.

    I want to store several ‘short’ and ‘float’ variables to the file and being able to retrieve them one at a time, sequentially without having to read the whole file at once. Something like the following sequence.

    open file,
    write variable a
    write variable b
    write variable c
    close file

    wait 500ms

    open file,
    read variable a
    variable x = variable a
    close file

    open file,
    read variable a … discard variable a
    read variable b
    variable y = variable b
    close file

    and so on. my problem is that my file is getting bigger and bigger every minute, as a new series of data gets added to it. But for the purpose if my application, only the last 384 series on variables would be needed et reconstitute a graphic. I can manage the pointers and indexes at will, for it the file contains 5000 entries, I can discard the first few and keep only the last 384 variables. But I need a way to get them in the same sequence they were written, of course.

    Can I do that ? Could you please show me just how I coud achieve this function ?

    Thank you for your generous help. Until then, I wish you have a great day.

    Reply
  4. Marco Fabiani

    Hello! First of all thank you for the tutorial, it was clear and helpfull. I am using the sd card to store data from sensors during the flight of a model rocket. I am doing also the fritzing sketch of my project, but I have not been able to find the sd card adapter part anywhere. Would it be possible for you to share it with me?
    Thank you in advance

    Reply
  5. Sanjeev Kumar TM

    Hi, I´m working on a project too but I´m working with a SI7021”, and saving my temperature and humidity value´s in a Micro SD Card and I can´t find the Micro SD Adapter file for fritzing so is it possible to send me the fritzing file ? it would be very helpful.

    If i can write the data with respect to time and if power loss happen how can i continue to write a data to the SD card with earlier data.

    Reply
  6. sravya

    hi i connected sd card interfacing with Arduino uno and i given pin description MOSI 11, MISO 12, SCK 13, CS 10, iam try to save the ultrasonic sensor values in sd card excel format if any body can know this please reply me thank you

    Reply

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.