ABC of Electronics cosycom.com
ARDUINO 03 · Getting Started

Understanding a Sketch: setup() and loop()

Every Arduino program follows the exact same two-function skeleton — learn that shape once and every sketch becomes readable.

An Arduino program is called a sketch, and no matter how simple or complex, every one of them is built from the same two required functions:

void setup() {
  // runs once, when the board powers on or resets
}

void loop() {
  // runs over and over, forever, after setup() finishes
}

setup() runs exactly once, right after the board powers up or is reset — it's where you configure things that only need to happen a single time: setting pin modes (next lesson), starting serial communication, initializing a sensor.

loop() then runs over and over, indefinitely, for as long as the board has power — this is where your program's actual ongoing behavior lives: checking a sensor, updating an output, reacting to a button press. When loop() reaches its end, the board simply starts it again from the top, immediately.

A few other things you'll see constantly inside both functions:

  • Every statement ends with a semicolon ; — a missing one is the most common beginner syntax error.
  • Curly braces {{ }} mark the start and end of each function's contents.
  • Lines starting with // are comments — notes for the programmer, ignored by the board entirely.

Nearly everything in the lessons ahead is really just "what code goes inside setup()" and "what code goes inside loop()" for a particular task.

setup() — once loop() — forever abcofelectronics
setup() then loop(), forever setup() runs once at power-on; loop() then repeats endlessly for as long as the board stays powered.
TRY THIS
Anything that should happen once (configuration) goes in setup(); anything that should happen repeatedly (the program's actual behavior) goes in loop().