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
Understanding Arduino setup() and loop() Execution Model
📖 Scenario: You are creating a simple Arduino program to control an LED light. The LED should turn on once when the Arduino starts and then blink repeatedly.
🎯 Goal: Build an Arduino sketch that uses the setup() function to turn on an LED once, and the loop() function to blink the LED on and off repeatedly.
📋 What You'll Learn
Create a variable called ledPin and set it to 13
Use pinMode(ledPin, OUTPUT); inside setup()
Turn the LED on once inside setup() using digitalWrite(ledPin, HIGH);
In loop(), blink the LED by turning it off and on with a delay of 500 milliseconds
Use digitalWrite(ledPin, LOW);, delay(500);, digitalWrite(ledPin, HIGH);, and delay(500); inside loop()
💡 Why This Matters
🌍 Real World
Controlling lights, motors, and sensors in simple electronics projects.
💼 Career
Understanding Arduino's execution model is essential for embedded systems and hardware programming jobs.
Progress0 / 4 steps
1
Set up the LED pin variable
Create an integer variable called ledPin and set it to 13.
Arduino
Hint
Pin 13 is usually connected to the built-in LED on many Arduino boards.
2
Initialize the LED pin in setup()
Write the setup() function. Inside it, use pinMode(ledPin, OUTPUT); to set the LED pin as an output. Then turn the LED on once using digitalWrite(ledPin, HIGH);.
Arduino
Hint
The setup() function runs once when the Arduino starts.
3
Write the loop() function to blink the LED
Write the loop() function. Inside it, turn the LED off with digitalWrite(ledPin, LOW);, wait 500 milliseconds with delay(500);, then turn the LED on with digitalWrite(ledPin, HIGH);, and wait another 500 milliseconds with delay(500);.
Arduino
Hint
The loop() function runs over and over again after setup() finishes.
4
Test the program by printing a message
Add a Serial.begin(9600); line inside setup() to start serial communication. Then add Serial.println("LED is blinking"); inside loop() to print the message each time the LED blinks.
Arduino
Hint
Open the Serial Monitor in the Arduino IDE to see the printed messages.
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
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.
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().
Final Answer:
To run code once at the start to prepare the Arduino -> Option D
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
Step 1: Recall Arduino function syntax
Arduino functions like loop() are declared with return type void and empty parentheses: void loop() {}.
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.
Final Answer:
void loop() {} -> Option B
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?
In loop(), the line delay(1000) is missing a semicolon at the end.
Step 2: Verify other parts
pinMode is correctly placed in setup(). digitalWrite works with pin 13. delay() is allowed in loop().
Final Answer:
Missing semicolon after delay(1000) in loop() -> Option A
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
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.
Step 2: Keep loop() empty to stop further blinking
Leaving loop() empty prevents repeated blinking after the initial 5 times.
Final Answer:
Use a counter in setup() to blink 5 times; leave loop() empty -> Option A
Quick Check:
One-time task in setup() = A [OK]
Hint: One-time actions go in setup(), repeated in loop() [OK]