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
Arduino IDE and Sketch Structure
📖 Scenario: You have a simple LED connected to your Arduino board. You want to learn how to write a basic Arduino sketch that turns the LED on and off repeatedly.
🎯 Goal: Build a basic Arduino sketch with the correct structure that blinks an LED connected to pin 13.
📋 What You'll Learn
Create a variable to store the LED pin number
Write the setup() function to set the LED pin as output
Write the loop() function to turn the LED on and off with a delay
Print a message to the Serial Monitor when the LED turns on
💡 Why This Matters
🌍 Real World
Blinking an LED is the first step in learning how to control hardware with Arduino. It helps you understand how to write code that interacts with physical devices.
💼 Career
Understanding Arduino sketch structure is essential for embedded systems developers, hobbyists, and engineers working with microcontrollers and IoT devices.
Progress0 / 4 steps
1
Set up the LED pin variable
Create an int variable called ledPin and set it to 13.
Arduino
Hint
Use int ledPin = 13; to store the pin number.
2
Write the setup() function
Write the setup() function. Inside it, use pinMode(ledPin, OUTPUT); to set the LED pin as an output.
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 on with digitalWrite(ledPin, HIGH);, wait 1000 milliseconds with delay(1000);, then turn the LED off with digitalWrite(ledPin, LOW);, and wait another 1000 milliseconds.
Arduino
Hint
The loop() function runs over and over again.
4
Add Serial message when LED turns on
Inside the setup() function, add Serial.begin(9600);. Then inside the loop() function, after turning the LED on, add Serial.println("LED is ON");. This will print a message to the Serial Monitor each time the LED turns on.
Arduino
Hint
Use Serial.begin(9600); in setup() and Serial.println("LED is ON"); in loop().
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
Step 1: Understand the role of setup()
The setup() function runs once when the Arduino starts to prepare the board.
Step 2: Compare with loop()
The loop() function runs repeatedly, but setup() runs only once.
Final Answer:
To run code once at the start to initialize settings -> Option A
In loop(), the line digitalWrite(13, HIGH) is missing a semicolon at the end.
Step 2: Verify other parts
pinMode is correctly in setup(), delay() is allowed in loop(), and digitalWrite() has correct arguments.
Final Answer:
Missing semicolon after digitalWrite(13, HIGH) -> Option B
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
Step 1: Understand blinking 5 times
Since loop() runs forever, use a counter variable inside loop() to count blinks.
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.
Final Answer:
Use a counter variable in loop() and stop blinking after 5 times -> Option C
Quick Check:
Counter in loop() controls blink count = A [OK]
Hint: Use a counter in loop() to limit repetitions [OK]