RAM Check for ATmega328P using Ethernet + SD + i2C Libraries

By Kendro VillaruelKendro Villaruel, on 06 Jul 2013 02:58

ATmega328P is a microcontroller chip made by ATMEL that is commonly used by basic Arduino Boards and clones.
However, it has its limitations when using multiple libraries. This page talks about how the ATmega328P can handle
multiple libraries by measuring the SRAM capacity.

Popular boards that use this chip (ATmega328P) are:

- Arduino UNO Board rev3
- Arduino Dueminalove
- Arduino BT
- Arduino Nano
- Arduino Mini
- Arduino Arduino Lilypad
- Arduino Pro/ Pro Mini
- Arduino Ethernet Board
- Arduino Ethernet Board + PoE

Memory Specifications

Memory Type Size
Flash Memory 32 KB
SRAM2 2 KB
EEPROM 1 KB
Initial RAM = 9 bytes out of 2048 bytes used
Initial.PNG
ATmega328P without any code written in the sketch has an initial RAM size of 2039 bytes (2048 - 9 bytes).
Adding Ethernet Library = 176 bytes out of 2048 bytes used
Ethernet.PNG
Adding the Ethernet Library (standard lib) leaves 1872 bytes available for coding.
- Ethernet library requires 167 bytes of SRAM (176 - 9 initial ram).
Adding Ethernet Library + SD Card = 814 bytes out of 2048 bytes used
Ethernet%20%2B%20SD.PNG
Adding the Ethernet Library (standard lib) and SD Card (optimised) leaves 1234 bytes available for coding.
- Ethernet library requires 167 bytes of SRAM (176 - 9 initial ram).
- SD Card library requires 638 bytes of SRAM.
Adding Ethernet Library + SD Card + LCD i2C = 1042 bytes out of 2048 bytes used
Ethernet%20%2B%20SD%20%2B%20i2C.PNG
Adding the Ethernet Library (standard lib) and SD Card (optimised) leaves 1006 bytes available for coding.
- Ethernet library requires 167 bytes of SRAM (176 - 9 initial ram).
- SD Card library requires 638 bytes of SRAM.
- LCD i2C library requires 228 bytes of SRAM.

Data type RAM consumption

Data type = byte

byte.PNG
A byte data type requires 1 byte of RAM space in your chip. As shown in the image above - used RAM space is
10 bytes (remember the initial 9 bytes that the sketch has).

Data type = unsigned int / int / word

uint.PNG
An unsigned int / int / word data type requires 2 bytes of RAM space in your chip. As shown in the image
above - used RAM space is 11 bytes (remember the initial 9 bytes that the sketch has).

Data type = unsigned long / long

ulong.PNG
An unsigned long / long data type requires 4 bytes of RAM space in your chip. As shown in the image
above - used RAM space is 13 bytes (remember the initial 9 bytes that the sketch has).

Data type = String with 7 characters

arduino%20string.PNG
A String data type that has 7 characters (arduino) requires 23 bytes of RAM space in your chip. As shown in
the image above - used RAM space is 32 bytes (remember the initial 9 bytes that the sketch has).

Data type = String with 19 characters

arduino%20lab%20bigfoot.PNG
A String data type that has 19 characters (arduino lab bigfoot) requires 35 bytes of RAM space in your chip.
As shown in the image above - used RAM space is 44 bytes (remember the initial 9 bytes that the sketch has).

Conclusion

By using Ethernet, SD Card, I2C Library in one sketch, you are using 1kilo byte of RAM already! Having 1 KB RAM left
will be inconvenient especially when you are doing complex programming. Although it is not the length of your sketch
that consumes the RAM space, but the number of variables you declare: from byte, int, long, to char and strings!

An average program multitasking features require about 500 to 1000 bytes of RAM. So when you don't have enough
RAM left, try switching to higher micro controllers (ADK or 2560) which has 8 KB of SRAM. Or by optimising the program.

Add a New Comment