The ESP32 is a powerful microcontroller that’s great for building IoT projects. But if you want to save data often—like sensor readings or device status—you need to know the best way to store it. Let’s learn how to do this step by step in the simplest way possible!
Why Storing Data Matters
Imagine you’re building a weather station with an ESP32. It collects temperature data every minute. What happens if the ESP32 restarts or loses power? All your data is lost unless you save it somewhere! That’s why learning to store data is so important.
Types of Storage Options on ESP32
ESP32 offers several ways to store data:
- RAM: Fast but data is lost when power is off.
- Flash Memory: Good for saving data even after power is off.
- MicroSD Card: Great for large amounts of data.
- EEPROM: Limited size but works for small data.
- NVS (Non-Volatile Storage): Reliable for frequent writes.
For this guide, we’ll focus on NVS because it’s simple and beginner-friendly.
What Is NVS and Why Use It?
NVS (Non-Volatile Storage) lets you store small amounts of data like numbers or text. The data stays saved even if the ESP32 is restarted or powered off. It’s also designed to handle frequent updates without wearing out.
Step-by-Step Guide to Using NVS
1. Set Up Your Environment
Before we begin, ensure you have:
- An ESP32 development board
- Arduino IDE installed on your computer
- ESP32 board support added to Arduino IDE (via Tools > Boards > Boards Manager)
Coding ESP32 In Arduino IDE
Read our detailed guide here
2. Include the Library
First, include the library for NVS in your code. ESP32 uses the Preferences library for this. Add this at the top of your sketch:
Read more about the preferences library here.
#include <Preferences.h>
3. Initialize NVS
You need to initialize NVS before using it. Here’s how:
Preferences preferences; void setup() { Serial.begin(115200); preferences.begin("storage", false); // "storage" is the namespace }
The namespace acts like a folder to organize your data. You can name it anything.
4. Write Data to NVS
To save data, use the put
functions like putInt
or putString
. For example, to save a counter:
preferences.putInt("counter", 10); // Saves the number 10 with the key "counter" Serial.println("Data saved!");
5. Read Data from NVS
To read data back, use the get
functions like getInt
or getString
. If the key doesn’t exist, you can set a default value:
int counter = preferences.getInt("counter", 0); // Default is 0 Serial.println("Counter value: " + String(counter));
6. Update Data Frequently
If you’re updating data often, like increasing a counter:
int counter = preferences.getInt("counter", 0); // Read current value counter++; // Increment the value preferences.putInt("counter", counter); // Save the updated value Serial.println("Updated counter: " + String(counter));
7. Close NVS When Done
Always close NVS to free up resources:
preferences.end();
Example: Save a Temperature Reading
Here’s a full example where we save and read a temperature value:
#include <Preferences.h> Preferences preferences; void setup() { Serial.begin(115200); preferences.begin("storage", false); // Simulate a temperature reading float temperature = 25.3; preferences.putFloat("temperature", temperature); Serial.println("Temperature saved!"); // Read the temperature back float savedTemp = preferences.getFloat("temperature", 0.0); Serial.println("Saved temperature: " + String(savedTemp)); preferences.end(); } void loop() { // No code here for this example }
Tips for Beginners
- Keep It Small: NVS is best for small data like numbers or short text.
- Don’t Overwrite Too Often: NVS can handle frequent writes, but avoid unnecessary updates to extend its lifespan.
- Use Unique Keys: Always use unique keys like “temperature” or “counter” to avoid overwriting data.
When to Use Other Storage Options
- MicroSD Card: If you need to save large amounts of data, like images or logs.
- Flash Memory: When data doesn’t change often but needs to be saved.
With these steps, you’re ready to store data efficiently on your ESP32. Keep experimenting, and happy building!