Sub topic 1
Three kinds of memory: flash, SRAM and EEPROM
Prerequisites
Assume you can write and upload a basic Arduino sketch, use the Serial Monitor, and understand variables, arrays, and functions. The examples use an AVR-based Arduino Uno.
An Arduino has several kinds of memory. They differ in what they store, how long they keep data, and how your program accesses them.
| Memory | Main use | Keeps data without power? | Typical Uno size |
|---|---|---|---|
| Flash | Program code and fixed data | Yes | 32 KB |
| SRAM | Working data while the sketch runs | No | 2 KB |
| EEPROM | Small settings and saved values | Yes | 1 KB |
The exact sizes depend on the board. Always check the board's documentation before relying on a particular amount.
Flash stores the sketch
Flash memory stores the compiled program that the microcontroller runs. It keeps its contents when power is removed, so your sketch is still present after you unplug and reconnect the board.
Flash also can store constant data, such as menu text or lookup tables. On classic AVR boards, however, a string literal can be copied into SRAM during startup unless you explicitly place it in flash. SRAM is much smaller than flash, so repeated or long messages can consume valuable working memory.
The F() macro tells the Arduino libraries to keep a string literal in flash:
void setup() {
Serial.begin(9600);
Serial.println(F("Sensor controller ready"));
}
void loop() {
}This example does not make the text editable while the program runs. It is suitable for messages that never change.
SRAM holds live working data
SRAM is the memory your program uses while it runs. It holds global variables, local variables, arrays, objects, and temporary values. Unlike flash and EEPROM, SRAM loses its contents when the board loses power.
The Uno has only 2 KB of SRAM. A large character array, many global variables, or several library objects can use much of it before setup() starts. The remaining SRAM must support the stack and any other run-time work.
A useful working model is:
- Global and static data occupies SRAM from the lower addresses.
- The heap can grow upward when code allocates memory dynamically.
- The stack grows downward when functions call one another or create local variables.
If the heap and stack meet, data becomes corrupted. The symptoms can look unrelated to memory: random resets, damaged text, incorrect sensor values, or a sketch that stops responding.
EEPROM stores persistent settings
EEPROM is nonvolatile memory: it keeps data after power is removed. It is useful for small values such as a calibration offset, a selected mode, or a timing setting.
EEPROM is slower than SRAM and has a limited number of write cycles. Avoid writing on every pass through loop(). Write only when a setting changes, or use a wear-reduction strategy when frequent updates are necessary.
The EEPROM library can store a complete value:
#include <EEPROM.h>
struct Settings {
uint16_t interval;
uint8_t mode;
};
void setup() {
Serial.begin(9600);
Settings saved = {1000, 2};
EEPROM.put(0, saved);
Settings loaded;
EEPROM.get(0, loaded);
Serial.println(loaded.interval);
Serial.println(loaded.mode);
}
void loop() {
}This example writes on every reset, which is fine for demonstration but not ideal for a finished product. In a real sketch, validate loaded data before using it, because EEPROM may contain old or uninitialized values.
Exercise
A device must remember the user's selected mode after being unplugged. Which memory should store the mode, and which memory should hold the variable while the program is running?
Show answer
Store the persistent mode in EEPROM. Load it into an SRAM variable when the program starts so the running sketch can use it.