Disclosure: Some links in this section are Amazon affiliate links. If you make a purchase through these links, we may earn a commission at no extra cost to you. Additionally, some links direct to products from our own brand, DIYables .
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
This image is created 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 4File myFile;int myInt = -52;float myFloat = -12.7;String myString = "HELLO";char myCharArray[] = "ArduinoGetStarted.com";byte myByteArray[] = {'1', '2', '3', '4', '5'};voidsetup() {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 linefor (int i = 0; i < 5; i++) { myFile.write(myByteArray[i]); // new lineif (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 CardSerial.print(ch); // print the character to Serial Monitor } myFile.close(); } else {Serial.print(F("SD Card: error on opening file arduino.txt")); }}voidloop() {}
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.
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
Arduino Uno
Newbiely.ino
···
8Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'Arduino Uno' on 'COM15')
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 - 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 4File myFile;int myInt = -52;float myFloat = -12.7;String myString = "HELLO";char myCharArray[] = "ArduinoGetStarted.com";byte myByteArray[] = {'1', '2', '3', '4', '5'};voidsetup() {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 cardfor (int i = 0; i < 5; i++) { myFile.write(myByteArray[i]); // new lineif (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 CardSerial.print(ch); // print the character to Serial Monitor } myFile.close(); } else {Serial.print(F("SD Card: error on opening file arduino.txt")); }}voidloop() {}
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.
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
Arduino Uno
Newbiely.ino
···
8Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'Arduino Uno' on 'COM15')
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
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.
You can share the link of this tutorial anywhere. Howerver, please do not copy the content to share on other websites. We took a lot of time and effort to create the content of this tutorial, please respect our work!