MyRoboPath
microcontrollers15 min readUpdated 2026-03-07Intermediate

Microcontroller Memory Systems: Flash ROM, SRAM, EEPROM & Memory-Mapped I/O

Master embedded memory systems: differentiate Flash program storage from SRAM variables, avoid Stack Overflow, and write to memory-mapped hardware peripheral registers.

MyRoboPath Engineering Lab
Peer-Reviewed Open-Source Hardware & Firmware Guide

Key Engineering Takeaways

  • Flash Memory is non-volatile ROM where your compiled C++ program code binary resides permanently.
  • SRAM (Static RAM) is volatile memory where global variables, dynamic buffers, and function call stacks are stored during execution.
  • EEPROM / NVS (Non-Volatile Storage) stores user settings (like Wi-Fi passwords and PID motor gains) that persist across reboots.
  • The Stack grows downward from high memory; the Heap grows upward from low memory. If they collide, a catastrophic Stack Overflow crash occurs.
  • In 32-bit microcontrollers, hardware pins and timers are controlled by reading and writing to specific 32-bit Memory-Mapped I/O addresses.
Prerequisites
  • Pointers and variables in C/C++
Required Hardware / Tools
  • Any microcontroller development board

The Three Primary Memory Types in Microcontrollers

Unlike a desktop computer where everything is loaded into general-purpose DDR RAM, microcontrollers divide memory into 3 distinct specialized silicon pools: ### 1. Flash Memory (Program ROM) - **Non-Volatile** (data remains when power is unplugged). - Stores the compiled firmware binary code, interrupt vector tables, and constant string literals (`PROGMEM`). - Rated for $\approx 10{,}000 - 100{,}000$ write/erase flash cycles. ### 2. Static RAM (SRAM) - **Volatile** (cleared to zero whenever power is turned off). - Ultra-fast read/write memory storing active variables, state machines, and calculations. ### 3. EEPROM / NVS Flash - **Non-Volatile byte-level storage** used to save calibration parameters, robot zero-point offsets, and network credentials that can be modified at runtime.
Microcontroller memory layout Flash SRAM EEPROM
Figure 6.1: Microcontroller memory map showing Flash ROM, SRAM (Stack & Heap), and Peripheral registers.Visual Guide

Stack vs Heap: Anatomy of Embedded SRAM

Inside the limited SRAM (e.g. $2\,\text{KB}$ on Arduino Uno, $520\,\text{KB}$ on ESP32), memory is partitioned into four dynamic segments: 1. **Static / Global Data**: Pre-allocated variables declared globally outside functions. 2. **The Heap**: Grows **UPWARD** from low memory when dynamic memory allocation is called (`malloc()` or `new`). 3. **The Stack**: Grows **DOWNWARD** from high memory every time a function is called, storing local variables and return addresses. 4. **Free RAM**: The empty gap between Stack and Heap. > [!WARNING] > **Stack Overflow Danger**: If you use deep recursive functions or allocate large local arrays, the Stack will grow down into the Heap, corrupting variables and causing random microcontroller reboots!
SRAM Stack and Heap memory layout diagram
Figure 6.2: SRAM memory layout: Stack growing downward colliding with Heap growing upward.Visual Guide

Frequently Asked Questions

Why should I avoid malloc() and new in microcontroller programming?

On systems with tiny RAM, repeatedly allocating and freeing variable-sized memory blocks causes Heap Fragmentation. Over time, RAM becomes broken into tiny unusable gaps, causing future allocations to fail and crashing your robot.

Tags:#Basic Microcontrollers#Memory#Flash Memory#SRAM#EEPROM#Stack#Heap#Registers