Arduino Void Setup and Void Loop Functions [Explained]

When you open a new program in the Arduino IDE, you immediately get empty void setup and void loop functions written for you.

void setup() {
  // put your setup code here, to run once:

}

void loop() {
  // put your main code here, to run repeatedly:

}

What are those void setup and void loop functions in Arduino?

In this tutorial I’ll explain to you the role of those functions and how to use them. At the end I’ll also give you some best practices to improve your Arduino programs.

Here is a quick video you can watch as an additional resource to this article:

If you like this video, subscribe to the Robotics Back-End Youtube channel so you don’t miss the next tutorials!


You are learning how to use Arduino to build your own projects?

Check out Arduino For Beginners and learn step by step.


Void setup and void loop: mandatory functions in Arduino

The Arduino void setup and void loop functions are mandatory. Try to compile a code with one of those functions missing, and you’ll get an error.

# Example of what's happening when there is no void setup() function in your Arduino program
C:\Users\user\AppData\Local\Temp\ccioFeXY.ltrans0.ltrans.o: In function `main':
C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino/main.cpp:43: undefined reference to `setup'
collect2.exe: error: ld returned 1 exit status
exit status 1
Error compiling for board Arduino Uno.

When you run a “standard” C/C++ program, you have to write a “main” function. This main function will be called first, and from there, you will call other functions and execute the functionalities of your program.

In Arduino, there is no main function. This is replaced by setup and loop. Instead of one mandatory function, you have 2.

All your Arduino program must include those functions. On top of them, you can add any number of functions, variables, and create as many files as you want. You just need to make sure that the void setup and void loop are present.

How void setup and void loop work

Principle

As the “main” function is called when you run a C/C++ program, the setup and loop functions will be automatically called.

As soon as the program starts:

  1. Variables that you create on top of the program will be declared/initialized.
  2. The setup function will be called once.
  3. All the code you’ve written inside this function will be executed. After that, the setup function exits.
  4. Now the loop function will be called, so all the code inside the loop will be executed.
  5. When the loop function exists, go back to step 4.

The code inside the void setup will be executed once, and only once, at the beginning of the program.

Then, the code inside the void loop will be executed again and again (hence the name “loop”), until you:

  • Power off the Arduino board.
  • or Restart the Arduino program – by pressing the reset button / uploading a new sketch / re-opening the Serial Monitor on some Arduino boards.

After you start or restart the program, all the data – variable states and execution step – from the previous run will be lost. If you want to keep some data between different program runs, one easy way is to use the EEPROM memory if available.

Code Example

Let’s write a code example to see how the Arduino void setup and void loop work in detail.

int counter;

void setup() {
  Serial.begin(9600);
  counter = 10;

  Serial.print("Counter: ");
  Serial.println(counter);
}

void loop() {
  counter++;
  Serial.print("Counter: ");
  Serial.println(counter);
  delay(1000);
}

And here’s the result when you compile the code, upload it to your Arduino board, and open the Serial Monitor.

Counter: 10
Counter: 11
Counter: 12
Counter: 13
Counter: 14
Counter: 15
Counter: 16

Great, now let’s analyze this by breaking down the code line by line.

Init and void setup

int counter;

First we create a global variable, so we can modify it inside a function and still be able to get its value in another function.

Note: even if you declare a variable without initializing it in Arduino, it will automatically get the value “0” (more info about the Arduino language). So, at this point of the program, the counter variable contains the value “0”.

void setup() {
  Serial.begin(9600);

This is the beginning of the void setup function. The execution will start with Serial.begin(9600);.

  counter = 10;

  Serial.print("Counter: ");
  Serial.println(counter);
}

Still in the setup function, we set a new value (10) for the counter variable. And just after that we print the value in the Serial Monitor. In the output you get, this is where the line “Counter: 10” comes from.

And as you can notice, in line 9 we close the curly brackets for the void setup function. So, what’s happening now?

void loop

void loop() {
  counter++;
  Serial.print("Counter: ");
  Serial.println(counter);
  delay(1000);
}

The void loop function is now called and every instruction you’ve provided here will be executed.

In this case, we increase the counter by 1 (so the first time we enter the loop, the counter goes from 10 to 11). Just after that, we print the value for the counter – this will be “Counter: 11”.

And finally, we add a 1 second pause to the program, so the output on the Serial Monitor will not be displayed to fast for our human eyes.

What happens when the void loop function exits?

Well, just after, it is called again. So, it means the line that comes after line 15 is line 12. The instruction just after delay(1000); is counter++;.

As the counter variable is a global variable, its value is not lost when we go out and enter the loop function again. So, now we increment it, it goes from 11 to 12.

And the cycle continues. Every time we enter the loop, we add 1 to the counter, print the value, and wait for 1 second.

Now, if you power off the Arduino, the program will stop. If you restart the program, any progress for the variables will be lost, and the entire program will start from the beginning.

Writing Arduino programs – best practices for void loop and void setup

Great, now that you understand how things work, I’m going to give you a few best practices you can implement right now in your Arduino programs, regarding those void setup and void loop functions.

Arduino void setup

As the void setup function is called only once at the very beginning of the program, this will be the place to:

  • Initialize variables’ values.
  • Setup communications (ex: Serial).
  • Setup modes for digital pins (input/output).
  • Initialize any hardware component (sensor/actuator) plugged to the Arduino.
  • Etc.

The void setup, as its name suggest, is made for you to do any setup required at the beginning of the program. Don’t write the core functionalities here, just the initialization code.

Depending on the complexity of your program, you may have a lot of instructions to write in that void function. You can create new functions that you call from the void setup, no problem with that.

Also, in general it’s better to avoid using delay(), but there’s no problem in the void setup: if a component needs 2 seconds to be initialized after being powered on, then wait 2 seconds!

Arduino void loop

Now, in the void loop you’ll write your main program, knowing that the initialization is already done. In this function, always keep in mind that the last line is followed by the first line!

Also, any variable you’ve declared inside the void loop will be lost when the program exits and enters the function again. So, if you want to be able to keep data between 2 void loop, make sure to declare variables in a more global scope.

As for void setup, there’s no need to write all the code directly in the function. You can create as many other functions as you want (and classes too), and call those functions in the void loop. Ideally, the void loop should contain just a few lines calling other functions. Don’t bloat your void loop, just as you don’t bloat your “main” in a standard C/C++ program.

Finally, pay attention to delay(). In the void loop (and any function called from the void loop) you don’t want to block the execution of the code too long, especially if you plan to do some kind of multi-threading with your Arduino.

Conclusion

Now you should have a better idea of the concept behind Arduino void setup and loop functions.

For now, focus on creating programs that work, and try to follow the best practices from this guide. As you progress with Arduino, you’ll get more understanding from your own experience. This experience will help you decide what you can/can’t do in your void setup and void loop functions – because in the end, every program is different.

Did you find this tutorial useful?

Do you want to learn Arduino from scratch?

If yes, this course is for you:

Arduino For Beginners - Complete Course

>> Arduino Programming For Beginners <<