ESP32 - Write Variable to SD Card

In this tutorial, we'll explore the process of writing various types of variables to a Micro SD Card using ESP32. Specifically, we'll cover the following topics:

This tutorial provides a comprehensive guide for storing different types of variables on a Micro SD Card, allowing for versatile data storage and retrieval. To learn how to read key-value pairs from the Micro SD Card and convert them to integer, float, and string formats, refer to the tutorial on ESP32 - Read Config from SD Card.

ESP32 Micro SD Card

Hardware Used In This Tutorial

1×ESP-WROOM-32 Dev Module
1×USB Cable Type-C
1×Micro SD Card
1×Micro SD Card Module
1×Jumper Wires
1×USB 3.0 SD Card Reader
1×(Recommended) ESP32 Screw Terminal Adapter

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 these links are affiliate links. We may earn a commission on your purchase at no extra cost to you. We appreciate it.

Introduction to Micro SD Card Module

Unfamiliar with Micro SD Card Module, including their pinouts, functionality, and programming? learn about them in the ESP32 - Micro SD Card tutorial.

Wiring Diagram

ESP32 Micro SD Card Module Wiring Diagram

This image is created using Fritzing. Click to enlarge image

If you're unfamiliar with how to supply power to the ESP32 and other components, you can find guidance in the following tutorial: How to Power ESP32.

※ 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.

ESP32 - 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
/* * This ESP32 code is created by esp32io.com * * This ESP32 code is released in the public domain * * For more detail (instruction and wiring diagram), visit https://esp32io.com/tutorials/esp32-write-variable-to-sd-card */ #include <SD.h> #define PIN_SPI_CS 5 // The ESP32 pin GPIO5 File myFile; int myInt = -52; float myFloat = -12.7; String myString = "HELLO"; char myCharArray[] = "esp32io.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); // stop the program } Serial.println(F("SD CARD INITIALIZED.")); Serial.println(F("--------------------")); SD.remove("esp32.txt"); // delete the file if existed // create new file by opening file for writing myFile = SD.open("esp32.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: Issue encountered while attempting to open the file esp32.txt")); } // open file for reading myFile = SD.open("esp32.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: Issue encountered while attempting to open the file esp32.txt")); } } void loop() { }

Quick Instructions

  • If this is the first time you use ESP32, see how to setup environment for ESP32 on Arduino IDE.
  • Do the wiring as above image.
  • Connect the ESP32 board to your PC via a micro USB cable
  • Open Arduino IDE on your PC.
  • Select the right ESP32 board (e.g. ESP32 Dev Module) and COM port.
  • 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 ESP32
  • See the result on Serial Monitor.
COM6
Send
SD CARD INITIALIZED. -------------------- -52 -12.70 HELLO esp32io.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 esp32.txt file on your PC, it looks like below
ESP32 writes variable to Micro SD Card

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

/* * This ESP32 code is created by esp32io.com * * This ESP32 code is released in the public domain * * For more detail (instruction and wiring diagram), visit https://esp32io.com/tutorials/esp32-write-variable-to-sd-card */ #include <SD.h> #define PIN_SPI_CS 5 // The ESP32 pin GPIO5 File myFile; int myInt = -52; float myFloat = -12.7; String myString = "HELLO"; char myCharArray[] = "esp32io.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); // stop the program } Serial.println(F("SD CARD INITIALIZED.")); Serial.println(F("--------------------")); SD.remove("esp32.txt"); // delete the file if existed // create new file by opening file for writing myFile = SD.open("esp32.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: Issue encountered while attempting to open the file esp32.txt")); } // open file for reading myFile = SD.open("esp32.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: Issue encountered while attempting to open the file esp32.txt")); } } void loop() { }

Quick Instructions

  • If this is the first time you use ESP32, see how to setup environment for ESP32 on Arduino IDE.
  • Do the wiring as above image.
  • Connect the ESP32 board to your PC via a micro USB cable
  • Open Arduino IDE on your PC.
  • Select the right ESP32 board (e.g. ESP32 Dev Module) and COM port.
  • Copy the above code and open with Arduino IDE
  • Click Upload button on Arduino IDE to upload code to ESP32
  • See the result on Serial Monitor.
COM6
Send
SD CARD INITIALIZED. -------------------- myInt=-52 myFloat=-12.70 myString=HELLO myCharArray=esp32io.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 esp32.txt file on your PC, it looks like below
ESP32 writes variable to Micro SD Card

Video Tutorial

Making video is a time-consuming work. If the video tutorial is necessary for your learning, please let us know by subscribing to our YouTube channel , If the demand for video is high, we will make the video tutorial.

※ OUR MESSAGES