Arduino Nano - Write Variable to SD Card

This tutorial instructs you how to write different types of variables to a Micro SD Card using Arduino Nano. Specifically, we will look at:

To get the key-value from the Micro SD Card and change it to int, float, or string, refer to Arduino Nano - Read Config from SD Card. For instructions on how to read the key-value from the Micro SD Card and convert it to int, float, or string, have a look at Arduino Nano - Read Config from SD Card

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×USB 3.0 SD Card Reader
1×(Optional) 9V Power Adapter for Arduino Nano
1×(Recommended) Screw Terminal Adapter for Arduino Nano

Or you can buy the following sensor kit:

1×DIYables Sensor Kit 30 types, 69 units
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. We appreciate your support.

Overview of Micro SD Card Module

If you are unfamiliar with the Micro SD Card Module, including its pinout, how it works, and how to program it, you can find out more in the Arduino Nano - Micro SD Card tutorial.

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 write a variable to a file on Micro SD Card

The following code:

  • Stores an integer value on a Micro SD Card
  • Stores a floating point value on a Micro SD Card
  • Stores a string on a Micro SD Card
  • Stores a character array on a Micro SD Card
  • Stores a byte array on a Micro SD Card
/* * 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-write-variable-to-sd-card */ #include <SD.h> #define PIN_SPI_CS 4 File myFile; int myInt = -52; float myFloat = -12.7; String myString = "HELLO"; char myCharArray[] = "ArduinoGetStarted.com"; byte myByteArray[] = {'1', '2', '3', '4', '5'}; 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.")); Serial.println(F("--------------------")); 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(myInt); // write int variable to SD card in line myFile.println(myFloat); // write float variable to SD card in line myFile.println(myString); // write String variable to SD card in line myFile.println(myCharArray); // write char array to SD card in line myFile.write(myByteArray, 5); myFile.write("\n"); // new line for (int i = 0; i < 5; i++) { myFile.write(myByteArray[i]); // new line if (i < 4) myFile.write(","); // comma } myFile.write("\n"); // new line 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() { }

Detailed Instructions

  • Ensure that the Micro SD Card is formatted as either FAT16 or FAT32 (you can find instructions on how to do this by searching on Google).
  • Copy the code and open it in the Arduino IDE.
  • Then, press the Upload button in the Arduino IDE to upload the code to the Arduino Nano.
  • Finally, view the result in the Serial Monitor.
COM6
Send
SD CARD INITIALIZED. -------------------- -52 -12.70 HELLO ArduinoGetStarted.com 12345 1,2,3,4,5
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  
  • Remove the Micro SD Card from the Micro SD Card module.
  • Insert the Micro SD Card into an USB SD Card reader.
  • Connect the USB SD Card reader to the PC.
  • Open the arduino-nano.txt file on your PC; it appears as follows.
Arduino Nano writes variable to Micro SD Card

Arduino Nano - How to write a key-value to a file on Micro SD Card

/* * 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-write-variable-to-sd-card */ #include <SD.h> #define PIN_SPI_CS 4 File myFile; int myInt = -52; float myFloat = -12.7; String myString = "HELLO"; char myCharArray[] = "ArduinoGetStarted.com"; byte myByteArray[] = {'1', '2', '3', '4', '5'}; 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.")); Serial.println(F("--------------------")); 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.print("myInt="); // write key to SD card myFile.println(myInt); // write int variable to SD card in line myFile.print("myFloat="); // write key to SD card myFile.println(myFloat); // write float variable to SD card in line myFile.print("myString="); // write key to SD card myFile.println(myString); // write String variable to SD card in line myFile.print("myCharArray="); // write key to SD card myFile.println(myCharArray); // write char array to SD card in line myFile.print("myByteArray="); // write key to SD card myFile.write(myByteArray, 5); myFile.write("\n"); // new line myFile.print("myByteArray2="); // write key to SD card for (int i = 0; i < 5; i++) { myFile.write(myByteArray[i]); // new line if (i < 4) myFile.write(","); // comma } myFile.write("\n"); // new line 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() { }

Detailed Instructions

  • Copy the code and open it using the Arduino IDE.
  • Click the Upload button on the Arduino IDE to send the code to the Arduino Nano.
  • Check out the result on the Serial Monitor.
COM6
Send
SD CARD INITIALIZED. -------------------- myInt=-52 myFloat=-12.70 myString=HELLO myCharArray=ArduinoGetStarted.com myByteArray=12345 myByteArray2=1,2,3,4,5
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  
  • Remove the Micro SD Card from the Micro SD Card module.
  • Insert the Micro SD Card into an USB SD Card reader.
  • Plug the USB SD Card reader into the PC.
  • Open the arduino-nano.txt file on your computer, it appears as follows:
Arduino Nano writes variable to Micro SD Card

Video Tutorial