Sub topic 2
Anatomy of a Sketch: `setup()` and `loop()`
This chapter assumes you know what a program and a variable are, and that you have an Arduino-compatible board, a USB cable, and the Arduino IDE installed. You should also know that a digital pin can have one of two main logic states: HIGH or LOW.
An Arduino sketch is a program written for a microcontroller board. The sketch usually contains two special functions:
setup()prepares the board.loop()contains the repeated work.
A function is a named block of instructions. The Arduino framework calls these two functions for you. You do not normally call setup() or loop() yourself.
The program's two phases
When the board starts or resets, it runs setup() once. This is where you configure pins, start serial communication, and set initial values.
After setup() finishes, the framework calls loop() repeatedly. When the last instruction in loop() finishes, the framework starts loop() again from its first instruction.
void setup() {
// Instructions here run once after startup.
}
void loop() {
// Instructions here run repeatedly.
}The comments begin with //. The compiler ignores everything from // to the end of that line. Comments explain the code to people and do not affect the board.
The braces, { and }, mark the beginning and end of each function. Every opening brace needs a matching closing brace. A semicolon ends most individual instructions.
A complete sketch
The built-in LED is connected to a board-defined pin represented by LED_BUILTIN. This name makes the sketch more portable because different Arduino-compatible boards may use different pin numbers for their built-in LEDs.
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
}
void loop() {
digitalWrite(LED_BUILTIN, HIGH);
delay(1000);
digitalWrite(LED_BUILTIN, LOW);
delay(1000);
}The pinMode() function configures a pin. Here, OUTPUT means the microcontroller will drive the pin's electrical level.
The digitalWrite() function sets an output pin to a digital state. HIGH turns the output on, and LOW turns it off. On a typical Arduino board, HIGH is close to the board's supply voltage and LOW is close to 0 volts.
The delay() function pauses the program for a number of milliseconds. A millisecond is one thousandth of a second, so delay(1000) pauses for one second.
The LED turns on and off because loop() repeats these four instructions:
- Set the pin
HIGH. - Wait one second.
- Set the pin
LOW. - Wait one second.
Why configuration belongs in `setup()`
pinMode() needs to run before the sketch uses the pin as an output. Placing it in setup() ensures that configuration happens after every reset and happens before the repeated work begins.
You could place pinMode() inside loop(), but that is poor structure. It would repeat an operation that only needs to happen once. Keeping initialization in setup() makes the program easier to read and reduces unnecessary work.
Variables that must retain a value between loop iterations are often declared outside both functions. Such a variable has global scope, which means code in both functions can access it.
const uint8_t LED_PIN = LED_BUILTIN;
unsigned long blinkCount = 0;
void setup() {
pinMode(LED_PIN, OUTPUT);
}
void loop() {
digitalWrite(LED_PIN, HIGH);
delay(500);
digitalWrite(LED_PIN, LOW);
delay(500);
blinkCount++;
}const marks a value that the sketch should not change. uint8_t is an unsigned integer type that can store a small non-negative number. The ++ operator adds one to blinkCount.
How execution flows
loop() is not a normal function that returns control to your code. The Arduino framework calls it again after it finishes. This repeated structure is the main working model for simple Arduino programs.
Common structure errors
The Arduino IDE reports compilation errors when the sketch cannot be translated into machine code. Common causes include:
- Omitting a semicolon after a function call.
- Forgetting a closing brace.
- Misspelling a function such as
digitalWrite(). - Writing
HIGHorLOWwith the wrong capitalization. - Defining two functions with the same name.
- Using a variable before declaring it.
C++ is case-sensitive. setup, Setup, and SETUP are different names. The Arduino framework recognizes only the exact names setup() and loop().
Exercise
If you move pinMode(LED_BUILTIN, OUTPUT); from setup() into loop(), will the LED necessarily stop blinking? What is the better location, and why?
Show answer
It may still blink because pinMode() will be called repeatedly before each pair of writes. The better location is setup(), because the pin needs configuring only once and setup() clearly separates initialization from repeated behavior.