Sub topic 14
Memory sizes on other boards
This chapter assumes you have read the earlier chapters on flash, SRAM, EEPROM, F(), PROGMEM and the freeRam() helper. It shows which of those techniques carry over to other boards and which ones change or stop working.
The earlier chapters used the Uno (ATmega328P) as the reference. Its 2 KB of SRAM forces careful habits. Other boards have much more memory, but they differ in more than size. The architecture, the width of int, the way flash is read and the way EEPROM works can all differ, and these differences decide whether your code compiles and behaves correctly.
Typical memory sizes
The figures below are for common boards. Check the datasheet or board documentation for your exact variant, because vendors ship several revisions.
| Board | Chip | Flash | SRAM | EEPROM |
|---|---|---|---|---|
| Uno R3, Nano | ATmega328P | 32 KB | 2 KB | 1 KB |
| Leonardo, Micro | ATmega32U4 | 32 KB | 2.5 KB | 1 KB |
| Nano Every | ATmega4809 | 48 KB | 6 KB | 256 bytes |
| Mega 2560 | ATmega2560 | 256 KB | 8 KB | 4 KB |
| Uno R4 Minima and WiFi | Renesas RA4M1 | 256 KB | 32 KB | 8 KB data flash |
| Zero, MKR series | SAMD21 | 256 KB | 32 KB | none (emulated by a library) |
| Due | SAM3X8E | 512 KB | 96 KB | none |
| Nano 33 BLE | nRF52840 | 1 MB | 256 KB | none |
| Raspberry Pi Pico | RP2040 | 2 MB external | 264 KB | none |
| ESP32 modules | ESP32 | 4 MB external (typical) | about 520 KB | none (emulated in flash) |
Three patterns stand out:
- SRAM grows far faster than flash from board to board. The Mega has eight times the Uno's flash and four times its SRAM. The Uno R4 has eight times the flash and sixteen times the SRAM.
- Many 32-bit boards have no real EEPROM. The EEPROM library on those boards stores data in a reserved area of flash or in data flash.
- On ESP32 and RP2040 boards, the program lives in an external flash chip. The flash size depends on the module you bought, not only on the chip.
Not all of a board's flash is available to your sketch. The Uno's bootloader uses 0.5 KB, and the compiler's report uses the remaining size as its limit. That is why the report says "maximum is 32256 bytes" for an Uno, not 32768.
Why more memory does not remove the problem
More SRAM makes the heap and the stack less likely to collide, but the failure modes stay the same.
- The heap still fragments if you grow and free
Stringobjects in a loop. A 32 KB heap takes longer to fail than a 2 KB one, but a device that must run for months can still hit the failure. - A recursive function still overflows the stack, though later.
- On boards with an operating system layer, such as ESP32 with FreeRTOS, each task has its own fixed stack. A task with a 4 KB stack can overflow even when the chip has hundreds of KB free.
Treat a large SRAM as extra margin. Keep the habits from the earlier chapters: fixed buffers, snprintf(), and measuring.
What changes in your code
The size of `int`
On AVR boards, int is 16 bits. On ARM, ESP32 and RP2040 boards, it is 32 bits. Code that relied on a 16-bit int wrapping at 65535 behaves differently, and a struct written to EEPROM can have a different layout and size. When a value must have a known width, use the fixed-width types such as uint16_t and int32_t. This also helps when you move a sketch between boards.
PROGMEM and F()
On AVR, flash and SRAM are separate address spaces. That is the reason PROGMEM and pgm_read_word() exist. On most 32-bit boards, flash is mapped into the same address space as RAM, so a const array stays in flash and ordinary indexing reads it.
The Arduino cores for these boards keep PROGMEM, F() and the pgm_read_* macros so that AVR sketches still compile. In most cases they do nothing special there. Your code still works, but the macros are not what saves memory on those boards.
The reverse is not safe. If you write a sketch for a 32-bit board and skip PROGMEM, it will compile there but can run out of SRAM when you move it to an Uno. If you plan to support AVR boards, keep using F() and PROGMEM.
Measuring free memory
The freeRam() helper from earlier uses the symbols __heap_start and __brkval. These are specific to the AVR C library, so the helper does not compile on ARM boards. Use a small wrapper that picks the right method per architecture.
#if defined(ARDUINO_ARCH_AVR)
extern int __heap_start;
extern int *__brkval;
#endif
long freeMemory() {
#if defined(ARDUINO_ARCH_AVR)
int v;
return (int)&v - (__brkval == 0 ? (int)&__heap_start : (int)__brkval);
#elif defined(ARDUINO_ARCH_ESP32) || defined(ARDUINO_ARCH_ESP8266)
return ESP.getFreeHeap();
#else
return -1;
#endif
}
void setup() {
Serial.begin(9600);
while (!Serial) {
}
Serial.print(F("sizeof(int): "));
Serial.println((int)sizeof(int));
Serial.print(F("sizeof(void*): "));
Serial.println((int)sizeof(void *));
Serial.print(F("Free memory: "));
Serial.println(freeMemory());
}
void loop() {
}On an Uno this prints 2 for int, 2 for a pointer and the free SRAM. On an ESP32 it prints 4, 4 and the free heap in bytes. On boards that fall into the last branch, it prints -1, which means "no method written for this core". Look in that core's documentation for its own function, or measure with the compiler's report instead.
Note that the two numbers do not mean the same thing. On AVR, the value is the gap between heap and stack. On ESP boards, ESP.getFreeHeap() counts only free heap. Do not compare them directly.
EEPROM
The EEPROM library keeps the same get() and put() calls on many boards, but the details differ.
- On ESP8266 and ESP32, you must call
EEPROM.begin(size)before reading or writing, andEEPROM.commit()afterward to write the changes to flash. - On boards with no EEPROM, such as SAMD21, you need a separate emulation library or another storage method.
- On the Nano Every, the EEPROM is only 256 bytes. A settings struct that fit on an Uno can overflow it.
- Flash-based storage wears out after a limited number of write cycles, so do not write on every
loop()pass.
Choosing what to do on each class of board
The right amount of care depends on the board, as the diagram shows.
In short:
- On boards with 8 KB of SRAM or less, apply every technique from this tutorial.
- On boards with tens of KB, you can use
Stringfor short-lived, bounded text. Avoid code that builds long strings in an endless loop. - On boards with hundreds of KB, the main risks move to per-task stacks, heap fragmentation over days of uptime, and the limits of the radio stack or other libraries.
Common mistakes when moving between boards
- Copying
freeRam()into an ARM sketch. It fails at compile time because the AVR symbols do not exist. - Assuming
intis 16 bits. Arithmetic that overflowed on the Uno does not overflow on a 32-bit board, which hides bugs until you go back to the smaller board. - Writing a struct to EEPROM on one board and reading it on another. Sizes and padding can differ, so the stored bytes do not match.
- Forgetting
EEPROM.commit()on ESP boards. The values appear to save, then vanish after reset. - Reading the flash size from the chip name instead of the build report. For ESP boards, the partition scheme you select in the IDE changes how much flash your sketch can use.
Exercise
You have a sketch that works on an Uno and prints a 16-bit counter that wraps to 0 after 65535. On an ESP32 the counter keeps counting past 65535. Why, and how do you make the behavior the same on both boards?
Show answer
On the Uno, int and unsigned int are 16 bits, so the counter wraps at 65535. On the ESP32 they are 32 bits, so the counter does not wrap until a much larger value. Declare the counter as uint16_t, which is 16 bits on every board, so it wraps at 65535 on both.
Exercise
A settings struct of 300 bytes is saved with EEPROM.put(0, s) and works on a Mega 2560. Name two things that can go wrong when you build the same sketch for a Nano Every and for an ESP32.
Show answer
On the Nano Every, the EEPROM holds only 256 bytes, so the 300-byte struct does not fit and the write goes out of range. On the ESP32, you must call EEPROM.begin(size) before using the EEPROM and EEPROM.commit() after writing, or the data is not stored in flash.