Arduino Sketch Too Big Error – How to Reduce Sketch Size?

I spent a long time writing an Arduino program, but only realize the sketch is too big to upload to the Arduino. It’s frustrating because you have to spend more time looking at your program, wondering what could be possibly be removed or changed to save space. In this article I will explain how you could reduce the size of your Arduino sketch.

There are three types of memory in the Arduino, program and variable initial values go to Flash memory, variables and their initial values go to SRAM, and also the EEPROM . Each Arduino has only limited program memory (flash memory) to store your sketch, for example the Arduino UNO has around 30KB, 1KB SRAM and 1KB EEPROM. This is what suggested on the Arduino site which pretty useless:

If you’re using floating point, try to rewrite your code with integer math, which should save you about 2 Kb. Delete any #include statements at the top of your sketch for libraries that you’re not using.

Otherwise, see if you can make your program shorter.

This is pretty ineffective way to reduce the size, without making serious changes to your code structure. Therefore I have done some research and hopefully this could help you optimize your sketch size.

  • Shorten or eliminate string constants
  • Shorten or eliminate initialized arrays
  • Reduce variables to the smallest datatype possible.
  • Store data in EEPROM (you have 1KB in Arduino UNO! So make use of that.)
  • Avoid using digitalRead/Write. Use direct port manipulation instead
  • You can save space by putting common code (repetitive)  in a function or a loop
  • Avoid overload functions. For example, always use Serial.print(char[]) method and possibly use sprintf() to turn a number into char[] and stop using Serial.print(float) or even Serial.print(int). It could save you 500 bytes in this case by not overloading two functions.
  • You can also bypass the Arduino bootloader, and upload your programs using a hardware programmer. This will save you about 2k of Flash (program) memory. Check the Arduino website for information on using a hardware programmer (instead of the USB port) for uploading your programs.

If none of these solve your issue, you might have to consider using a more powerful board with more memory, e.g. the Arduino Mega. In extreme cases, by reducing variable size too much,, you could have overflow issue and memory corruptions. Also make sure you’re not giving too little space for your variables and plan for future expansion.

One thought on “Arduino Sketch Too Big Error – How to Reduce Sketch Size?

  1. WesleyBPeres

    A good book for Optimizations is “Arduino Internals: Look into the heart of your Arduino board” – Dale Wheat

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

Are you Robot? *

For prompt technical support, please use our forum IntoFPV.com. I check blog comments weekly.