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:
- Writing a string variable to a Micro SD Card with ESP32.
- Writing an integer variable to a Micro SD Card with ESP32.
- Writing a float variable to a Micro SD Card with ESP32.
- Writing a character array variable to a Micro SD Card with ESP32.
- Writing a byte array variable to a Micro SD Card with ESP32.
- Writing a variable as a key-value pair to a Micro SD Card with ESP32.
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.
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) Screw Terminal Expansion Board for ESP32 |
Or you can buy the following sensor kits:
1 | × | DIYables Sensor Kit (30 sensors/displays) | |
1 | × | DIYables Sensor Kit (18 sensors/displays) |
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
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.
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.
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
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.
- 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 - How to write a key-value to a file on Micro SD Card
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.
- 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
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.