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.
- • Pointers and variables in C/C++
- • 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.
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:
- 1Static / Global Data: Pre-allocated variables declared globally outside functions.
- 2The Heap: Grows UPWARD from low memory when dynamic memory allocation is called (
malloc()ornew). - 3The Stack: Grows DOWNWARD from high memory every time a function is called, storing local variables and return addresses.
- 4Free RAM: The empty gap between Stack and Heap.
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.