Setup & Loop

! Introduction

We shall start with the basics. This first tutorial shows the first concepts to program an Arduino.

There are thousands of different software programs written specifically for Arduino. However, all of them follow the same structure with two main sections called Setup and Loop.

! Anatomy of a Program for Arduino

When you open the Arduino program, the following screen with show up. In it we can notice the Setup and Loop sections: IDE

Program for Arduino

! Setup

The Setup is a mandatory section of an Arduino program. The Setup function declaration is made on the following way:

 
void setup()
{
// Lines of setup code
}

Any code that is inside the setup (), that is, between curly braces ({ }) is executed only once in the beginning of your program.

This function is useful to configure the Arduino. It is here, for example, that you put the initial configurations, such as if a LED starts off or on, which the input and output pins are, among other things. You'll understand these concepts in the next tutorials.

You can further explore the setup() function at the Arduino page.

! Loop

Like the Setup section, the Loop is also mandatory in an Arduino program. Its declaration is made the following way:

 
void loop()
{
// Lines of loop code
}

The majority of your code will be executed inside this section. After the execution of setup(), the loop() is initiated.

The program starts right after the opening curly brace ({), and the processor executes the lines of code until it reaches the closing curly brace (}). Once at the end, it jumps back to the first line of the loop and starts over.

The loop() function will be executed forever, or until you upload a new code, restarting the process. It can be also restarted by resetting the Arduino (through the reset button, for instance).

You can also explore more about the loop() function at the Arduino page.