ESP8266 - Write Variable to SD Card
This tutorial provides comprehensive guidance on writing various types of variables to a Micro SD Card using ESP8266. The covered topics include:
- Writing a string variable to a Micro SD Card with ESP8266
- Writing an int variable to a Micro SD Card with ESP8266
- Writing a float variable to a Micro SD Card with ESP8266
- Writing a char array variable to a Micro SD Card with ESP8266
- Writing a byte array variable to a Micro SD Card with ESP8266
- Writing a variable as a key-value pair to a Micro SD Card with ESP8266
To extract key-value pairs from the Micro SD Card and convert them to int, float, or string, please refer to the following tutorial: ESP8266 - Read Config from SD Card.
Hardware Preparation
Or you can buy the following sensor kits:
1 | × | DIYables Sensor Kit (30 sensors/displays) | |
1 | × | DIYables Sensor Kit (18 sensors/displays) |
Additionally, some of these links are for products from our own brand, DIYables.
Overview of Micro SD Card Module
If you are unfamiliar with the Micro SD Card Module, including its pinout, how it works, and how to program it, the ESP8266 - Micro SD Card tutorial can provide you with the necessary information.
Wiring Diagram
This image is created using Fritzing. Click to enlarge image
See more in ESP8266's pinout and how to supply power to the ESP8266 and other components.
※ NOTE THAT:
If you have an Ethernet shield or any other shield that has a Micro SD Card Holder, you do not need to use the Micro SD Card Module. Simply insert the Micro SD Card into the Micro SD Card Holder on the shield.
ESP8266 - How to write a variable to a file on Micro SD Card
The following code:
- Stores an integer value to the Micro SD Card
- Stores a floating point value to the Micro SD Card
- Stores a string to the Micro SD Card
- Stores a character array to the Micro SD Card
- Stores a byte array to the Micro SD Card
Detailed Instructions
To get started with ESP8266 on Arduino IDE, follow these steps:
- Check out the how to setup environment for ESP8266 on Arduino IDE tutorial if this is your first time using ESP8266.
- Wire the components as shown in the diagram.
- Connect the ESP8266 board to your computer using a USB cable.
- Open Arduino IDE on your computer.
- Choose the correct ESP8266 board, such as (e.g. NodeMCU 1.0 (ESP-12E Module)), and its respective COM port.
- Ensure that the Micro SD Card is formatted with either FAT16 or FAT32 (you can search for instructions on how to do this online).
- Then, open the code in the Arduino IDE.
- Once the code is open, press the Upload button in the Arduino IDE to compile and upload the code to the ESP8266.
- Finally, view the results in the Serial Monitor.
- Remove the Micro SD Card from the Micro SD Card module.
- Insert the Micro SD Card into an USB SD Card reader.
- Attach the USB SD Card reader to the PC.
- Open the esp8266.txt file on your computer, it appears as follows.
ESP8266 - How to write a key-value to a file on Micro SD Card
Detailed Instructions
- Wire the components as shown in the diagram.
- Connect the ESP8266 board to your computer using a USB cable.
- Open Arduino IDE on your computer.
- Choose the correct ESP8266 board, such as (e.g. NodeMCU 1.0 (ESP-12E Module)), and its respective COM port.
- Copy the code and open it in Arduino IDE.
- Click the Upload button to send it to the ESP8266.
- View the outcome on the Serial Monitor.
- Remove the Micro SD Card from the Micro SD Card module.
- Insert the Micro SD Card into an USB SD Card reader.
- Connect the USB SD Card reader to the computer.
- Open the esp8266.txt file on your PC; it will appear as follows.