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 Runs in your browser. Free, and no account needed.

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.

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() {
}

Parts list

1 part, plus the jumper wires. Every one is in the editor's parts bin.

Change it and keep it

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