Bird
Raised Fist0
Arduinoprogramming~10 mins

setup() and loop() execution model in Arduino - Step-by-Step Execution

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Concept Flow - setup() and loop() execution model
Start Program
Call setup() once
Enter loop()
Execute loop() body
Repeat loop() forever
The Arduino program starts by running setup() once, then repeatedly runs loop() forever.
Execution Sample
Arduino
void setup() {
  pinMode(LED_BUILTIN, OUTPUT);
}

void loop() {
  digitalWrite(LED_BUILTIN, HIGH);
  delay(1000);
  digitalWrite(LED_BUILTIN, LOW);
  delay(1000);
}
This code sets the built-in LED pin as output once, then turns the LED on and off every second repeatedly.
Execution Table
StepFunction CalledActionState ChangeOutput/Effect
1setup()pinMode(LED_BUILTIN, OUTPUT)LED pin set as outputNo visible output
2loop()digitalWrite(LED_BUILTIN, HIGH)LED turned ONLED lights up
3loop()delay(1000)Wait 1000 msLED stays ON for 1 second
4loop()digitalWrite(LED_BUILTIN, LOW)LED turned OFFLED turns off
5loop()delay(1000)Wait 1000 msLED stays OFF for 1 second
6loop()Repeat steps 2-5LED blinks repeatedlyLED blinks on and off every second
💡 loop() never exits; it repeats forever after setup() runs once
Variable Tracker
VariableStartAfter setup()After loop() step 2After loop() step 4After loop() step 6 (repeat)
LED_BUILTIN modeundefinedOUTPUTOUTPUTOUTPUTOUTPUT
LED stateundefinedundefinedHIGH (ON)LOW (OFF)Repeats HIGH and LOW
Key Moments - 3 Insights
Why does setup() run only once but loop() runs forever?
setup() is designed to initialize hardware and runs once at program start (see execution_table step 1). loop() contains the main code and repeats endlessly (see steps 2-6).
What happens if loop() finishes its code?
loop() never actually finishes; after the last statement, Arduino calls loop() again automatically (see execution_table step 6).
Why do we use delay() inside loop()?
delay() pauses the program for a set time, controlling how long the LED stays ON or OFF (see steps 3 and 5). Without delay, LED would blink too fast to see.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the state of the LED after step 4?
ALED is ON
BLED is blinking
CLED is OFF
DLED state is undefined
💡 Hint
Check the 'State Change' column at step 4 in execution_table
At which step does the program wait for 1 second with the LED ON?
AStep 3
BStep 4
CStep 2
DStep 5
💡 Hint
Look for delay(1000) after turning LED ON in execution_table
If we remove delay() calls, how would the LED behave according to the execution_table?
ALED would stay OFF permanently
BLED would blink too fast to see
CLED would stay ON permanently
DLED would not change state
💡 Hint
Consider the role of delay() in timing the LED blinking in execution_table steps 3 and 5
Concept Snapshot
Arduino programs have two main functions:
setup() runs once at start to initialize hardware.
loop() runs repeatedly forever to keep the program running.
Use delay() inside loop() to control timing.
setup() prepares; loop() repeats the main actions.
Full Transcript
The Arduino program starts by running the setup() function once. This function is used to prepare the hardware, like setting pin modes. After setup() finishes, the program enters the loop() function. The loop() runs its code repeatedly forever. In the example, setup() sets the built-in LED pin as output. Then loop() turns the LED on, waits one second, turns it off, waits one second, and repeats this cycle forever. The delay() function pauses the program to make the LED blink visible. The key idea is setup() runs once, loop() runs forever, and delay() controls timing inside loop().

Practice

(1/5)
1. What is the main purpose of the setup() function in an Arduino program?
easy
A. To stop the Arduino program
B. To run code repeatedly forever
C. To reset the Arduino board
D. To run code once at the start to prepare the Arduino

Solution

  1. Step 1: Understand the role of setup()

    The setup() function runs only once when the Arduino starts. It is used to prepare things like pin modes or initial settings.
  2. Step 2: Compare with other options

    loop() runs repeatedly, so To run code repeatedly forever is incorrect. Options A and D describe actions not done by setup().
  3. Final Answer:

    To run code once at the start to prepare the Arduino -> Option D
  4. Quick Check:

    setup() runs once = C [OK]
Hint: Remember: setup() runs once, loop() runs forever [OK]
Common Mistakes:
  • Thinking setup() runs repeatedly
  • Confusing setup() with loop()
  • Believing setup() resets the board
2. Which of the following is the correct way to declare the loop() function in Arduino?
easy
A. int loop() {}
B. void loop() {}
C. void loop(void) {}
D. loop() void {}

Solution

  1. Step 1: Recall Arduino function syntax

    Arduino functions like loop() are declared with return type void and empty parentheses: void loop() {}.
  2. Step 2: Check each option

    void loop() {} matches correct syntax. int loop() {} wrongly uses int return type. void loop(void) {} is valid C++ but less common in Arduino examples. loop() void {} has incorrect order.
  3. Final Answer:

    void loop() {} -> Option B
  4. Quick Check:

    Standard Arduino loop syntax = A [OK]
Hint: Use 'void loop()' exactly as Arduino expects [OK]
Common Mistakes:
  • Using wrong return type like int
  • Swapping order of function name and return type
  • Adding parameters inside loop()
3. What will be the output on the Serial Monitor when running this Arduino code?
void setup() {
  Serial.begin(9600);
  Serial.println("Start");
}

void loop() {
  Serial.println("Looping");
  delay(1000);
}
medium
A. Start and Looping printed once each
B. Only Looping printed repeatedly
C. Start printed once, then Looping printed every second
D. No output because Serial.begin() is missing

Solution

  1. Step 1: Analyze setup() output

    Serial.begin(9600) starts serial communication. Serial.println("Start") prints "Start" once at the beginning.
  2. Step 2: Analyze loop() output

    loop() prints "Looping" every 1000 milliseconds (1 second) repeatedly.
  3. Final Answer:

    Start printed once, then Looping printed every second -> Option C
  4. Quick Check:

    setup() once, loop() repeats = B [OK]
Hint: setup() prints once, loop() repeats output [OK]
Common Mistakes:
  • Thinking loop() runs once
  • Missing Serial.begin() call
  • Ignoring delay causing fast prints
4. Identify the error in this Arduino code:
void setup() {
  pinMode(13, OUTPUT);
}

void loop() {
  digitalWrite(13, HIGH);
  delay(1000);
  digitalWrite(13, LOW);
  delay(1000)
}
medium
A. Missing semicolon after delay(1000) in loop()
B. pinMode should be in loop(), not setup()
C. digitalWrite cannot be used with pin 13
D. delay() cannot be used inside loop()

Solution

  1. Step 1: Check syntax line by line

    In loop(), the line delay(1000) is missing a semicolon at the end.
  2. Step 2: Verify other parts

    pinMode is correctly placed in setup(). digitalWrite works with pin 13. delay() is allowed in loop().
  3. Final Answer:

    Missing semicolon after delay(1000) in loop() -> Option A
  4. Quick Check:

    Syntax error = missing semicolon [OK]
Hint: Check every line ends with a semicolon [OK]
Common Mistakes:
  • Placing pinMode in loop() unnecessarily
  • Assuming digitalWrite can't use pin 13
  • Thinking delay() is disallowed in loop()
5. You want to blink an LED connected to pin 9 exactly 5 times when the Arduino starts, then stop blinking. Which code correctly uses setup() and loop() to do this?
hard
A. Use a counter in setup() to blink 5 times; leave loop() empty
B. Blink 5 times inside loop() without a counter
C. Blink 5 times inside loop() using a counter, then stop blinking
D. Blink 5 times inside setup(), then keep blinking in loop()

Solution

  1. Step 1: Understand blinking 5 times only once

    Since blinking 5 times is a one-time task, it should be done in setup() which runs once.
  2. Step 2: Keep loop() empty to stop further blinking

    Leaving loop() empty prevents repeated blinking after the initial 5 times.
  3. Final Answer:

    Use a counter in setup() to blink 5 times; leave loop() empty -> Option A
  4. Quick Check:

    One-time task in setup() = A [OK]
Hint: One-time actions go in setup(), repeated in loop() [OK]
Common Mistakes:
  • Blinking in loop() without stopping
  • Not using a counter to limit blinks
  • Putting one-time code in loop() causing repeats