The void setup is a function created at the beginning of the program, between the initialization of the variable and the void loop. The void setup contains the initialization of the components such as an input or an output of the Arduino card, an initialization of the serial monitor. We only initialize the components that we want to control.
The void setup is a function which will execute only one time at the beginning of the program.
void setup(){
// your code
}
The void setup function is obligatory in every Arduino programs, even if there isn’t anything inside. Forget to write it will create an error.
In this function you can initialize a value to a variable, assign a component to a pin and import a library or the serial monitor.
Here is an example of what we can write in the void setup:
void setup(){
Serial.begin(9600); // Initialization of the serial monitor at 9600 bit/s or bauds.
Serial.println("Decoding"); // To write a message in the serial monitor at the beginning of the program and juste one time.
pinMode(2, OUTPUT); // Initializing the pin 2 as an output
pinMode(5, INPUT) // Initializing the pin 2 as an input
pinMode(LED, OUTPUT) // Initializing the led as an output
}
As we can see in this example there are many instructions:
We will ask us in this part what is the advantage of the void setup compared to the void loop?
The most important advantage of the void setup is that it’s only executed one time at the beginning. That way you can initialize variable and the serial monitor.
Here is some ideas of what you can put inside: