Arduino Nano - Micro SD Card

This tutorial instructs you how to use the Micro SD Card with Arduino Nano. In detail, we will learn:

Arduino Nano Micro SD Card

Hardware Preparation

1×Arduino Nano
1×USB A to Mini-B USB cable
1×Micro SD Card
1×Micro SD Card Module
1×Jumper Wires
1×Breadboard
1×(Optional) MicroSD to SD Memory Card Adapter
1×(Recommended) Screw Terminal Expansion Board for Arduino Nano
1×(Recommended) Breakout Expansion Board for Arduino Nano
1×(Recommended) Power Splitter For Arduino Nano

Or you can buy the following sensor kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you.
Additionally, some of these links are for products from our own brand, DIYables.

Overview of Micro SD Card Module

The Micro SD Card Module is a connection between Arduino Nano and a Micro SD Card. It facilitates communication between the two, allowing the Arduino Nano to access the Micro SD Card.

The Micro SD Card Module Pinout

Micro SD Card Module pinout

The Micro SD Card Module has 6 pins:

  • VCC pin: This should be connected to the Arduino's 5V pin.
  • GND pin: This should be connected to the Arduino's GND.
  • MISO pin: (Master In Slave Out) This should be connected to the Arduino's MOSI pin.
  • MOSI pin: (Master Out Slave In) This should be connected to the Arduino's MISO pin.
  • SCK pin: This should be connected to the Arduino's SCK pin.
  • SS pin: (Slave Select) This should be connected to the pin specified in Arduino Nano code as a SS pin.

Preparation

Make sure that the Micro SD Card is formatted as either FAT16 or FAT32 (search online for more information).

Wiring Diagram

  • You can use male-to-female jumper wires to connect the micro SD Card module to Arduino Nano
The wiring diagram between Arduino Nano and Micro SD Card Module

This image is created using Fritzing. Click to enlarge image

  • Or you can plug the micro SD Card module to breadboard and then use the male-to-male jumper wires
The wiring diagram between Arduino Nano and Micro SD Card Module

This image is created using Fritzing. Click to enlarge image

Arduino Nano - How to open a file on Micro SD Card and create if not existed

Arduino Nano Code

Detailed Instructions

  • Place the Micro SD Card into the Micro SD Card module.
  • Follow the wiring diagram to connect the Micro SD Card module to the Arduino Nano.
  • Connect the Arduino Nano to a computer using a USB cable.
  • Open the Arduino IDE and select the appropriate board and port.
  • Open the Serial Monitor in the Arduino IDE.
  • Copy and paste the code below into the Arduino IDE.
/* * This Arduino Nano code was developed by newbiely.com * * This Arduino Nano code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano/arduino-nano-micro-sd-card */ #include <SD.h> #define PIN_SPI_CS 4 File myFile; void setup() { Serial.begin(9600); if (!SD.begin(PIN_SPI_CS)) { Serial.println(F("SD CARD FAILED, OR NOT PRESENT!")); while (1); // don't do anything more: } Serial.println(F("SD CARD INITIALIZED.")); if (!SD.exists("arduino.txt")) { Serial.println(F("arduino.txt doesn't exist. Creating arduino.txt file...")); // create a new file by opening a new file and immediately close it myFile = SD.open("arduino.txt", FILE_WRITE); myFile.close(); } // recheck if file is created or not if (SD.exists("arduino.txt")) Serial.println(F("arduino.txt exists on SD Card.")); else Serial.println(F("arduino.txt doesn't exist on SD Card.")); } void loop() { }
  • Click the Upload button on the Arduino IDE to compile and upload the code to the Arduino Nano.
  • Check the Serial Monitor for the output of the first execution.
COM6
Send
SD CARD INITIALIZED. arduino-nano.txt doesn't exist. Creating arduino-nano.txt file... arduino-nano.txt exists on SD Card.
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  
  • The output that will appear on the Serial Monitor for subsequent executions
COM6
Send
SD CARD INITIALIZED. arduino-nano.txt exists on SD Card.
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

※ NOTE THAT:

You may not observe the output on the Serial Monitor if the first upload is done before opening the Serial Monitor.

  • Remove the Micro SD Card from the module.
  • Put the Micro SD Card into an USB SD Card reader.
  • Link the USB SD Card reader to the PC.
  • Verify if the file is present or not.

Arduino Nano - How to write/read data to/from a file on Micro SD Card

The following code:

  • Saves data to a file
  • Reads the content of the file character-by-character and displays it on the Serial Monitor
/* * This Arduino Nano code was developed by newbiely.com * * This Arduino Nano code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano/arduino-nano-micro-sd-card */ #include <SD.h> #define PIN_SPI_CS 4 File myFile; void setup() { Serial.begin(9600); if (!SD.begin(PIN_SPI_CS)) { Serial.println(F("SD CARD FAILED, OR NOT PRESENT!")); while (1); // don't do anything more: } Serial.println(F("SD CARD INITIALIZED.")); // open file for writing myFile = SD.open("arduino.txt", FILE_WRITE); if (myFile) { myFile.println("Created by ArduinoGetStarted.com"); // write a line to Arduino myFile.println("Learn Arduino and SD Card"); // write another line to Arduino myFile.close(); } else { Serial.print(F("SD Card: error on opening file arduino.txt")); } // open file for reading myFile = SD.open("arduino.txt", FILE_READ); if (myFile) { while (myFile.available()) { char ch = myFile.read(); // read characters one by one from Micro SD Card Serial.print(ch); // print the character to Serial Monitor } myFile.close(); } else { Serial.print(F("SD Card: error on opening file arduino.txt")); } } void loop() { }
  • The Serial Monitor displayed the contents of the file.
COM6
Send
Created by ArduinoGetStarted.com Learn Arduino Nano and SD Card
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

※ NOTE THAT:

By default, the data will be added to the end of the file. If you restart Arduino Nano with the code above, the text will be added to the file again. This will result in the Serial Monitor displaying more lines as follows:

COM6
Send
Created by ArduinoGetStarted.com Learn Arduino Nano and SD Card Created by ArduinoGetStarted.com Learn Arduino Nano and SD Card
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

You can remove the Micro SD Card from the module and view its contents on your computer using a USB SD Card reader.

Arduino Nano - How to read a file on Micro SD Card line-by-line

/* * This Arduino Nano code was developed by newbiely.com * * This Arduino Nano code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano/arduino-nano-micro-sd-card */ #include <SD.h> #define PIN_SPI_CS 4 File myFile; void setup() { Serial.begin(9600); if (!SD.begin(PIN_SPI_CS)) { Serial.println(F("SD CARD FAILED, OR NOT PRESENT!")); while (1); // don't do anything more: } Serial.println(F("SD CARD INITIALIZED.")); // open file for writing myFile = SD.open("arduino.txt", FILE_WRITE); if (myFile) { myFile.println("Created by ArduinoGetStarted.com"); // write a line to Arduino myFile.println("Learn Arduino and SD Card"); // write another line to Arduino myFile.close(); } else { Serial.print(F("SD Card: error on opening file arduino.txt")); } // open file for reading myFile = SD.open("arduino.txt", FILE_READ); if (myFile) { int line_count = 0; while (myFile.available()) { char line[100]; // maximum is 100 characters, change it if needed int line_length = myFile.readBytesUntil('\n', line, 100); // read line-by-line from Micro SD Card line_count++; Serial.print(F("Line ")); Serial.print(line_count); Serial.print(F(": ")); Serial.write(line, line_length); // print the character to Serial Monitor // \n character is escaped by readBytesUntil function Serial.write('\n'); // print a new line charactor } myFile.close(); } else { Serial.print(F("SD Card: error on opening file arduino.txt")); } } void loop() { }
  • The outcome displayed on the Serial Monitor.
COM6
Send
SD CARD INITIALIZED. Line 1: Created by ArduinoGetStarted.com Line 2: Learn Arduino Nano and SD Card
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

※ NOTE THAT:

You may observe additional lines on the Serial Monitor if the file's content is not erased beforehand.

Arduino Nano - How to overwrite a file on Micro SD Card

By default, the content will be added to the end of the file. The easiest way to overwrite a file is to delete the existing file and create a new one with the same name.

/* * This Arduino Nano code was developed by newbiely.com * * This Arduino Nano code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano/arduino-nano-micro-sd-card */ #include <SD.h> #define PIN_SPI_CS 4 File myFile; void setup() { Serial.begin(9600); if (!SD.begin(PIN_SPI_CS)) { Serial.println(F("SD CARD FAILED, OR NOT PRESENT!")); while (1); // don't do anything more: } Serial.println(F("SD CARD INITIALIZED.")); SD.remove("arduino.txt"); // delete the file if existed // create new file by opening file for writing myFile = SD.open("arduino.txt", FILE_WRITE); if (myFile) { myFile.println("Created by ArduinoGetStarted.com"); // write a line to Arduino myFile.println("Learn Arduino and SD Card"); // write another line to Arduino myFile.close(); } else { Serial.print(F("SD Card: error on opening file arduino.txt")); } // open file for reading myFile = SD.open("arduino.txt", FILE_READ); if (myFile) { while (myFile.available()) { char ch = myFile.read(); // read characters one by one from Micro SD Card Serial.print(ch); // print the character to Serial Monitor } myFile.close(); } else { Serial.print(F("SD Card: error on opening file arduino.txt")); } } void loop() { }
  • The outcome displayed on the Serial Monitor.
COM6
Send
SD CARD INITIALIZED. Created by ArduinoGetStarted.com Learn Arduino Nano and SD Card
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  
  • Restart Arduino Nano
  • Verify if the material in the file is added to the Serial Monitor or not.

You can remove the Micro SD Card from the module and view its contents on your PC. A USB SD Card reader is required for this.

Video Tutorial

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!