typestar

EEPROM in Arduino

1024 bytes that survive power loss.

// EEPROM survives power loss: 1024 bytes of remember-me.
#include <EEPROM.h>

const int THRESHOLD_ADDR = 0;

void setup() {
  Serial.begin(9600);
  int saved = EEPROM.read(THRESHOLD_ADDR);
  Serial.print("Saved threshold: ");
  Serial.println(saved);
  // update only writes when the value changed -- cells wear out.
  EEPROM.update(THRESHOLD_ADDR, 180);
}

void loop() {
}

How it works

  1. #include <EEPROM.h> brings in the persistent store.
  2. EEPROM.read recovers the byte saved last session.
  3. update writes only on change — cells wear out.

Keywords and builtins used here

The run, in numbers

Lines
16
Characters to type
362
Tokens
65
Three-star pace
65 tpm

At the three-star pace of 65 tokens a minute, this run takes about 60 seconds.

Type this snippet

Step 3 of 3 in Data, step 21 of 27 in Language basics.

← Previous Next →