Arduino projects

MicroSD card adapter module Arduino Uno

In this tutorial, we will introduce how to use the MicroSD card adapter module and the Arduino Uno development board to read and write SD cards. The main advantage of this module is that it can be used with the SD library that comes with the Arduino IDE. The SD library makes it very easy to initialize, read, and write to the card.

MicroSD card adapter module  Arduino Uno

List of Materials Required:

– Arduino Uno development board

– Jumper wires

– MicroSD card adapter module

– MicroSD card

How to connect a MicroSD Card Adapter Module to an Arduino Uno?

The module has a voltage regulator onboard. Hence, the 5V and 3.3V pins of the Arduino can be used as a power source. The module communicates with the Arduino Uno via SPI (Serial Peripheral Interface). The following table shows the complete pin connections.

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

MicroSD card adapter module  Arduino Uno

How do I program an SD card reader?

As mentioned before, reading and writing SD cards is very simple when using the standard SD library of the Arduino IDE. Make sure to use the latest version of the SD library ( Sketch-> Include Library-> Manage Libraries -> search for “SD”). For example, version 1.1.0 does not work with the module in this article. Fortunately, version 1.1.1 works fine. In addition, the SD card must be formatted as FAT16 or FAT32. If something does not work as expected, always upload the library’s CardInfo example (File -> Examples-> SD-> CardInfo) to the Arduino when debugging and read the messages of the serial monitor.

In the code for this article, a random number between 0 and 9 is written to the SD card. The number is written to a file called “file.txt”. Next, the contents of “file.txt” are read. At the end of the loop function, a 5 second delay is added. Note that when the Arduino is started, a check is made to see if a file called “file.txt” exists. If it does, the file is deleted.

  1. #include <SD.h> //Load SD library
  2. int chipSelect = 4; //chip select pin for the MicroSD Card Adapter
  3. File file; // file object that is used to read and write data
  4. void setup() {
  5.   Serial.begin(9600); // start serial connection to print out debug messages and data
  6.   pinMode(chipSelect, OUTPUT); // chip select pin must be set to OUTPUT mode
  7.   if (!SD.begin(chipSelect)) { // Initialize SD card
  8.     Serial.println(“Could not initialize SD card.”); // if return value is false, something went wrong.
  9.   }
  10.   if (SD.exists(“file.txt”)) { // if “file.txt” exists, fill will be deleted
  11.     Serial.println(“File exists.”);
  12.     if (SD.remove(“file.txt”) == true) {
  13.       Serial.println(“Successfully removed file.”);
  14.     } else {
  15.       Serial.println(“Could not removed file.”);
  16.     }
  17.   }
  18. }
  19. void loop() {
  20.   file = SD.open(“file.txt”, FILE_WRITE); // open “file.txt” to write data
  21.   if (file) {
  22.     int number = random(10); // generate random number between 0 and 9
  23.     file.println(number); // write number to file
  24.     file.close(); // close file
  25.     Serial.print(“Wrote number: “); // debug output: show written number in serial monitor
  26.     Serial.println(number);
  27.   } else {
  28.     Serial.println(“Could not open file (writing).”);
  29.   }
  30.   file = SD.open(“file.txt”, FILE_READ); // open “file.txt” to read data
  31.   if (file) {
  32.     Serial.println(“— Reading start —“);
  33.     char character;
  34.     while ((character = file.read()) != -1) { // this while loop reads data stored in “file.txt” and prints it to serial monitor
  35.       Serial.print(character);
  36.     }
  37.     file.close();
  38.     Serial.println(“— Reading end —“);
  39.   } else {
  40.     Serial.println(“Could not open file (reading).”);
  41.   }
  42.   delay(5000); // wait for 5000ms
  43. }

Copy code

If everything went well, the serial monitor should show similar output as shown in the following screenshot:

MicroSD card adapter module  Arduino Uno

Back to top button