ATmega328P/How a sketch is written/11. Flash, SRAM and EEPROM
How a sketch is written · 11 of 33

Flash, SRAM and EEPROM

The ATmega328P has three memories: 32 KB of flash for the sketch, 2 KB of SRAM for its variables, and 1 KB of EEPROM for anything that must survive a reset. SRAM is the one that runs out, and quoted text is usually what fills it.

Your board

Three memories, three jobs

MemorySizeHoldsAfter a reset or power-off
Flash32 KB, 32,256 bytes for the sketchthe sketch, and fixed textkept
SRAM2,048 bytesvariables, the stacklost
EEPROM1,024 bytesvalues your sketch saveskept

The IDE keeps the rest of the flash, 512 bytes, for the bootloader, the program that receives uploads over USB. The UNO's settings keep 512 bytes and allow a sketch 32,256. The Nano's keep 2 KB and allow 30,720, under either Processor choice, ATmega328P or Old Bootloader: that is the number in the Nano's entry in the IDE's boards.txt, and the IDE refuses a bigger sketch even if the bootloader on the chip is smaller. Flash is written at upload time and Microchip rates it for at least 10,000 write and erase cycles, which is a lot of uploads.

The two lines after a build

The IDE prints these when it finishes compiling. For the sketch on this page, with the Arduino AVR core this book was checked against:

Sketch uses 2006 bytes (6%) of program storage space. Maximum is 32256 bytes.

Global variables use 188 bytes (9%) of dynamic memory, leaving 1860 bytes for local variables. Maximum is 2048 bytes.

With Arduino Nano chosen the sketch is the same 2006 bytes, and the first line ends Maximum is 30720 bytes.

The first line is flash: your code, the parts of the Arduino core it calls, and any fixed text. The second is SRAM, and it counts only global variables, the ones made once when the chip starts. 188 bytes is about the least a sketch that uses Serial can manage; most of it is Serial's two 64-byte buffers, one for each direction.

"Leaving 1860 bytes for local variables" is the stack: the variables made inside functions while they run, and the return address of every call in progress. The IDE cannot know how much of it the sketch will need. If the stack grows into the globals, the sketch fails in ways that look random.

Quoted text costs SRAM, unless it is in F()

Where your strings go
20 strings, in SRAM
Each message is printed with
Different 30-letter messages in the sketch20
Global variables
808 bytes
Left for the stack
1240 bytes
Text in SRAM
620 bytes
Every quoted string is copied into SRAM when the sketch starts. Each 30-letter message costs 31 bytes, the extra one being the zero that marks its end. These 20 cost 620. Slide right to see where the IDE starts to complain.

A string in quotes, Serial.println("Starts counted"), is stored in flash with the program and copied into SRAM when the chip starts, so that it can be read like any other variable. It costs its length plus one byte, the zero that marks its end, of SRAM for the whole run, whether it is printed once or never.

F("Starts counted") tells the compiler to leave it in flash, and print() reads it from there a byte at a time. It works anywhere print and println do. The sketch above prints both its strings that way, which is why its global variables are the bare 188.

Above 1536 bytes, three quarters of SRAM, the IDE adds a line: "Low memory available, stability problems may occur." It still uploads. Above 2048 it stops with "Not enough memory" and uploads nothing.

EEPROM: 1024 bytes that survive

EEPROM keeps its contents without power, through resets and through uploads over USB. The EEPROM library that comes with the IDE reads and writes it by address, 0 to 1023:

  • EEPROM.read(address) returns one byte.
  • EEPROM.write(address, value) writes one byte, every time it is called.
  • EEPROM.update(address, value) reads the byte first and writes only if the value is different.
  • EEPROM.get and EEPROM.put do the same for a whole variable, a long or a float across several bytes. put uses update for each byte.

A byte of EEPROM that has never been written reads 255; a four-byte unsigned long of them reads 4,294,967,295, which the sketch takes to mean "never used".

Every write wears it out

Microchip rates each EEPROM byte for at least 100,000 write and erase cycles, and each write takes about 3.3 ms. Once a reset is fine: 100,000 resets is more than the board will see. A write on every pass of loop() is not: with a value that changes each time, update writes too, and 100,000 writes at 3.3 ms each is about five and a half minutes.

So write when something changes that has to be kept: a setting chosen, a count at a reset, a reading once an hour. Never on a timer you have not worked out. The libraries chapter is next, and EEPROM.h was the first library in this book.

The code

Count_Resets.ino

Every time the chip starts, the sketch reads a number from EEPROM, adds one, and writes it back. Open the Serial Monitor at 9600 baud, then press RESET a few times.

// Count_Resets.ino — count every start of the board in EEPROM
// Tools > Board: Arduino Uno, or Arduino Nano with
//   Processor ATmega328P (Old Bootloader if the upload says
//   "not in sync")
// Tools > Port: the CH340 port
// Serial Monitor: 9600 baud. Type c and Enter to clear the count.
// Wiring: none. Press RESET to add one.
#include <EEPROM.h>

const int ADDRESS = 0;   // bytes 0 to 3 hold the count

void setup() {
  Serial.begin(9600);
  unsigned long starts;
  EEPROM.get(ADDRESS, starts);
  if (starts == 0xFFFFFFFF) {   // erased bytes read 255: never used
    starts = 0;
  }
  starts = starts + 1;
  EEPROM.put(ADDRESS, starts);  // writes only the bytes that changed
  Serial.print(F("Starts counted: "));
  Serial.println(starts);
}

void loop() {
  if (Serial.available() > 0 && Serial.read() == 'c') {
    unsigned long zero = 0;
    EEPROM.put(ADDRESS, zero);
    Serial.println(F("Cleared. Press RESET."));
  }
}

Opening the monitor counts too, because it resets the board, and so does unplugging. Type c and press Enter to put the count back to 0.

When it does not work

The count starts at a strange large number

Those bytes of EEPROM were written before, by an earlier sketch or at the factory, and the sketch can only tell never-written bytes (all 255) from used ones. Type c and press Enter to clear it, then press RESET.

Low memory available, stability problems may occur

Global variables have passed three quarters of the 2048 bytes of SRAM, leaving under 512 for the stack. Wrap every quoted string you print in F(), shrink arrays, and use byte instead of int for small numbers. The warning goes when the total drops to 1536 or below.

The sketch resets itself or prints rubbish after a while

A likely cause is the stack running into the variables: SRAM is full, and the IDE's number only counts globals. Large local arrays and String objects that grow are the usual culprits. Move text into F() and fixed-size arrays out of functions, and watch whether it stops.

EEPROM values are gone after uploading with a programmer

An upload through USB uses the bootloader, which writes flash only and leaves EEPROM alone. Burning the bootloader or uploading with a programmer starts with a chip erase, and the standard UNO and Nano fuse settings do not protect EEPROM from it, so expect it to be wiped.

Where this goes next

Code someone else wrote, added with one #include line, and where the IDE keeps it.

What a library is →

Edit this page — content/books/atmega328p/flash-sram-and-eeprom.mdx

Discuss this article

Ask about this page. The answer stays here, on the page it belongs to, for whoever hits the same wall next.

Browse Modules and blocks on the forum →