Understanding the Infinitive Loop in Arduino: How Many Times Does It Run?

Understanding the Infinitive Loop in Arduino: How Many Times Does It Run?

When crafting an Arduino program, one of the most fundamental concepts to understand is the void loop function. This function plays a crucial role in executing the heart of your program, running repeatedly in an endless cycle once the setup function has completed. Let’s delve into how this loop operates and explore its behavior in different scenarios.

How the Loop Function Works

The void loop function is invoked continuously after the setup function has finished executing. Unlike the setup function, which runs only once at the beginning, the loop function is designed to run repeatedly until the power is removed from the Arduino board or the board is reset.

Here is a simplified breakdown:

Setup Function

Function: Runs once at the start of the program. It is typically used to initialize variables, set pin modes, start using libraries, and perform other one-time setup tasks.

Loop Function

Function: After the setup function completes, the loop function is repeatedly called. There is no built-in limit to how many times it runs. It will continue to loop indefinitely unless interrupted by external factors such as a power shutdown or board reset.

Factors Influencing Loop Frequency

The frequency at which the loop function runs depends on various factors, including the duration of the code inside it and the performance of the Arduino board. If the code within the loop function executes quickly, it can run thousands or even millions of times per second. Conversely, if the code includes delays or long-running processes, the frequency will be significantly lower.

Practical Example

Let's look at a simple example of an Arduino sketch to illustrate this concept:

void setup {
  // Initialize serial communication
  (9600);
}
void loop {
  // Print a message to the Serial Monitor
  (Looping...);
  delay(1000); // Wait for 1 second
}

In this example, the loop function will run once every second due to the delay(1000) statement. Consequently, it would run approximately 60 times per minute or 1,440 times in 24 hours.

Experimental Insights

If you are curious about the precise frequency of the loop function, you can measure it experimentally. For instance, I measured the loop frequency on an Arduino UNO, and it was found to run in 250 nanoseconds (ns) when the loop was empty. This means the loop can run as many as 4 million times per second.

Refer to the screenshot below for a visualization. The loop contains two processor instructions, each taking 125 nanoseconds (ns) to create a pulse. Subtracting the 250 nanoseconds from the period provides the time it takes to run an empty loop. Interestingly, if you use the Serial port, the time taken for each loop iteration will be considerably longer due to the overhead involved in serial communication.

Key Takeaways:

The void loop function runs repeatedly until the Arduino board is powered off or reset. Loop frequency is influenced by the code duration inside the loop and the performance of the board. An empty loop runs in 250 nanoseconds on an Arduino UNO, allowing for up to 4 million iterations per second.

By understanding these fundamental behaviors, you can optimize your Arduino programs for better performance and efficiency.