100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Programming

Arduino Sketch Structure

Understand the anatomy of an Arduino sketch: the mandatory setup() and loop() functions, global declarations, comments, and how the program executes from power-on onward.

FoundationsBeginner9 min readJul 10, 2026
Analogies

The Two Mandatory Functions

Every Arduino sketch is built on two required functions: setup() and loop(). The board's firmware calls setup() exactly once immediately after power-on or reset, then calls loop() repeatedly and endlessly for as long as the board has power. You put one-time initialization in setup() — configuring pin modes, starting Serial communication, initializing sensors — and put your continuously running behavior in loop(). Even an empty sketch must define both functions, because the underlying main() provided by the Arduino core expects to call them.

🏏

Cricket analogy: setup() and loop() are like the coin toss and the overs: the toss happens once to decide sides (setup), then over after over is bowled repeatedly until the innings ends (loop).

cpp
// Global scope: declarations visible everywhere
const int ledPin = 13;
int counter = 0;

void setup() {
  pinMode(ledPin, OUTPUT);   // runs once
  Serial.begin(9600);        // start serial at 9600 baud
}

void loop() {
  counter++;                 // runs forever
  Serial.println(counter);
  digitalWrite(ledPin, HIGH);
  delay(200);
  digitalWrite(ledPin, LOW);
  delay(200);
}

Global Declarations and Scope

Code written above setup() lives in global scope and is visible to every function in the sketch. This is where you declare pin-number constants, library objects, and variables that must persist and be shared across both setup() and loop(). A variable declared inside loop() is local — it is created fresh on each pass and destroyed at the end of that pass, so it cannot remember a value between iterations. If you need state to survive from one loop() call to the next, declare it globally or mark it static. Using const for fixed values, like a pin number, prevents accidental modification and documents intent.

🏏

Cricket analogy: Global variables are like the scoreboard visible to the whole ground all match long, while a local variable is like a single ball's outcome — noted, then gone once the next ball is bowled.

To keep a value between loop() iterations without polluting the global namespace, declare it static inside the function: 'static int calls = 0;'. It is initialized only once and retains its value across every subsequent call, giving you persistence with local scope.

Comments and Readability

Comments are ignored by the compiler but essential for humans. Arduino uses C++ comment syntax: two slashes // begin a single-line comment that runs to the end of the line, while /* ... */ wraps a block comment that can span many lines. Good sketches comment the why behind wiring choices, magic numbers, and timing values rather than restating the obvious. Because sketches often map directly to physical pins and hardware, a one-line note like '// pin 9 drives the servo signal wire' saves enormous debugging time when you revisit the circuit weeks later.

🏏

Cricket analogy: Comments are like a scorer's margin notes explaining why a bowling change was made — invisible to the game itself but priceless when you review the match later.

Execution Flow and Blocking

Because loop() runs continuously, the speed and structure of the code inside it directly shape responsiveness. A call to delay(1000) blocks the entire processor for one second — nothing else can run, no buttons are checked, no serial is read — because Arduino is single-threaded with no operating system to switch tasks. For simple sketches this is fine, but responsive projects avoid long delay() calls and instead compare millis(), the count of milliseconds since power-on, to schedule actions without freezing. Understanding that loop() is a tight, uninterrupted cycle is the key to reasoning about Arduino timing.

🏏

Cricket analogy: delay() blocking the processor is like a batter freezing mid-crease staring at the sky — the whole game stalls, no running between wickets, until they snap out of it.

Do not put an infinite while(true) loop or an unbounded blocking wait inside setup() or loop() unless intended — the board cannot process new uploads while such code runs, sometimes forcing you to reset the board or use the double-tap bootloader trick to recover.

  • Every sketch must define setup() and loop(); setup() runs once, loop() runs forever.
  • Put one-time initialization (pinMode, Serial.begin) in setup() and continuous behavior in loop().
  • Global declarations above setup() are shared everywhere; local variables inside loop() reset each pass.
  • Use static inside a function to persist a value across loop() calls without going global.
  • // starts a single-line comment; /* */ wraps a block comment — both are ignored by the compiler.
  • delay() blocks the single-threaded processor; use millis() for non-blocking timing in responsive sketches.

Practice what you learned

Was this page helpful?

Topics covered

#Programming#ArduinoProgrammingStudyNotes#ArduinoSketchStructure#Arduino#Sketch#Structure#Two#StudyNotes#SkillVeris#ExamPrep