Arduino

How Much Code Can an Arduino Uno Hold

Arduino and all-other microcontroller-based devices use memory to store data. Memory is an essential part of any computing system, especially when it comes to embedded systems and design. Allocating Arduino memory in a dynamic way improves efficiency of Arduino boards. Memory can store input and outputs coming from sensors and other devices attached to Arduino. Here we will discuss how much code Arduino UNO can store in its memory.

Arduino Uno Memory Allocation

Microcontrollers used in Arduino boards are specific for embedded system applications. Unlike a conventional computer normally used in our homes and offices microcontrollers have well defined tasks for what they designed for. Microcontrollers lack multilayer cached memory and disk based virtual memory like used in home CPU. Normally while programming Arduino UNO, memory is not considered most of the time until one’s code is stuck due to low memory problems. To get a solution we need to understand the problem first.

Arduino boards mainly consist of three types of memory.

  • SRAM is where Arduino creates variables used in sketches and manipulates them accordingly.
  • Flash memory is a program space where we write Arduino sketches, and it stores Arduino code.
  • EEPROM is a space which usually stores long term data present in our sketch.

SRAM is a volatile memory whose data will be lost once Arduino is powered off while Flash and EEPROM are non-volatile; their information persists even if we remove Arduino power.

Here is a brief comparison of different Arduino boards microcontrollers memory allocation:

Arduino Processor Flash SRAM EEPROM
UNO, UNO Ethernet, Pro Mini, Nano 3.0 ATmega328 32kB 2kB 1kB
Leonardo, Micro ATmega32U4 32kB 2.5kB 1kB
Mega ATmega256 256kB 8kB 4kB

How Much Code Arduino Uno Can Hold

How much code Arduino UNO can store? Answer to this question is, it all depends how we program Arduino UNO. Arduino UNO has three types of memory as discussed earlier, if we exceed any one of these our code will not compile. Arduino UNO has 32kB of Flash memory which is sufficient to write thousands of lines of code.

Normally while writing Arduino code SRAM is the most valuable memory on Arduino boards. Arduino UNO has only 2kB of SRAM which equals 2048 bytes. That’s not too much to program Arduino UNO for extensive user interface and graphical applications. Arduino is powerful enough to control motors, sensors, and drivers but not enough to handle a whole running human robot.

To check how much space is Arduino sketch utilizing, run a program and look for memory usage in the output window.

For example, after compiling a simple LED blink program, we got the output as shown below. Here 2% of Flash memory equal to 924 bytes out of 32256 bytes(32kB) is used by the Blink program written in Arduino IDE. While 9 bytes of SRAM out of total 2048 bytes(2kB) is utilized in creating variables used in Blink LED sketch.

When we compile the Arduino program, the IDE will tell how big the problem is. Using some optimization techniques, we can increase Arduino program holding capacity. Below image shows an example of SRAM and Flash memory which exceeds microcontroller data limits.

How to Optimize Arduino Memory

Note that there is not much Arduino UNO memory available there such as SRAM is only 2kB. It can easily be used up using some useless strings in a sketch. For example:

char message[] = "LinuxHint.com";

Declaration like these can eat up lots of SRAM. Here “LinuxHint.com” puts 14 bytes into SRAM each of these char takes 1 byte, plus 1 for the terminator ‘\0’.

How to Optimize Arduino Code for Better Usage of Memory

Optimization of Arduino code is essential for complex projects so here are few ways to optimize Arduino sketch.

Remove Dead Code

If Arduino code is calling multiple libraries, then there might be a chance a portion of code is not in use. Remove all the unused libraries, functions, and variables. If one is not sure about them, comment it out. If the program still compiles and works fine then that part of code is not used by Arduino.

Libraries consume a lot of SRAM, like using an SD-card library can take up to 1kB of SRAM. Avoid unnecessary library usage while writing sketches.

Store Constant String in Flash Memory

Static strings can be one of the main causes of Arduino memory wastes. For example:

Serial.println("LinuxHint.com");

Static strings like these are automatically copied in SRAM from Flash memory. To avoid this, use F() macro function. This will prevent SRAM from calling it directly and saves memory. F() function can be applied as follows:

Serial.println(F("LinuxHint.com"));

Using the F() macro in the above string we have saved 14 bytes of SRAM.

Correct Data Type

While using large arrays and lookup tables, use the data type according to need. Use the smallest data type that can fit data easily. For example, int will take two bytes while byte will take only one. Similarly avoid using float when you have a whole number try using int. This will save extra bytes in Arduino sketch which will give overall extra space to write sketches. Different types of data types and memory they occupy in Arduino is shown in the following table:

Data Type Size (Bytes) Range of Values
Void 0 null
bool/boolean 1 True/False
Char 1 -128 to +127
unsigned char 1 0 to 255
Byte 1 0 to 255
Int 2 -32,768 to 32,767
unsigned int 2 0 to 65,535
Word 2 0 to 65,535
Long 4 -2,147,483,648 to 2,147,483,647
unsigned long 4 0 to 4,294,967,295
Float 4 -3.4028235E+38 to 3.4028235E+38
Double 4 3.4028235E+38 to 3.4028235E+38
String Character array

Conclusion

In this guide, we have covered how much code Arduino Uno can hold, further we discussed different parameters that lead to low memory issues. Dynamic memory allocation using Arduino functions can be very helpful in project building. Using techniques mentioned one can optimize Arduino memory usage.

About the author

Kashif

I am an Electrical Engineer. I love to write about electronics. I am passionate about writing and sharing new ideas related to emerging technologies in the field of electronics.