Flash vs RAM vs EEPROM in Microcontroller: Key Differences & Usage
Flash is non-volatile memory used to store program code permanently, RAM is volatile memory for temporary data storage during execution, and EEPROM is non-volatile memory used to store small amounts of data that must persist after power off. Each serves a distinct role in embedded C programming for managing code and data.Quick Comparison
Here is a quick comparison of Flash, RAM, and EEPROM in microcontrollers:
| Feature | Flash | RAM | EEPROM |
|---|---|---|---|
| Type | Non-volatile memory | Volatile memory | Non-volatile memory |
| Purpose | Stores program code | Stores temporary data | Stores persistent data |
| Data Retention | Keeps data without power | Loses data when power off | Keeps data without power |
| Write Speed | Slower write | Fast read/write | Slower write than RAM |
| Write Cycles | Limited (~10,000 to 100,000) | Unlimited | Limited (~100,000) |
| Typical Size | Large (kB to MB) | Medium (kB) | Small (bytes to kB) |
Key Differences
Flash memory is where the microcontroller stores its program code permanently. It retains data even when the power is off, but writing to flash is slower and limited in the number of write cycles. Flash is usually large enough to hold the entire program.
RAM is fast, volatile memory used to hold variables and temporary data while the program runs. It loses all data when power is lost, so it cannot store persistent information. RAM size is smaller than flash but optimized for speed.
EEPROM is a special type of non-volatile memory used to store small amounts of data that must survive power cycles, like configuration settings or calibration data. It has slower write speeds and limited write cycles compared to RAM but is designed for frequent updates unlike flash.
Code Comparison
Example showing how to store and read a value in RAM in Embedded C:
#include <stdio.h> int main() { int ram_variable = 25; // Stored in RAM printf("RAM value: %d\n", ram_variable); ram_variable = 50; // Change value in RAM printf("Updated RAM value: %d\n", ram_variable); return 0; }
EEPROM Equivalent
Example showing how to simulate storing and reading a value in EEPROM in Embedded C (using a simple array to mimic EEPROM behavior):
#include <stdio.h> // Simulated EEPROM storage unsigned char EEPROM[512]; void EEPROM_write(unsigned int address, unsigned char value) { // Simulate EEPROM write delay EEPROM[address] = value; } unsigned char EEPROM_read(unsigned int address) { return EEPROM[address]; } int main() { EEPROM_write(10, 42); // Write value 42 to EEPROM address 10 unsigned char val = EEPROM_read(10); printf("EEPROM value at address 10: %d\n", val); return 0; }
When to Use Which
Choose Flash to store your program code and constants that do not change during runtime. Use RAM for variables and data that change frequently and only need to exist while the device is powered on. Use EEPROM when you need to save small amounts of data that must persist after power off, like user settings or calibration values.
In summary, Flash is for permanent code storage, RAM is for fast temporary data, and EEPROM is for persistent data that changes occasionally.