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

by Oscar

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.

Some of the links on this page are affiliate links. I receive a commission (at no extra cost to you) if you make a purchase after clicking on one of these affiliate links. This helps support the free content for the community on this website. Please read our Affiliate Link Policy for more information.

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.

Leave a Comment

By using this form, you agree with the storage and handling of your data by this website. Note that all comments are held for moderation before appearing.


Rafi Dharmawan 7th November 2020 - 7:01 pm

thanks, it help a lot

Yurii 28th September 2019 - 6:01 pm

Avoid using sprintf, it takes about 1.5 kBytes

Danny 25th April 2019 - 9:20 am

How do I use direct port manipulation instead of using digitalRead/Write?

WesleyBPeres 26th June 2015 - 5:07 pm

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