What is the “Sketch too big” error in Arduino?
Before jumping to the solution of the error first understand what the error is as it can solve 90% of your issue. So, in case of “Sketch too big” error it is obvious that code needs more space than the memory capacity of the Arduino board. In short, there is a need to shorten the code in order to make it run because Arduino boards have a limited amount of space and there is no way to increase it.
Why is the “Sketch too big” error encountered?
Normally this error is faced when the Arduino code is long enough, thus taking a lot of space on the Arduino board. To get an idea that how much space is left you can see the output tab when the code is compiled as the Arduino IDE tells the used storage space as well as the maximum storage space:
How to fix the “Sketch too big” error
There are different solutions that can be done to remove this error that are:
- Using functions instead of writing same lines multiple times
- Removing any unnecessary variables
- Using limited number of global variables
- Making the use of string literals limited
- Declaring the exact array size that is needed
Using functions instead of writing same lines multiple times
Use of functions in the Arduino comes in handy when the same operation is to be repeated at multiple stages in a code. This will considerably reduce the size of the code and will leave you with some space. If you need to read more about creating functions then read this guide.
Removing unnecessary variables
Another thing that one can do is try to reduce the number of unnecessary variables in an Arduino code. For example, if you are saving the result of the same operation in different variables then each new variable will take some space in the code. So, try using the same variable each time the same operation is performed in the code.
Using limited number of global variables
The primary purpose of using the global variables is to access the variable anywhere in the program. The global variables continue to take up the space while the code is running whereas the local variables are only used within the functions, so they take up less space as they are not active during entire code execution. If you need details about local and global variables, then read this guide.
Making the use of string literals limited
The strings that are normally used to print for labeling the results of a program also take a lot of space if a lot of labeling is required. Such strings are also called string literals and to save the space try saving it in the sketch storage rather than in the controller’s memory. To do this use F() macro to save the strings used to label the results of the program.
Declaring the exact array size that is needed
Arrays are normally used to save multiple values at the same time, but they can also take up considerable space if their proper sizes are not declared. This can also free up some space for compiling the code and can prove to Beneficial in rectifying the “Sketch too big” error.
Note: Keep all the above things in mind while writing the Arduino code in order to avoid such error in first place
Conclusion
While programming Arduino, there are chances that one might encounter a number of errors, errors can be removed with a little bit of searching about the potential solution. However, there are some errors that are quite difficult to remove, one of which is the “Sketch too big”. If you are experiencing the same error, then read this guide as I have explained the solutions that can make this error go away.