Sub topic 8
How a Sketch Is Structured: `setup()` and `loop()`
Prerequisites and the basic model
Assume that the Arduino IDE is installed, your board is connected, and you can select the correct board and port. You should also know that a program is a list of instructions.
An Arduino program is called a sketch. A sketch normally has two important functions:
setup()runs once when the board starts or resets.loop()runs repeatedly for as long as the board has power.
A function is a named group of instructions. The parentheses after a function name hold parameters, if the function needs them. The braces contain the instructions that belong to the function.
void setup() {
// Instructions here run once.
}
void loop() {
// Instructions here run repeatedly.
}The word void means that the function does not return a value. Arduino’s program framework calls these two functions for you. You do not need to write a separate main() function.
What belongs in `setup()`
Use setup() for one-time preparation. Typical tasks include:
- Configuring pins as inputs or outputs.
- Starting serial communication.
- Initializing a sensor, display, or library.
- Setting an initial value.
For example, the built-in LED must be configured as an output before the sketch tries to control it:
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
}pinMode() configures a digital pin. LED_BUILTIN is a board-specific constant that identifies the built-in LED. OUTPUT tells the board that the sketch will send a voltage signal through that pin.
After setup() finishes, Arduino calls loop().
What belongs in `loop()`
Put repeated behavior in loop(). The following sketch turns the built-in LED on, waits for one second, turns it off, and waits again:
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
}
void loop() {
digitalWrite(LED_BUILTIN, HIGH);
delay(1000);
digitalWrite(LED_BUILTIN, LOW);
delay(1000);
}digitalWrite() sets a digital output to HIGH or LOW. HIGH usually means the pin outputs its supply voltage. LOW means the pin outputs approximately zero volts.
delay(1000) pauses the sketch for 1,000 milliseconds, or one second. During this pause, the processor does not run the next instruction in your sketch. This makes delay() useful for simple demonstrations, but more advanced projects often use a timer based on millis() so other tasks can continue.
The execution cycle
The board follows this cycle:
A reset starts the cycle again. Uploading a sketch usually resets the board after the upload completes, so setup() runs again and any variables return to their initial values.
Common structure errors
The Arduino IDE reports an error if the sketch has mismatched braces, missing semicolons, or misspelled function names. Keep these details in mind:
- Every opening brace
{needs a matching closing brace}. - Most instructions end with a semicolon.
- Arduino function names are case-sensitive.
setup()andSetup()are different names. - Put repeated actions in
loop(), not insetup(). - Do not place code outside a function unless it is a declaration or definition supported by the language.
A comment begins with //. The compiler ignores the rest of that line, so comments can explain the purpose of each section without changing the sketch.
Exercise
If you move digitalWrite(LED_BUILTIN, HIGH); from loop() into setup(), what happens?
Show answer
The LED is set to HIGH once when the board starts. It will not be repeatedly turned on by the sketch.