Site icon DIY Usthad

Beginner’s Guide to Using NVS for ESP32 Data Storage

esp32 data storage

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:

  1. RAM: Fast but data is lost when power is off.
  2. Flash Memory: Good for saving data even after power is off.
  3. MicroSD Card: Great for large amounts of data.
  4. EEPROM: Limited size but works for small data.
  5. 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:

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

  1. Keep It Small: NVS is best for small data like numbers or short text.
  2. Don’t Overwrite Too Often: NVS can handle frequent writes, but avoid unnecessary updates to extend its lifespan.
  3. Use Unique Keys: Always use unique keys like “temperature” or “counter” to avoid overwriting data.

When to Use Other Storage Options


With these steps, you’re ready to store data efficiently on your ESP32. Keep experimenting, and happy building!


Test Your Knowledge About ESP32 Data Storage!

1 / 7

What command is used to release resources after using NVS?

2 / 7

Which of these is NOT a suitable use case for NVS?

3 / 7

What happens if you try to retrieve a key from NVS that doesn’t exist?

4 / 7

What is the purpose of the preferences.putInt() function?

5 / 7

True or False: Data stored in NVS is lost when the ESP32 is powered off.

6 / 7

Which library is commonly used for NVS in the Arduino IDE for ESP32?

7 / 7

What does NVS stand for in ESP32 programming?

Your score is

The average score is 71%

0%

Exit mobile version