Table of Contents

The ESP32 Preferences library provides a simple and efficient way to store key-value pairs in the ESP32’s non-volatile storage (NVS), making it ideal for saving small amounts of data such as configuration settings, user preferences, or flags. This data is retained across reboots, making it perfect for storing things like Wi-Fi credentials, device settings, or application state.

Key Features:

  1. Key-Value Storage:
  • Data is stored as key-value pairs, where the key is a string (identifier) and the value is typically an integer, float, or string.
  • You can easily store and retrieve data by providing the key.
  1. Non-Volatile Storage:
  • The data stored with Preferences persists across resets or reboots because it is stored in non-volatile memory (NVS), which is a small portion of the ESP32’s flash memory.
  • Ideal for storing configuration or state data that needs to be retained even if the device is powered off.
  1. Simple API:
  • The Preferences library provides a simple API to set, get, and delete key-value pairs.
  • Supports types like int, float, and String for storing data.
  • No need to worry about file management or handling complex data structures like with LittleFS.
  1. Efficient Storage:
  • Preferences is designed to be lightweight and optimized for small amounts of data, making it fast and efficient for simple use cases.
  • It automatically manages memory, including freeing up space when entries are deleted.
  1. Limited Storage:
  • The ESP32 has a limited amount of non-volatile storage, typically around 64 KB for NVS, so Preferences is best used for small amounts of data (like settings, flags, counters).
  • It is not suitable for storing large files or datasets, unlike LittleFS or SD cards.

Typical Use Cases for Preferences:

  • Storing Wi-Fi credentials: Save SSID, password, and other connection settings.
  • Saving user preferences: Store settings like screen brightness, volume level, or user-selected options.
  • Flags and state variables: Keep track of flags (e.g., whether a device has been configured before) or states (e.g., an initial setup or user flow state).
  • Counters or time-based data: Track the number of times a device was used, or store timestamps.

Categorized in:

Storage,

Tagged in: