Built-in project · Arduino Uno R3

EEPROM: memory that survives a reset, on an Arduino Uno

The Uno’s 1 KB of EEPROM keeps its bytes when the power goes. The sketch writes an int and a long with EEPROM.put, reads them back with EEPROM.get, and counts its own starts. The whole build, an Arduino Uno, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.

Teaching? Assign it to your class in one click.

Beginner About 10 minutes to build and run. Runs in your browser. Free, and no account needed.

The parts retail for about $27. On Mokxi they're free.

Step by step, with the wiring and the common mistakes: Arduino EEPROM: Read, Write, put() and get()

EEPROM: memory that survives a reset · Arduino Uno R3live0.000 s 0.00x
Click to open it in the editor
The circuit itself, running here on the simulator. Press what can be pressed; click anything else to open it in the editor.

What you'll learn

  • EEPROM keeps data when power is off
  • Each byte wears out after about 100,000 writes
  • Storing an int and a long with EEPROM.put and EEPROM.get
  • Choosing addresses so values do not overlap
  • Blank EEPROM reads 255
  • Difficulty: Beginner
  • Time: About 10 minutes
  • Board: Arduino Uno R3
  • Parts: 1

How it works

The Uno has 1024 bytes of EEPROM, memory that keeps its contents with the power off. Normal variables vanish at reset, but EEPROM stays. This project has no parts to wire, just the board.

On each start, the sketch writes an int (2000) at address 0 and a long (65539) at address 2 with EEPROM.put, then reads them back with EEPROM.get and prints them. A fresh run of the sketch also shows they were stored.

The address 6 byte is a start counter. It reads the byte, adds one, and saves it with EEPROM.update. Press reset and the count goes up. A blank EEPROM reads 255, so the sketch treats that as zero.

Parts list

1 part, plus the jumper wires. They retail for about $27; on Mokxi they're free.

The code

The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.

sketch.ino · Arduino Uno R3
// EEPROM: a few bytes the Uno keeps when it is switched off.
//
// The ATmega328P has 1024 bytes of EEPROM beside its RAM. RAM forgets
// everything at reset; EEPROM does not. Each byte can be written about
// 100,000 times, so it is for settings and counts, not for things that change
// every millisecond.
//
// EEPROM.put(address, value) writes a value of any type, taking as many bytes
// as it needs; EEPROM.get(address, variable) reads it back. Here address 0
// holds an int (2 bytes), address 2 a long (4 bytes), and address 6 a count
// of how many times the sketch has started.
//
// Press the board's reset (or Stop and Run again) and the count goes up.

#include <EEPROM.h>

const int INT_ADDRESS = 0;
const int LONG_ADDRESS = 2;
const int COUNT_ADDRESS = 6;

void setup() {
  Serial.begin(9600);
  Serial.println("Mokxi Uno: EEPROM");

  int smallIn = 2000;
  long bigIn = 65539;
  EEPROM.put(INT_ADDRESS, smallIn);
  EEPROM.put(LONG_ADDRESS, bigIn);

  int smallOut = 0;
  long bigOut = 0;
  EEPROM.get(INT_ADDRESS, smallOut);
  EEPROM.get(LONG_ADDRESS, bigOut);
  Serial.print("int read back: ");
  Serial.println(smallOut);
  Serial.print("long read back: ");
  Serial.println(bigOut);

  byte starts = EEPROM.read(COUNT_ADDRESS);
  if (starts == 255) starts = 0;  // a blank EEPROM reads 255 everywhere
  starts++;
  EEPROM.update(COUNT_ADDRESS, starts);
  Serial.print("this sketch has started ");
  Serial.print(starts);
  Serial.println(starts == 1 ? " time" : " times");
}

void loop() {
}

Code walkthrough

The key functions of the sketch, and what each one does.

setup sketch.ino

Does all the work once. It puts an int at address 0 and a long at address 2, reads both back and prints them, then loads the counter byte from COUNT_ADDRESS, adds one and saves it with EEPROM.update.

void setup() {
  Serial.begin(9600);
  Serial.println("Mokxi Uno: EEPROM");

  int smallIn = 2000;
  long bigIn = 65539;
  EEPROM.put(INT_ADDRESS, smallIn);
  EEPROM.put(LONG_ADDRESS, bigIn);

  int smallOut = 0;
  long bigOut = 0;
  EEPROM.get(INT_ADDRESS, smallOut);
  EEPROM.get(LONG_ADDRESS, bigOut);
  Serial.print("int read back: ");
  Serial.println(smallOut);
  Serial.print("long read back: ");
  Serial.println(bigOut);

  byte starts = EEPROM.read(COUNT_ADDRESS);
  if (starts == 255) starts = 0;  // a blank EEPROM reads 255 everywhere
  starts++;
  EEPROM.update(COUNT_ADDRESS, starts);
  Serial.print("this sketch has started ");
  Serial.print(starts);
  Serial.println(starts == 1 ? " time" : " times");
}

loop sketch.ino

Is empty on purpose. The sketch only needs to run once per start, so there is nothing to repeat.

void loop() {
}

Troubleshooting

The usual mistakes with this circuit, and the fix for each.

  • The count never goes up This project
    Press reset or Stop and Run again. The count only changes at start.
  • Values overwrite each other This project
    An int takes 2 bytes and a long takes 4. That is why the addresses are 0, 2 and 6.
  • Upload fails or nothing runs after wiring pins 0 and 1 to a module. Arduino Uno R3
    Pins 0 and 1 are the USB serial port. Move the module to other pins, or unplug it while uploading. Also check the right board is selected.
  • Pins misbehave or the board resets when you power motors or servos from it. Arduino Uno R3
    Each I/O pin gives about 20 mA, 40 mA max. Power motors and servos from their own supply and join its ground to the Uno's GND.

Change it and keep it

Open it in the editor, change the circuit or the code, and keep your version in a free account.