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).
// 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
1. How often does the Arduino core call setup() and loop()?
2. What happens to a variable declared inside loop() between iterations?
3. Why does delay(1000) make an Arduino unresponsive during that time?
4. Which keyword keeps a local variable's value across loop() calls without making it global?
5. What is the correct syntax for a single-line comment in an Arduino sketch?
Was this page helpful?
You May Also Like
What Is Arduino?
An introduction to Arduino as an open-source electronics platform combining a programmable microcontroller board with a simple software toolchain for building interactive projects.
Arduino Data Types and Variables
Learn the core Arduino/C++ data types, how much memory each uses on an 8-bit AVR, and how to declare variables and constants correctly to avoid overflow and wasted RAM.
Your First Blink Sketch
Build and understand the classic Blink program line by line, learn pinMode, digitalWrite, and delay, and see how to make blinking non-blocking with millis().
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics