Introduction to Arduino Coding
Introduction to Arduino Coding
Understanding setup() and loop() Functions
April Joy B. Arcilla
Learning Objectives
• Understand the structure of an Arduino sketch.
• Learn the purpose of setup() and loop() functions.
• Apply coding concepts to control LEDs.
Basic Structure of Arduino Code
void setup() {
// runs once when the board is powered on or reset
void loop() {
// runs continuously after setup
Explanation:
• setup() → Initialization
• loop() → Repeated execution
What is setup()?
• Called once when the Arduino starts.
• Used to initialize pins, start serial communication, etc.
Example:
void setup() {
pinMode(5, OUTPUT);
What is loop()?
• Called repeatedly after setup().
• Used to run the main logic of your program.
Example:
void loop() {
digitalWrite(5, HIGH);
delay(1000);
digitalWrite(5, LOW);
delay(1000);
LED Blinking Example
Code:
int led = 5;
void setup() {
pinMode(led, OUTPUT);
void loop() {
digitalWrite(led, HIGH);
delay(1000);
digitalWrite(led, LOW);
delay(1000);
Explanation:
• LED turns on for 1 second, then off for 1 second.
Hands-On Activity
Task: Connect an LED to pin 5 and upload the blinking code.\ Goal: Observe the LED
blinking every second.
Quick Quiz
1. What does pinMode() do?
2. What happens if you remove delay()?
3. Can loop() run without setup()?
Summary
• setup() runs once → for initialization.
• loop() runs repeatedly → for main logic.
• These two functions are essential in every Arduino sketch.
Q\&A / Discussion
• Encourage students to ask questions.
• Discuss how they can modify the code to blink multiple LEDs.