Stop a Loop Arduino
-
Stop the
void loop()
UsingSleep_n0m1
Library -
Stop the
void loop()
Usingexit(0)
-
Stop the
void loop()
Using an Infinite Loop

This tutorial will discuss methods to stop a loop in Arduino. There are two kinds of loops in Arduino; one is the void loop()
which is provided by default and the other one which user creates there own. User-created loops can be ended easily using break
method. To end the void loop()
of Arduino, you can use the following methods.
Stop the void loop()
Using Sleep_n0m1
Library
The above method may work for all Arduino boards, but the Arduino will continue to use power. Using Sleep_n0m1
library, you can put your Arduino CPU to permanent sleep until you reset it manually or using a timer. Please note that this may not work for all Arduino boards.
#include <Sleep_n0m1.h>
Sleep sleep;
unsigned long sleepTime; //how long you want the Arduino to sleep
void setup()
{
sleepTime = 50000; //set sleep time in ms, max sleep time is 49.7 days
}
void loop()
{
// Your Code
sleep.pwrDownMode(); //set sleep mode
sleep.sleepDelay(sleepTime); //sleep for: sleepTime
}
Please make sure to use Sleep_n0m1
library after you have finished with your code. This method will draw only a little power. Use this link for more details.
Stop the void loop()
Using exit(0)
The void loop()
of Arduino can be ended using the exit(0)
method after your code, but note that Arduino.cc
does not provide any method to end this loop, so that this method may not work for all Arduino boards.
void loop() {
// All of your code here
// exit the loop
exit(0); //0 is required to prevent error.
}
Please note that after exit(0)
, your Arduino will stop working until you reset it manually. So make sure you use this method after the code has finished its task.
Stop the void loop()
Using an Infinite Loop
The above method may not work for all Arduino boards, so we have to use another method. The infinite loop method will work for all the Arduino boards, but the Arduino will stay awake and will continue consuming power. In this method, you can insert an infinite loop after your code. Arduino will process your code, enter an infinite loop and stay there until you reset it manually.
void loop(){
// All of your code
while(1){ // infinite loop
}
}
If Arduino enters the infinite loop, it will not return until you reset it, so make sure to use an infinite loop after you have finished with your code.