0
0
Power-electronicsComparisonBeginner · 4 min read

Flash vs RAM vs EEPROM in Microcontroller: Key Differences & Usage

In microcontrollers, 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:

FeatureFlashRAMEEPROM
TypeNon-volatile memoryVolatile memoryNon-volatile memory
PurposeStores program codeStores temporary dataStores persistent data
Data RetentionKeeps data without powerLoses data when power offKeeps data without power
Write SpeedSlower writeFast read/writeSlower write than RAM
Write CyclesLimited (~10,000 to 100,000)UnlimitedLimited (~100,000)
Typical SizeLarge (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:

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;
}
Output
RAM value: 25 Updated RAM value: 50
↔️

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):

c
#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;
}
Output
EEPROM value at address 10: 42
🎯

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.

Key Takeaways

Flash stores program code permanently and retains data without power.
RAM holds temporary data and loses it when power is off.
EEPROM stores small persistent data with limited write cycles.
Use Flash for code, RAM for runtime variables, and EEPROM for saved settings.
Writing to Flash and EEPROM is slower and limited compared to RAM.