The quick answer
Most microcontrollers contain up to three kinds of memory, each with a different job:
- Flash holds your program and constant data. It keeps its contents without power, but it’s erased in blocks and wears out after many erase cycles.
- SRAM is the working memory: variables, the stack and buffers. It’s fast and can be rewritten endlessly, but it loses everything when power is removed.
- EEPROM stores small amounts of data that must survive power loss, such as settings, calibration values and counters. It can be rewritten byte by byte, but slowly, and it has limited write endurance too.
A typical microcontroller has much more flash than SRAM, and EEPROM, if present, is usually the smallest of the three.
Volatile vs non-volatile memory
- Volatile memory needs power to hold data. SRAM is volatile: switch off and the contents are gone.
- Non-volatile memory keeps data without power. Flash and EEPROM are non-volatile, which is why your program is still there after you unplug the board.
The trade-off is that non-volatile memories are slower to write and wear out with repeated writes, while SRAM is fast and doesn’t wear.
Flash memory
On-chip flash in microcontrollers is usually NOR flash. Its key property is that the CPU can read it directly, byte by byte, so code can run straight from flash (“execute in place”) rather than being copied into RAM first.
How flash is written and erased
Flash has an awkward rule: a write can only change bits in one direction, typically from 1 to 0. To set bits back to 1, you must erase a whole block, called a page or sector depending on the chip, and blocks range from a few hundred bytes to many kilobytes. So updating one byte in flash typically means:
- Copy the block’s contents to RAM.
- Erase the block.
- Write the modified contents back.
Each erase wears the cells slightly. On-chip microcontroller flash is commonly rated for around 10,000 erase cycles, sometimes more, along with a data retention period, often quoted in decades at specified temperatures. Exact figures vary by chip, so check the datasheet. This is plenty for occasional firmware updates, but it can be used up quickly if code rewrites flash in a tight loop.
Flash also has to be unlocked before programming on most chips, and the CPU may stall while the flash bank it’s running from is being erased or written.
Flash speed and wait states
Flash reads are slower than the fastest CPU clocks. When a chip runs above a certain frequency, it inserts wait states: extra clock cycles for each flash access. Many microcontrollers soften this with prefetch buffers, instruction caches or accelerators, so code usually runs close to full speed. Forgetting to raise the wait-state setting before increasing the clock is a classic cause of crashes.
Storing data in flash
Besides code, flash holds constant data such as lookup tables, strings, fonts and images. It can also store data that changes rarely, such as configuration, logs or a file system, as long as the code respects the erase-block rules and spreads writes out to limit wear. Flash file systems such as LittleFS are designed for this.
SRAM
SRAM (static RAM) stores each bit in a small circuit of transistors that holds its state as long as power is applied. Unlike DRAM, it doesn’t need refreshing. On a microcontroller, SRAM is typically fast enough to keep up with the CPU without wait states.
SRAM costs much more chip area per bit than flash, which is why microcontrollers usually have far less of it. A chip might have 256 KB of flash but only 64 KB of SRAM, and small 8-bit parts may have just a few kilobytes or less.
What lives in SRAM
- Global and static variables, both initialized (the
.datasection) and zero-initialized (the.bsssection) - The stack: local variables, function arguments, return addresses and saved registers during interrupts
- The heap, if the program uses dynamic allocation (
malloc,new, some String classes) - Buffers for communication, DMA, displays and audio
- Sometimes code that must run fast or keep running while flash is being erased
Running out of SRAM is one of the most common embedded problems, and it’s often silent. A stack that grows into other data corrupts variables and causes unpredictable crashes rather than a clean error.
EEPROM
EEPROM (electrically erasable programmable read-only memory) is non-volatile memory that can be erased and rewritten one byte at a time, without the block-erase step flash needs. That makes it convenient for small values that change occasionally:
- User settings and preferences
- Calibration constants
- Device IDs and serial numbers
- Counters, such as how many times a device has been powered up
EEPROM writes are slow compared with RAM, each taking on the order of milliseconds, and EEPROM also wears out. On-chip EEPROM is often rated for around 100,000 write cycles per location, and some external EEPROM chips are rated higher. Again, check the specific datasheet.
Microcontrollers without EEPROM
Many modern 32-bit microcontrollers have no true EEPROM at all. Common substitutes are:
- EEPROM emulation in flash: a software layer that stores values in flash pages and handles erasing and wear spreading. Several vendors publish libraries or application notes for this.
- Key-value storage in flash: for example, Espressif’s NVS (non-volatile storage) on the ESP32. The Arduino
EEPROMlibrary on such chips is also emulated in flash. - An external EEPROM chip, such as an I2C part from the 24-series families, when you need byte-level updates or extra endurance.
Some chip families also provide small battery-backed registers or RAM that survive resets and low-power modes while a backup supply is present.
Flash vs SRAM vs EEPROM
| Flash | SRAM | EEPROM | |
|---|---|---|---|
| Keeps data without power | Yes | No | Yes |
| Main use | Program code, constants, rarely changed data | Variables, stack, heap, buffers | Small settings, calibration, counters |
| Typical size on an MCU | Largest (KB to MB) | Medium (bytes to hundreds of KB, sometimes more) | Smallest, often absent |
| Read speed | Fast, may need wait states at high clocks | Fastest | Slower |
| Write method | Erase a whole block, then program | Any byte, any time | Byte by byte |
| Write speed | Slow, especially erase | Very fast | Slow (milliseconds per write) |
| Write endurance | Limited (often ~10,000 erase cycles) | Effectively unlimited | Limited (often ~100,000 cycles) |
Endurance and speed figures are typical orders of magnitude. Always check the datasheet for your chip.
A real example: the ATmega328P
The ATmega328P used on the Arduino Uno R3 has:
- 32 KB of flash for the program, part of which is used by the Arduino bootloader
- 2 KB of SRAM for variables and the stack
- 1 KB of EEPROM for persistent data
Its datasheet rates the flash for 10,000 write/erase cycles and the EEPROM for 100,000. With only 2 KB of SRAM, a few text strings, a large array and a library or two can use it all, which is why Arduino code often uses the F() macro or PROGMEM to keep constant strings in flash. That’s a quirk of the AVR’s Harvard architecture, explained in our article on Harvard vs von Neumann architecture.
Where your code and variables end up
When you build a program, the linker places each part into a memory region:
| Section | Contents | Stored in | At runtime |
|---|---|---|---|
.text |
Compiled code | Flash | Executed from flash (or copied to RAM in some designs) |
.rodata |
const data, string literals |
Flash | Read from flash on most 32-bit MCUs |
.data |
Initialized globals and statics | Initial values in flash | Copied to SRAM by startup code |
.bss |
Zero-initialized globals and statics | Nothing (just a size) | Zeroed in SRAM by startup code |
| Stack | Locals, return addresses | — | SRAM, usually growing down from the top |
| Heap | Dynamic allocations | — | SRAM, between .bss and the stack |
Notice that initialized variables cost both flash (for the starting values) and SRAM (for the live copy). The build output usually reports flash and RAM usage. Keep an eye on it, and remember the stack and heap aren’t always included in that figure.
Other memory you may meet
- Boot ROM: factory-programmed code in many chips that runs first, often providing a built-in bootloader for USB or UART programming.
- OTP (one-time programmable) memory and fuses: bits that can be written once, used for configuration, security keys or calibration.
- FRAM: ferroelectric RAM, a non-volatile memory with fast writes and very high endurance, used in some microcontrollers such as TI’s MSP430FR family and in external chips.
- External flash: larger SPI or QSPI NOR flash chips for extra code, assets or data logging. Some chips can execute code from them through a memory-mapped interface and cache.
- PSRAM and SDRAM: external RAM for applications that need much more working memory, such as displays, cameras or audio. For example, some ESP32 modules include PSRAM.
- Caches and tightly coupled memory (TCM): small, fast memories in higher-end cores such as the Cortex-M7 that speed up access to code and data.
Practical tips
- Watch SRAM, not just flash. A program that fits in flash can still crash from stack overflow. Use the toolchain’s memory reports and, where available, stack-usage analysis.
- Write non-volatile memory only when values change. Compare before writing, and never write EEPROM or flash in a fast loop.
- Spread writes for frequently updated data. Use wear-levelling schemes, a flash file system or a ring buffer across several locations.
- Plan for power loss mid-write. Use checksums, version counters or two alternating copies so a half-written record can be detected and the previous good copy used.
- Keep large constants in flash. On 32-bit ARM chips,
constusually does this automatically. On AVR, usePROGMEMor theF()macro. - Read the memory map in the reference manual: it shows where flash, SRAM, peripheral registers and any special memories live in the address space. Our article on buses in embedded systems explains how the CPU reaches them.
For how memory fits alongside the processor, peripherals and software layers, see our embedded system architecture guide.