Almost all Arduino compatible microcontrollers contain several different types of memory with each their own characteristics.
Flash Memory
The flash memory is where the program you upload is stored.
This segment is quite large but writing it requires programming the microcontroller.
It can be used for constant read only data as well such as debug strings or large arrays using the PROGMEM
and F
macros. You can learn more about those here.
Atmel specs the Arduino UNO’s flash memory to around 10,000 write cycles before it wears out so it is not suitable for fast changing data.
SRAM
The Static Random Access Memory is used to store data during execution and is generally very fast. It is volatile so the contents are erased when the board is without power.
EEPROM
Electrically Erasable Programmable Read-Only Memory (EEPROM) is another non-volatile memory available.
It contains less storage than the flash memory but the number of write cycles it can endure substantially more.
The Arduino UNO’s EEPROM is specified to 100,000 write cycles so it should be used for storing parameters and long term data but not as a scratch pad to write to a couple of times per second.
Reading and writing to the EEPROM is generally quite slow and abstractions for it are collected in the \prog{EEPROM.h} library.
Writing takes quite a lot of time (3.3ms on the UNO) so it pays off to first check whether the write is actually necessary by reading the value first.
EEPROM can also be accessed like any other array with EEPROM[address]
.
Some important EEPROM functions:
EEPROM.write(address, val)
- Writes exactly one byte,
val
, toaddress
.
- Writes exactly one byte,
EEPROM.read(address)
- Reads exactly one byte from
address
.
- Reads exactly one byte from
EEPROM.update(address, val)
- Performs the same function as
write()
, but checks if the value is different before writing to reduce write cycles.
- Performs the same function as
EEPROM.put(address, val)
- Writes
val
, which can be any data type or object, toaddress
. Usesupdate()
in the background.
- Writes
EEPROM.get(address, val)
- Reads the datatype specified in
val
fromaddress
.
- Reads the datatype specified in
EEPROM.begin(mem_amnt)
- Initializes
mem_amnt}
of bytes in the EEPROM. Only necessary on the ESP8266. Make sure this is called before any read/write operations.
- Initializes
EEPROM.commit()
- Commits the changes made by the program to the EEPROM. Only necessary on the ESP8266.
The following is an example of how to store and get data from EEPROM on the ESP8266. It is the same on the UNO, except for the EEPROM.begin()
and EEPROM.commit()
function calls are not necessary.