ESP32 - Read Config from SD Card
In this tutorial, we will delve into the utilization of a config file on a Micro SD Card. The tutorial will cover the following aspects:
- Saving a config.txt file on the Micro SD Card, which includes key-value pairs.
- Reading the configuration from the Micro SD Card and storing it into an int variable.
- Reading the configuration from the Micro SD Card and storing it into a float variable.
- Reading the configuration from the Micro SD Card and storing it into a String variable.
This tutorial enables you to manage configuration settings through a file stored on the Micro SD Card, offering flexibility for various data types such as integers, floats, and strings.
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.
How It Works
The key-value pairs are pre-stored on the Micro SD Card according to the following format:
- Each key-value pair is on one line. In another word, each key-value pair is separated from others by a newline character
- The key and value are separated by a = character
ESP32 code will search the key and find the corresponding value, and save the value to a variable. The variable type can be int, float, or String
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.
How To read config to variables
- Create config.txt file on your PC via Notepad or Notepad++
- Copy below key-value pairs and paste it to the config.txt:
- Connect the Micro SD Card to the PC via USB 3.0 SD Card Reader
- Make sure that the Micro SD Card is formatted FAT16 or FAT32 (Google for it)
- Copy config.txt file to the root directory of the Micro SD Card
- Disconnect the Micro SD Card from PC
- Connect the Micro SD Card to ESP32 via Micro SD Card Module according to the above wiring diagram
- Copy the below code and open with Arduino IDE
- Click Upload button on Arduino IDE to upload code to ESP32
- See the result on Serial Monitor.
- The Serial Monitor shows the values from variables that is identical to the values saved on the micro SD Card
Now you can modify the code to add more variables.
※ NOTE THAT:
The above code does not care about the order of key-value pairs. It will search for the key from the beginning to the end of the file until the key is matched.
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.