Sub topic 1
How a Sketch Runs: `setup()` and `loop()`
Prerequisites
Assume you can open, verify, and upload a sketch with the Arduino IDE. You should also know what a board pin, voltage, LED, and resistor are. Connect an Arduino Uno to your computer over USB. For the examples, use the board's built-in LED or an external LED connected through a resistor.
An Arduino program is called a sketch. A sketch normally has two special functions:
setup()runs once after the board starts or resets.loop()runs repeatedly for as long as the board has power.
A function is a named block of code. The parentheses after the function name are required, even when the function has no inputs. Braces mark the beginning and end of the function body.
void setup() {
// One-time preparation goes here.
}
void loop() {
// Repeating work goes here.
}The Arduino framework calls these functions for you. You do not call setup() or loop() from your own code. Internally, the framework performs startup work, calls setup() once, and then keeps calling loop() in a continuous cycle.
The startup and repetition cycle
When the board receives power or resets, it initializes its hardware and runs setup(). This is where you configure pins, start serial communication, and set initial values.
After setup() finishes, the framework calls loop(). When the code reaches the closing brace of loop(), the framework immediately calls it again. A simple sketch therefore follows this pattern:
flowchart TD
A[Power on or reset] --> B[Run setup once]
B --> C[Run loop]
C --> D{Board still running?}
D -->|Yes| C
D -->|No| E[Stop]Configuring a pin in `setup()`
A digital pin can be configured as an input or an output. The pinMode() function selects the mode. The digitalWrite() function sets an output pin to a high or low voltage.
LED_BUILTIN is a board-specific constant that identifies the built-in LED pin. Using it makes the sketch easier to move between many Arduino boards.
const int ledPin = LED_BUILTIN;
void setup() {
pinMode(ledPin, OUTPUT);
}
void loop() {
digitalWrite(ledPin, HIGH);
delay(500);
digitalWrite(ledPin, LOW);
delay(500);
}The declaration beginning with const int gives the pin number a readable name. const means the value should not change while the program runs. OUTPUT, HIGH, and LOW are names supplied by the Arduino environment.
The setup() function configures the pin once. The loop() function changes the LED state and waits 500 milliseconds between changes. Because loop() repeats, the LED continues blinking.
What `delay()` does
delay(500) pauses the processor for 500 milliseconds. During this pause, the sketch does not execute the next statement. This is useful for a first blinking example, but it can make a larger program slow to respond to buttons, sensors, or communication.
Later, you can replace long delays with timing based on millis(). That approach lets loop() continue running while you check whether enough time has passed. The important structure remains the same: initialize in setup() and perform ongoing work in loop().
Common errors
- Putting an action after an infinite loop means that action may never run.
- Forgetting a closing brace causes a compiler error.
- Calling
pinMode()only after trying to control a pin can produce confusing hardware behavior. - Expecting
setup()to run again after every pass throughloop()leads to incorrect initialization logic. - A reset, including one caused by uploading a sketch, runs
setup()again.
Exercise
How many times does setup() run, and how many times does loop() run after a normal board reset?
Show answer
setup() runs once. loop() runs repeatedly until the board loses power or stops.