The ATMega328 at the heart of an Arduino Uno has a kilobyte of electrically erasable
read-only memory (EEPROM). EEPROM is designed to remember its contents for many
years. Despite its name, it is not really read-only. You can write to it.
The Arduino commands for reading and writing to EEPROM are just as awkward to use
as the ones for using
PROGMEM
. You have to read and write to and from EEPROM one
byte at a time.
The example of sketch 8-02 allows you to enter a single-digit letter code from the Serial
Monitor. The sketch then remembers the digit and repeatedly
writes it out on the Serial
Monitor.
To try this sketch, open the Serial Monitor and enter a new character. Then unplug the
Arduino and plug it back in. When
you reopen the Serial Monitor, you will see that the
letter has been remembered.
The
function
EEPROM.write
takes two arguments.
The first is the address, which is
the memory location in EEPROM and should be between 0 and 1023. The second
argument is the data to write at that location. This must be a single byte.
A character is
represented as eight bits, so this is fine, but you cannot directly store a 16-bit
int
.
Storing an
int
in EEPROM
To store a two-byte
int
in locations 0 and 1 of the EEPROM, you would have to do this: