DEV Community

Hedy
Hedy

Posted on

How to store data on an Arduino after disconnecting it?

You need some kind of non-volatile memory – something that doesn’t lose data when power is removed. On Arduino there are a few common options:

1. Use the built-in EEPROM (simplest)

Boards like Arduino Uno / Nano / Mega have a small EEPROM on the chip (typically 1–4 KB).

Good for:

  • Small amounts of data: settings, counters, calibration values, last state, etc.
  • A few bytes to a few hundred bytes.

Key points:

  • Survives power loss / USB unplug.
  • Limited write endurance (typically ~100,000 writes per cell) → don’t write in a tight loop.

Basic example (store a value and read it after reset/power-off):

#include <EEPROM.h>

int addr = 0;

void setup() {
  Serial.begin(9600);

  // Example: write a number once (only if not initialized, etc.)
  int valueToStore = 42;
  EEPROM.put(addr, valueToStore);   // writes 2 bytes (int) starting at addr

  // Read it back
  int storedValue = 0;
  EEPROM.get(addr, storedValue);

  Serial.print("Stored value: ");
  Serial.println(storedValue);
}

void loop() {
  // nothing
}

Enter fullscreen mode Exit fullscreen mode

Tip: use EEPROM.get() / EEPROM.put() for structs and multi-byte data, and only write when the value changes, not every loop.

2. Use an SD card module / shield (for larger logs)

If you want to keep a lot of data (sensor logs, CSV logs, etc.), use an SD card:

  • Plug in an SD card module/shield (SPI).
  • Use the SD library to read/write files.
  • Great for logging over hours/days and then analyzing on a PC.

Basic SD example:

#include <SPI.h>
#include <SD.h>

const int chipSelect = 10;  // depends on your shield/module

void setup() {
  Serial.begin(9600);

  if (!SD.begin(chipSelect)) {
    Serial.println("SD init failed!");
    while (1);
  }
  Serial.println("SD init OK.");

  File dataFile = SD.open("log.txt", FILE_WRITE);
  if (dataFile) {
    dataFile.println("Hello, this will still be there after power off.");
    dataFile.close();
  } else {
    Serial.println("Error opening log.txt");
  }
}

void loop() {
  // nothing, but you could keep appending sensor values here
}
Enter fullscreen mode Exit fullscreen mode

After unplugging the Arduino, the data stays saved on the SD card.

3. External EEPROM / FRAM chips (I²C or SPI)

If you need more non-volatile memory than internal EEPROM but don’t want an SD card:

  • I²C EEPROM (e.g., 24LC256) – cheap, but has write-cycle limits like internal EEPROM.
  • FRAM (ferroelectric RAM) modules – much higher write endurance, very fast writes, great for heavy logging.

You connect them via I²C or SPI and use a library (often from the module vendor) to read/write bytes or blocks.

When this is useful:

You want persistent data storage for logs, but:

  • SD card is overkill or too big.
  • You want more writes than EEPROM can tolerate.

4. Internal Flash (advanced / not typical on Uno)

On some MCUs you can write to the program flash at runtime, but:

  • It’s more complex and risky (you can corrupt your program).
  • Not supported nicely on classic AVR Arduinos via the standard API.
  • For most Arduino-level projects, EEPROM or SD is easier and safer.

Which should you use?

  • Just need to remember a few things after power-off
    → Built-in EEPROM is perfect (settings, last mode, calibration, counters).

  • Need to log lots of data over time (sensor readings, timestamps, etc.)
    → Use an SD card module.

  • Need frequent writes, but not huge files
    → Consider FRAM module (I²C SPI FRAM breakout).

Top comments (0)