The Arduino IDE helps you write and upload code to Arduino boards easily. A sketch is the name for your Arduino program, and it has a simple structure to follow.
When you want to control lights, motors, or sensors with an Arduino board.
When you are starting a new project and need to write code for Arduino hardware.
When you want to test simple ideas by writing small programs for Arduino.
When you want to learn how to make electronics interactive using code.
Syntax
Arduino
void setup() {
// code here runs once when the board starts
}
void loop() {
// code here runs repeatedly
}
setup() runs once to set up your board.
loop() runs over and over to keep your program running.
Examples
This sketch blinks the built-in LED on pin 13 on and off every second.
Arduino
void setup() {
pinMode(13, OUTPUT); // set pin 13as 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
}
This sketch sends a message to the computer every 2 seconds.
This program makes the built-in LED blink on and off every half second.
Arduino
void setup() {
pinMode(13, OUTPUT); // set pin 13as output
}
void loop() {
digitalWrite(13, HIGH); // turn LED on
delay(500); // wait half a second
digitalWrite(13, LOW); // turn LED off
delay(500); // wait half a second
}
OutputSuccess
Important Notes
Every Arduino sketch must have setup() and loop() functions.
Use comments (//) to explain your code for yourself and others.
The Arduino IDE compiles and uploads your sketch to the board with a click.
Summary
The Arduino IDE is a simple tool to write and upload code to Arduino boards.
A sketch has two main parts: setup() runs once, loop() runs repeatedly.
This structure helps you control hardware easily and make interactive projects.
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]
Common Mistakes:
Trying to remove loop() function
Putting repeated code only in setup()
Using delay() to count blinks incorrectly
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.