Why Is Your Arduino Crashing? 4 Common Causes And Solutions!
When I first heard that an Arduino could crash, I pictured a scenario in which Arduinos crashed into each other. However, that’s not really what happens when an Arduino crashes, so my past self shouldn’t have taken it too literally.
Still, you may be wondering: What causes an Arduino to crash?
An Arduino will crash if it receives poor dynamic heap allocation, receives inconsistent power, calls too many functions, or encounters poor memory allocation. As you can see, most of the scenarios involve the Arduino running out of memory, which results in a crash.
The rest of this article will be dissecting each of the four scenarios in addition to providing you solutions. But first, let’s define what crashing is:
What Is Crashing?
An Arduino crash is similar to an auto-reset since your Arduino stops everything that it’s doing and starts over from the beginning. This happens because your Arduino’s chip stops working so it abandons all the tasks. This can be very annoying, especially if you’re in the middle of running a project.
The best and easiest solution to handle its abrupt stop is to incorporate code for a watchdog timer. This timer is unique in that it will constantly check on your Arduino and if it senses any problems, it will immediately restart your Arduino.
If you want to learn more about auto-reset and how you can prevent it from happening to you, you should go check out my Arduino auto-reset guide.
Common Causes For Crashing and Hanging (With Solutions)
1. Dynamic Heap Allocation
Dynamic heap allocation is when you try assigning memory space as your Arduino is running the sketch. This is a bad idea because you don’t know exactly how much memory you need to assign.
In most (worst-case) scenarios, you would overestimate your memory usage (which wastes valuable memory) or underestimate it (which doesn’t allow your program to run properly).
Solution: You should avoid using the malloc() function since it allocates your memory. However, if you find yourself really needing to use it, I would recommend also including the free() function.
2. Power Brownout
A power brownout is basically a situation where your Arduino’s power supply dips. Consequently, because your Arduino is not receiving enough power to continue running, it will crash and restart.
There are many culprits for a power brownout, and it all depends on the power supply you’re relying on and your situation. The common causes for a power brownout may include:
- Heavy loads
- A heavy load is a situation where your Arduino board is competing for power with a power-hungry component. Components like DC (direct current) motors eat up a lot of power, so if you don’t use an additional power source, it is likely that your Arduino will crash and auto-reset.
- Solution: Make sure to include an additional power source to support these heavy loads
- A heavy load is a situation where your Arduino board is competing for power with a power-hungry component. Components like DC (direct current) motors eat up a lot of power, so if you don’t use an additional power source, it is likely that your Arduino will crash and auto-reset.
- Dying battery pack
- Solution: If you’ve made your Arduino project portable so that it relies heavily on batteries or a battery pack, you’ll need to check to see if your battery or battery pack still have any juice left inside of them. If not, you’ll definitely need to switch to a different battery or recharge your battery pack.
- Power supply disruption or noise
- Solution: If you’re connecting your Arduino to an outlet or computer, make sure the power you’re receiving isn’t inconsistent. A sudden drop or noise in your power supply can cause your Arduino to crash. Moreover, if you’re using a computer that doesn’t supply power when it sleeps, you’ll need to change the computer setting so that it never sleeps.
Many Arduino boards usually a varying range of input voltage it can take. For instance, the Arduino Uno can handle 6V to 20V.
Solution: It is recommended that you supply between 9V-12V. That way, the Arduino’s voltage regulator can handle the input voltage in case you accidentally give it a little more than 12V. The 9V is recommended as the minimum just in case there’s a small power drop.
3. Excess Functions
What I mean by this is that you’re calling too many functions. This situation can be exacerbated if you have a function that calls itself (also known as a recursive loop). If you find yourself stuck in this situation, I highly recommend that you press the reset button and fix your code.
As you can see, calling a function uses up precious memory, so you’ll need to make strategic decisions.
If you use up all the memory, your Arduino won’t be able to run the rest of the code. As a result, you may see your Arduino board reset, similar to you pressing the reset button on the Arduino board itself.
Solution: As obvious as it may sound, you should try to call less functions or try to find ways to strategically call your functions. If you simply don’t have enough memory, I’d highly recommend you buy the Arduino Mega 2560 R3 since it has the most memory.
4. Memory Misallocation
Another reason for a crash is improperly allocating your memory. If this happens, your Arduino will run out of memory and won’t have the ability to run all your code.
Your Arduino can run out of memory if you use a large number of variables improperly. There are two types of variables that affect the memory usage for your Arduino: global and local variables.
Local variables can only be used in the function they’re declared in. Global variables are declared outside of any functions, so they have a global scope. That means any function following it can call and use it.
Solution: If you only need the variable for one or two specific functions, just create the variable inside the functions to save memory (as local variables). That way, you’re less likely to run out of memory and cause your Arduino to crash.
If you want to learn more about memory or memory optimization tips so that your Arduino won’t run out of memory and crash, then you should definitely go read my 2-part memory guides. The first one is a memory optimization guide and the second talks about adding memory to your Arduino (in addition to more memory optimization tricks).
Summary – tl;dr (Too Long; Didn’t Read)
Here are some of the key points you should remember after reading this article.
- Crashing happens due to your Arduino running out of memory. Consequently, it will stop everything it’s doing and restart.
- The four causes of crashing are:
- Using dynamic heap allocation
- The solution is to not manually allocate memory
- Not using a constant supply of power
- The solution is to make sure your Arduino has a reliable power source
- Calling too many functions
- The solution is to strategically use less functions
- Allocating too much memory to a variable
- The solution is to utilize local variables more often than global ones.
- Using dynamic heap allocation
Hopefully, by the end of this article, you have found it to be a big hit!