Skip to content

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.
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

### What NVS key types are supported?

int8, int16, int32, int64, uint8, uint16, uint32, uint64, string, blob. Each key must have a consistent type.

How much NVS storage is available?

Default NVS partition is 24KB. Keys are limited to 15 characters. Total key-value pairs depend on key name length and data size.

What happens if NVS is corrupted?

Call nvs_flash_erase() then nvs_flash_init() to reformat. Data is lost. Use CRC checksums for critical data.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro