How to stop an Arduino program

You may be wondering how to stop an Arduino program that you’ve written, but it’s not a computer that you open to do a task and close once it’s done. Nothing will be broken if you close it on the spot since you’re not handling writing to files and the like.

A microcontroller such as an Arduino never really stops its execution: it’s meant to run a program forever, as long as it’s powered on. More precisely, when you power up an Arduino, it will:

  • First run the code in the setup() function once.
  • And then run the code in the loop() function over and over again until it loses power.

In fact, being able to completely stop the execution of a program would be a problem : you’d be able to stop the execution of all logic in your circuit and you wouldn’t be able to restart it if needed.

If your goal is to avoid running part of the code when it’s not needed, or to run some part of the code less frequently, there are better ways to go around doing this that work with the architecture of a microcontroller, not against it.

The way you’ll see the most often is with a call to the delay() function inside the loop, which will stop all execution for a while. This doesn’t stop the Arduino from consuming power, but it can help you avoid polling sensors or other devices too frequently. If you’re using interruptions, this will not stop the interruptions from being executed, only the main loop.

Here is an example of how you can avoid running a large block of unneeded code with a delay :

void loop() 
  {
  // Some code here.
  if (stopCondition)
    {
    // Waits for a while and then restarts at the top of the loop.
    delay(1000);
    return;
    }

  // A lot of code that you don't want to run if the stop condition is reached.

  // A bit more delay so it doesn't restart at the top of the loop immediately.
  delay(1000);
  }

There are also ways to lock yourself out forever if you really want, but they are situations you generally want to avoid since they would require a reset or cycling the power to get out of. It’s doable, but it’s not very clean and it means that you would lose control of parts of your project for a while. Some external components may also need a bit of setup time to get ready, which you don’t want to pay every time.

If you only need to stop the execution of your microcontroller and wait for a while until something is done initializing, you can do something like the example below. On the other hand, you should make sure you have a way to get out of it! The only thing that will be able to change the condition in that case and break you out of the while will be an interrupt.

bool stopCondition = true;
void loop() 
  {
  // Some code here.

  while(stopCondition)
    {
    // Waits forever here until an interrupt changes the stop condition to false.
    }

  // The rest of your code that's not being executed.
  }

You can also put the microcontroller in sleep mode if you need to save power for an extended duration. During sleep, the microcontroller will stop its execution completely and will only restart on a trigger such as a timer or an external output. The advantage here is that it will wake up cleanly on an event and resume its execution.