Why is my program stored in flash memory instead of EEPROM in ATmega328?

EEPROM is normally used to save data between power downs/ups or for backup (in case power goes down).

So if you save some data (e.g. acquired by your sketch) to EEPROM, and the Arduino is powered on again (or not, that does not matter), the data is still in EEPROM and your sketch can continue with the data without losing anything.

And it's a good thing that EEPROM can be written more often than Flash, because once your sketch is finished and you build it in your 'product', you don't need to change it anymore. Only during development you will write to Flash. Maybe it happens a lot, but I doubt you do that many thousands of times (if you do, you need to change your workflow).

But the data on EEPROM might be written daily, hourly or even more (depending on the requirements).


EEPROM is more expensive per bit than flash, so it's only used where it is needed. Sometimes the EEPROM is left out entirely, and applications that require EEPROM have to use an external chip or use some trickery to make flash pretend to be EEPROM. It doesn't make much sense to compromise 2048 Kbytes of flash for a few bytes of non-volatile memory.

If I recall correctly, there were a few early MCUs that had a small amount of EEPROM for program memory (and no flash), but they could not compete.

In most cases flash will only be programmed once or twice, and having to erase it in large blocks rather than byte-by-byte is not an issue, so the designers will use the least expensive memory that meets their high-volume end-user requirements.

The datasheet states 10,000 times minimum endurance for the flash on the ATmega328p, so that's enough for most practical situations, even development where it may be reprogrammed many times.

● Write/erase cycles: 10,000 flash/100,000 EEPROM

However, note that the data retention time may be reduced by many erase/write cycles, which is a reason for not shipping development units into the field. From the ATSAME70Q21 datasheet:

enter image description here

Earlier MCUs often used OTP (one-time programmable) EPROM (but with no physical window for the 'E' on production chips) for the program memory (and they may have had a bit of EEPROM).

There are a few MCUs that use a different technology for the program memory and can be reprogrammed more times- for example TI's Ferroelectric FRAM technology MCUs.

Market disadvantages of FeRAM are much lower storage densities than flash devices, storage capacity limitations and higher cost. Like DRAM, FeRAM's read process is destructive, necessitating a write-after-read architecture.

FRAM memory has a limited number of lifetime read cycles so even if the higher cost is okay, it's not a panacea.

There certainly disadvantages of flash- it's relatively slow compared to RAM, it's erased in large blocks, so a lot of information has to be re-written to change a single bit, limited number of write cycles, requirement for an on-chip charge pump etc. but the high density and low cost is what makes it the dominant technology.


The EEPROM is used for things that can change a lot, like storing some user configuration settings so they survive during reset or powerdown. It is not directly sitting on memory bus, but it is accessed via address and data register. Therefore code cannot be executed from it. It can be erased, read and written one byte at a time, and because it is EEPROM it can handle the many erase and write cycles. The page size is only 4 bytes and the hardware handles a single byte write operation as read-modify-write of 4 bytes for you. It is merely a peripheral.

The Flash on the other hand, sits on the address bus so code can be executed from it. Erase can happen on 128 byte blocks. If you want to change one byte, you must read 128 bytes to temporary buffer, change the byte you want, erase the 128 byte page, and write 128 byte page back. And it can only work for less erase/program cycles. So while it can be used for configuration storage and updates, it juat has some downsides. But code execution is fast from Flash.

Tags:

Flash

Eeprom