The setup() and loop() functions control how an Arduino program starts and runs repeatedly. This helps the board do tasks over and over without stopping.
setup() and loop() execution model in Arduino
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
Arduino
void setup() {
// code here runs once
}
void loop() {
// code here runs repeatedly
}setup() runs only once when the Arduino starts or resets.
loop() runs over and over forever after setup() finishes.
Examples
Arduino
void setup() {
pinMode(13, OUTPUT); // set pin 13 as output
}
void loop() {
digitalWrite(13, HIGH); // turn LED on
delay(1000); // wait 1 second
digitalWrite(13, LOW); // turn LED off
delay(1000); // wait 1 second
}Arduino
void setup() {
Serial.begin(9600); // start serial communication
}
void loop() {
Serial.println("Hello Arduino!"); // print message
delay(2000); // wait 2 seconds
}Sample Program
This program sets up pin 13 as an output and starts serial communication. It prints "Setup done" once. Then it turns the LED on and off every half second, printing the LED status each time.
Arduino
void setup() {
pinMode(13, OUTPUT); // initialize pin 13 as output
Serial.begin(9600); // start serial communication
Serial.println("Setup done");
}
void loop() {
digitalWrite(13, HIGH); // turn LED on
Serial.println("LED ON");
delay(500); // wait half a second
digitalWrite(13, LOW); // turn LED off
Serial.println("LED OFF");
delay(500); // wait half a second
}Important Notes
Do not put long delays in setup() because it delays starting the repeated loop().
Use loop() to keep your program running tasks continuously.
Arduino automatically calls setup() once and then calls loop() repeatedly.
Summary
setup() runs once at the start to prepare your Arduino.
loop() runs forever to keep your program working.
This model helps Arduino do repeated tasks easily.
Practice
1. What is the main purpose of the
setup() function in an Arduino program?easy
Solution
Step 1: Understand the role of
Thesetup()setup()function runs only once when the Arduino starts. It is used to prepare things like pin modes or initial settings.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 bysetup().Final Answer:
To run code once at the start to prepare the Arduino -> Option DQuick 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
Solution
Step 1: Recall Arduino function syntax
Arduino functions likeloop()are declared with return typevoidand empty parentheses:void loop() {}.Step 2: Check each option
void loop() {} matches correct syntax. int loop() {} wrongly usesintreturn type. void loop(void) {} is valid C++ but less common in Arduino examples. loop() void {} has incorrect order.Final Answer:
void loop() {} -> Option BQuick 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
Solution
Step 1: Analyze setup() output
Serial.begin(9600)starts serial communication.Serial.println("Start")prints "Start" once at the beginning.Step 2: Analyze loop() output
loop()prints "Looping" every 1000 milliseconds (1 second) repeatedly.Final Answer:
Start printed once, then Looping printed every second -> Option CQuick 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
Solution
Step 1: Check syntax line by line
Inloop(), the linedelay(1000)is missing a semicolon at the end.Step 2: Verify other parts
pinMode is correctly placed insetup(). digitalWrite works with pin 13. delay() is allowed in loop().Final Answer:
Missing semicolon after delay(1000) in loop() -> Option AQuick 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
Solution
Step 1: Understand blinking 5 times only once
Since blinking 5 times is a one-time task, it should be done insetup()which runs once.Step 2: Keep
Leavingloop()empty to stop further blinkingloop()empty prevents repeated blinking after the initial 5 times.Final Answer:
Use a counter insetup()to blink 5 times; leaveloop()empty -> Option AQuick 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
