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.
Jump into concepts and practice - no test required
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
}
void loop() {
digitalWrite(LED_BUILTIN, HIGH);
delay(1000);
digitalWrite(LED_BUILTIN, LOW);
delay(1000);
}| Step | Function Called | Action | State Change | Output/Effect |
|---|---|---|---|---|
| 1 | setup() | pinMode(LED_BUILTIN, OUTPUT) | LED pin set as output | No visible output |
| 2 | loop() | digitalWrite(LED_BUILTIN, HIGH) | LED turned ON | LED lights up |
| 3 | loop() | delay(1000) | Wait 1000 ms | LED stays ON for 1 second |
| 4 | loop() | digitalWrite(LED_BUILTIN, LOW) | LED turned OFF | LED turns off |
| 5 | loop() | delay(1000) | Wait 1000 ms | LED stays OFF for 1 second |
| 6 | loop() | Repeat steps 2-5 | LED blinks repeatedly | LED blinks on and off every second |
| Variable | Start | After setup() | After loop() step 2 | After loop() step 4 | After loop() step 6 (repeat) |
|---|---|---|---|---|---|
| LED_BUILTIN mode | undefined | OUTPUT | OUTPUT | OUTPUT | OUTPUT |
| LED state | undefined | undefined | HIGH (ON) | LOW (OFF) | Repeats HIGH and LOW |
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.
setup() function in an Arduino program?setup()setup() function runs only once when the Arduino starts. It is used to prepare things like pin modes or initial settings.loop() runs repeatedly, so To run code repeatedly forever is incorrect. Options A and D describe actions not done by setup().setup() runs once = C [OK]loop() function in Arduino?loop() are declared with return type void and empty parentheses: void loop() {}.int return type. void loop(void) {} is valid C++ but less common in Arduino examples. loop() void {} has incorrect order.void setup() {
Serial.begin(9600);
Serial.println("Start");
}
void loop() {
Serial.println("Looping");
delay(1000);
}Serial.begin(9600) starts serial communication. Serial.println("Start") prints "Start" once at the beginning.loop() prints "Looping" every 1000 milliseconds (1 second) repeatedly.setup() once, loop() repeats = B [OK]void setup() {
pinMode(13, OUTPUT);
}
void loop() {
digitalWrite(13, HIGH);
delay(1000);
digitalWrite(13, LOW);
delay(1000)
}loop(), the line delay(1000) is missing a semicolon at the end.setup(). digitalWrite works with pin 13. delay() is allowed in loop().setup() and loop() to do this?setup() which runs once.loop() empty to stop further blinkingloop() empty prevents repeated blinking after the initial 5 times.setup() to blink 5 times; leave loop() empty -> Option A