ESP32 NVS Read Returns Default Value Always
DodaTech
Updated 2026-06-26
1 min read
In this tutorial, you'll learn about ESP32 NVS Read Returns Default Value Always. We cover key concepts, practical examples, and best practices.
The Problem
ESP32 NVS reads always return the default value, even after writing.
Quick Fix
Wrong
nvs_get_i32(handle, "counter", &value); // Handle not opened
value = 0 (default). Never returns the stored value.
Right
nvs_handle_t handle;
esp_err_t err = nvs_open("storage", NVS_READONLY, &handle);
if (err == ESP_OK) {
int32_t value = 0;
err = nvs_get_i32(handle, "counter", &value);
if (err == ESP_ERR_NVS_NOT_FOUND) {
value = 0; // Key doesn"t exist yet
} else if (err == ESP_OK) {
Serial.printf("Read counter: %d\n", value);
}
nvs_close(handle);
} else {
Serial.println("NVS namespace not found');
}
Read counter: 42
(Returns the last written value)
Prevention
Open namespace with correct access mode (READONLY for reads). Always check return codes. Handle ESP_ERR_NVS_NOT_FOUND for first-time reads. Close handle after reading. Verify namespace name matches exactly.
DodaTech engineers apply these same patterns across Doda Browser, DodaZIP, and Durga Antivirus Pro for production IoT reliability.
FAQ
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro