Bird
Raised Fist0
Arduinoprogramming~10 mins

Arduino IDE and sketch structure - 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 - Arduino IDE and sketch structure
Open Arduino IDE
Write Sketch
Setup() runs once
Loop() runs repeatedly
Back to Loop()
Upload to Board
Board runs sketch
Back to Loop()
The Arduino sketch starts with setup() running once, then loop() runs repeatedly. You write code in the IDE, upload it, and the board runs it.
Execution Sample
Arduino
void setup() {
  pinMode(13, OUTPUT);
}

void loop() {
  digitalWrite(13, HIGH);
  delay(1000);
  digitalWrite(13, LOW);
  delay(1000);
}
This sketch turns the built-in LED on pin 13 on and off every second.
Execution Table
StepFunctionActionPin 13 StateDelay(ms)
1setup()Set pin 13 as OUTPUTN/AN/A
2loop()Turn LED ONHIGHN/A
3loop()Wait 1000 msHIGH1000
4loop()Turn LED OFFLOWN/A
5loop()Wait 1000 msLOW1000
6loop()Repeat loop()LOWN/A
💡 loop() runs forever; Arduino keeps blinking LED on pin 13 every second
Variable Tracker
VariableStartAfter setup()After loop step 2After loop step 3After loop step 4After loop step 5After loop step 6
Pin 13 ModeUNKNOWNOUTPUTOUTPUTOUTPUTOUTPUTOUTPUTOUTPUT
Pin 13 StateLOW (default)LOWHIGHHIGHLOWLOWLOW
Delay0001000100010001000
Key Moments - 3 Insights
Why does setup() run only once but loop() runs forever?
setup() is designed to initialize settings once before the main program runs. loop() contains the main code that repeats endlessly, as shown in execution_table rows 1 (setup) and 2-6 (loop).
What happens if you forget to set pinMode in setup()?
Without pinMode set to OUTPUT, digitalWrite may not work correctly. The LED might not turn on as expected. This is why execution_table step 1 sets pin 13 mode before using it.
Why do we use delay() in loop()?
delay() pauses the program so the LED stays on or off long enough to see. Without delay, the LED would switch too fast to notice, as shown in steps 3 and 5.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the state of pin 13 after step 4?
ALOW
BHIGH
CUNKNOWN
DOUTPUT
💡 Hint
Check the 'Pin 13 State' column at step 4 in execution_table.
At which step does the program first wait for 1000 milliseconds?
AStep 2
BStep 4
CStep 3
DStep 5
💡 Hint
Look at the 'Delay(ms)' column in execution_table to find when delay is first used.
If you remove the pinMode(13, OUTPUT) line, what likely happens?
ALED blinks faster
BLED stays off or behaves unpredictably
CLED stays on permanently
DProgram will not compile
💡 Hint
Refer to key_moments about pinMode importance and execution_table step 1.
Concept Snapshot
Arduino sketch has two main functions:
- setup(): runs once to initialize
- loop(): runs repeatedly
Use pinMode() in setup() to set pin mode
Use digitalWrite() and delay() in loop() to control outputs
Upload sketch via Arduino IDE to run on board
Full Transcript
In Arduino programming, you write code called a sketch using the Arduino IDE. The sketch has two main parts: setup() and loop(). setup() runs once to prepare things like pin modes. loop() runs over and over to do the main work, like blinking an LED. In the example, setup() sets pin 13 as an output. Then loop() turns the LED on, waits one second, turns it off, waits one second, and repeats forever. Variables like pin mode and pin state change as the program runs. Beginners often wonder why setup() runs once and loop() repeats, or why pinMode is needed. The delay() function pauses the program so changes are visible. This simple structure lets you control hardware easily with Arduino.

Practice

(1/5)
1. What is the main purpose of the setup() function in an Arduino sketch?
easy
A. To run code once at the start to initialize settings
B. To run code repeatedly in a loop
C. To declare variables globally
D. To stop the program from running

Solution

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

    The setup() function runs once when the Arduino starts to prepare the board.
  2. Step 2: Compare with loop()

    The loop() function runs repeatedly, but setup() runs only once.
  3. Final Answer:

    To run code once at the start to initialize settings -> Option A
  4. Quick Check:

    setup() runs once = B [OK]
Hint: Remember: setup runs once, loop runs forever [OK]
Common Mistakes:
  • Confusing setup() with loop()
  • Thinking setup() runs repeatedly
  • Believing setup() stops the program
2. Which of the following is the correct basic structure of an Arduino sketch?
easy
A. void setup() { } void loop() { }
B. void start() { } void repeat() { }
C. void main() { }
D. void initialize() { } void run() { }

Solution

  1. Step 1: Recall Arduino sketch structure

    The Arduino sketch must have setup() and loop() functions defined with void return type.
  2. Step 2: Check options for correct function names

    Only void setup() { } void loop() { } uses setup() and loop() correctly.
  3. Final Answer:

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

    Correct function names = C [OK]
Hint: Look for setup() and loop() function names [OK]
Common Mistakes:
  • Using wrong function names like main()
  • Missing either setup() or loop()
  • Using incorrect return types
3. What will be the output on the Serial Monitor when running this Arduino sketch?
void setup() {
  Serial.begin(9600);
  Serial.println("Start");
}

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

Solution

  1. Step 1: Analyze setup() output

    Serial.begin(9600) starts serial communication, then "Start" is printed once.
  2. Step 2: Analyze loop() output

    Inside loop(), "Looping" is printed every 1000 milliseconds (1 second) repeatedly.
  3. Final Answer:

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

    setup() once, loop() repeats = D [OK]
Hint: setup() prints once, loop() repeats output [OK]
Common Mistakes:
  • Thinking setup() runs repeatedly
  • Forgetting Serial.begin() is needed
  • Assuming no delay means no output
4. Identify the error in this Arduino sketch:
void setup() {
  pinMode(13, OUTPUT);
}

void loop() {
  digitalWrite(13, HIGH)
  delay(1000);
  digitalWrite(13, LOW);
  delay(1000);
}
medium
A. pinMode should be in loop()
B. Missing semicolon after digitalWrite(13, HIGH)
C. delay() cannot be used in loop()
D. digitalWrite() needs two arguments

Solution

  1. Step 1: Check syntax line by line

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

    pinMode is correctly in setup(), delay() is allowed in loop(), and digitalWrite() has correct arguments.
  3. Final Answer:

    Missing semicolon after digitalWrite(13, HIGH) -> Option B
  4. Quick Check:

    Syntax error: missing semicolon = A [OK]
Hint: Look for missing semicolons after statements [OK]
Common Mistakes:
  • Putting pinMode in loop() unnecessarily
  • Thinking delay() is not allowed in loop()
  • Miscounting digitalWrite() arguments
5. You want to blink an LED connected to pin 9 exactly 5 times, then stop. Which modification to the Arduino sketch structure is best?
hard
A. Remove loop() function entirely
B. Put blinking code inside setup() and leave loop() empty
C. Use a counter variable in loop() and stop blinking after 5 times
D. Use delay(5000) in setup() to blink 5 times

Solution

  1. Step 1: Understand blinking 5 times

    Since loop() runs forever, use a counter variable inside loop() to count blinks.
  2. Step 2: Evaluate options

    Putting blinking code in setup() runs once, so it performs only one blink cycle, not 5. Removing loop() is invalid. Using delay(5000) only delays, does not blink 5 times.
  3. Final Answer:

    Use a counter variable in loop() and stop blinking after 5 times -> Option C
  4. Quick Check:

    Counter in loop() controls blink count = A [OK]
Hint: Use a counter in loop() to limit repetitions [OK]
Common Mistakes:
  • Trying to remove loop() function
  • Putting repeated code only in setup()
  • Using delay() to count blinks incorrectly