
For those new to microcontrollers, the terms “ROM” and “RAM” can be quite confusing. Both are storage areas, but why are they divided into two? Some say ROM is “dead memory” and RAM is “live memory”; while this is not incorrect, it is too vague.
Whether it’s the 51 microcontroller, STM32, or the ATmega328P used in Arduino, both ROM and RAM are essential. They are both types of “memory”, but the way they “store” data is vastly different.

First, let’s talk about ROM, which stands for “Read-Only Memory”. The name might be misleading; modern ROM is no longer just “read-only”. This term has been carried over from early products. In the past, data was “burned” into ROM at the factory, and users could only read it, with no way to modify it (for example, the ROM storing station information in old radios). However, the ROM in modern microcontrollers has been upgraded and can be rewritten multiple times, although the way it is written is different from RAM. Its core function is to “store data without loss”. Even if the microcontroller loses power, the contents of ROM can be retained and used again when powered on. Now, regarding RAM, which stands for “Random Access Memory”. The term “random access” means that it can read and write data without following a sequence; it can directly access addresses, unlike hard drives that find data by tracks. However, it has a critical drawback: “data is cleared when power is lost”. When the microcontroller loses power, everything stored in RAM is gone, like wiping a blackboard clean. Its core function is to “temporarily store data”, serving as a “temporary workspace” for the microcontroller during operation.

Why do we need both? Because when a microcontroller is working, it needs both a “long-term storage warehouse” (for example, programs and fixed parameters) and a “temporary workspace” (for example, intermediate values during calculations and sensor data that changes frequently). ROM serves as the warehouse, storing data securely; RAM serves as the table, convenient for use but not suitable for long-term storage.
Both are essential; without ROM, the microcontroller wouldn’t know what to do when powered on; without RAM, there would be no place to temporarily store data during operation, and the program wouldn’t run at all.
ROM
Many people think ROM has only one function: storing programs. This is not true; modern microcontroller ROMs have many functions, and different types of ROM have different uses. Let’s first discuss several common types of ROM in microcontrollers and what they specifically do.
Early microcontroller ROMs were quite limited, but with technological advancements, the types of ROM have increased significantly. The most common types include: Flash ROM: Most modern microcontrollers (such as the entire STM32 series, STC89C52 in the 51 series, and ATmega series used in Arduino) use Flash ROM. Its characteristics include the ability to be erased and rewritten multiple times (with erase cycles ranging from tens of thousands to hundreds of thousands, which is sufficient for daily use), and it retains data even when power is lost. When we “burn a program” into a microcontroller, we are actually writing the program into Flash ROM. EEPROM (Electrically Erasable Programmable Read-Only Memory): Some microcontrollers come with a small amount of EEPROM (for example, the ATmega328P has 512 bytes of EEPROM, and the STM32L series also has it). It is similar to Flash ROM in that it retains data when power is lost and can be rewritten multiple times, but it offers more flexibility in erasing. Flash ROM typically requires “block erasing” (for example, erasing 128 bytes or 256 bytes at a time), while EEPROM can be erased “byte by byte”, allowing for modification of a single byte without affecting others. However, its downside is that the erase speed is slower than Flash, and its capacity is smaller (generally just a few KB).
RAM
If ROM is the “warehouse”, then RAM is the “temporary workspace” during microcontroller operation. When a program runs, it needs a place to temporarily store data, which relies entirely on RAM. Its characteristics are “fast read and write speeds” but “data loss when power is lost”, serving a completely different purpose than ROM.
The data stored in RAM can be summarized into two categories: temporary variables during program execution and “state data” during program execution. The common feature of this data is that it is only useful while the program is running; it becomes useless when power is lost; and it requires frequent read and write operations, demanding high speed. First, let’s discuss “temporary variables”. The variables we define while programming (for example, int a=0; float temp=25.3;) are by default stored in RAM. For instance, when the microcontroller reads sensor data, it first stores the raw value in a RAM variable, then performs calculations (such as subtracting calibration values and multiplying by coefficients), and the intermediate values generated during the calculations are also stored in RAM. The final result may be stored in RAM (for temporary use) or written to ROM (for long-term storage).
Why must this data be stored in RAM? Because they require “fast read and write”: RAM’s read and write speeds are much faster than those of ROM (for example, the RAM read and write speed of STM32 can reach tens of MB per second, while Flash read and write speeds are usually only a few MB per second). During program execution, variables and state data may need to be read and written dozens or even hundreds of times per second. If they were stored in ROM, the speed would be insufficient, causing the program to lag or even fail to run.
Some may ask: “Since ROM can store data, can it replace RAM?” or “Since RAM is fast, can we add a battery to prevent data loss when power is off, replacing ROM?” In fact, early engineers did try this, such as using “battery-backed RAM” as ROM, which could indeed retain data when power was lost and had fast read and write speeds, but the cost was too high (several times more expensive than Flash), and the capacity could not be made large. It is now only used in a few special devices. While ROM (especially Flash) can store data, its read and write speeds are slow, and it has limitations on erase cycles, making it unable to replace RAM. The temporary data during program execution requires fast read and write, which ROM cannot provide. Therefore, in the end, ROM and RAM are not about “which is better” but rather about “working together”: ROM is responsible for “long-term storage”, ensuring that the microcontroller has programs and parameters available when powered on; RAM is responsible for “temporary turnover”, allowing programs to run efficiently and process data quickly.