in ESP32 / ESP-IDF - when to use EEPROM vs NVS vs SPIFFS?

I'm fairly new to doing production work on ESP32 microcontrollers, and I'm wanting a little context and nuance from people who've been around the block a few times. So this question is a bit more on that kind of thing rather than a "how do I code X" kind of question.

I have lots of data storage needs on my current project.

  • larger blobs of data that need to be stored less often
  • smaller blobs of data that need to be updated more often
  • factory settings (like serial number, board revision, etc) that are particular to a given device, but aren't going to be encoded in C.
  • etc

I'm familiar with storing data in "blobs", and I'm familiar with encoding / decoding data with protocol buffers.

So given all that, I'm trying to gain context on the differences between my various storage options on the ESP32, and when to use each.

  • EEprom
  • NVS
  • SPIFFS / LittleFS
  • other options...

What use cases make you pick one of these options over another?


Solution 1:

There's no EEPROM on the ESP32, just the flash.

NVS is a simple non-volatile key-value store with different data types (integers 8-64 bits, strings, blobs). It's reasonably convenient to use, does wear levelling and supports flash encryption (although that a bit of a hassle). I'd use it for storing factory settings and anything else which is reasonably small (there's a 4000 byte limit on strings, 508,000 byte limit on blobs). If the device needs to write often, you might want to create a separate, dedicated, read-only NVS partition for storing device attributes (serial, hw info) so it's guaranteed to not get clobbered by power failures during write.

ESP IDF supports SPIFFS and FAT file systems.

SPIFFS is light-weight and much better in terms of wear levelling and reliability. I'd use this for storing any larger files. It doesn't support flash encryption, unfortunately.

FAT file system is probably the worst choice because it's not really natively Flash-friendly, nor reliable. Espressif has built some kind of a layer between FAT and flash to accommodate wear levelling. The only critical advantage of FAT is that it supports flash encryption.

Then there are third party options which I haven't used, unfortunately.

As always, consider the number of page erases your writes are going to cause in the flash - this gives you an estimate of how many times you can write before the chip's lifetime is reached.