Arduino - Write Variable to SD Card

In this tutorial, we are going to learn how to write variable to Micro SD Card with Arduino. In detail, we will learn:

To read the key-value from the Micro SD Card and convert it to int, float, string, See Arduino - Read Config from SD Card

Arduino Micro SD Card

Hardware Required

1×Arduino UNO or Genuino UNO
1×USB 2.0 cable type A/B
1×Micro SD Card
1×Micro SD Card Module
n×Jumper Wires
1×USB 3.0 SD Card Reader
Please note: These are affiliate links. If you buy the components through these links, We may get a commission at no extra cost to you. We appreciate it.

About Micro SD Card Module

If you do not know about Micro SD Card Module (pinout, how it works, how to program ...), learn about them in the Arduino - Micro SD Card tutorial.

Wiring Diagram

Arduino Micro SD Card Module Wiring Diagram

Image is developed using Fritzing. Click to enlarge image

※ NOTE THAT:

If you use an Ethernet shield or any shield that has a Micro SD Card Holder, you do not need to use the Micro SD Card Module. You just need to insert the Micro SD Card to the Micro SD Card Holder on the shield.

Arduino - How to write a variable to a file on Micro SD Card

The below code does:

  • Write a int variable to Micro SD Card
  • Write a float variable to Micro SD Card
  • Write a string variable to Micro SD Card
  • Write a char array to Micro SD Card
  • Write a byte array to Micro SD Card
/* * Created by ArduinoGetStarted.com * * This example code is in the public domain * * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-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() { }

Quick Steps

  • Make sure that the Micro SD Card is formatted FAT16 or FAT32 (Google for it)
  • Copy the above code and open with Arduino IDE
  • Click Upload button on Arduino IDE to upload code to Arduino
  • See the result on 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  
  • Detach the Micro SD Card from the Micro SD Card module
  • Insert the Micro SD Card to an USB SD Card reader
  • Connect the USB SD Card reader to the PC
  • Open the arduino.txt file on your PC, it looks like below
  • Arduino writes variable to Micro SD Card

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

/* * Created by ArduinoGetStarted.com * * This example code is in the public domain * * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-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() { }

Quick Steps

  • Copy the above code and open with Arduino IDE
  • Click Upload button on Arduino IDE to upload code to Arduino
  • See the result on 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  
  • Detach the Micro SD Card from the Micro SD Card module
  • Insert the Micro SD Card to an USB SD Card reader
  • Connect the USB SD Card reader to the PC
  • Open the arduino.txt file on your PC, it looks like below
  • Arduino writes variable to Micro SD Card

Video Tutorial

We are considering to make the video tutorials. If you think the video tutorials are essential, please subscribe to our YouTube channel to give us motivation for making the videos.

The Best Arduino Starter Kit

※ OUR MESSAGES