📘 Lesson P6 – Loops in Arduino Programming
🎯 Learning Objectives
After completing this lesson, students will be able to:
✅ Understand what loops are
✅ Understand why loops are needed
✅ Use the for loop
✅ Use the while loop
✅ Use the do-while loop
✅ Understand loop control and repetition
✅ Create repetitive tasks efficiently
✅ Use loops in real Arduino projects
1. Introduction
Imagine you want an LED to blink:
- Turn ON
- Turn OFF
100 times
Would you write:
digitalWrite(13,HIGH);
digitalWrite(13,LOW);
digitalWrite(13,HIGH);
digitalWrite(13,LOW);
digitalWrite(13,HIGH);
digitalWrite(13,LOW);
100 times?
❌ No.
That would make the program unnecessarily long and difficult to manage.
Instead, we use:
Loops
Loops allow Arduino to repeat instructions automatically.
2. What is a Loop?
A loop is a programming structure that repeats a block of code multiple times.
Think of a loop as:
“Do the same task again and again until a condition changes.”
Real-Life Example
Imagine a security guard checking a gate.
He does not check once and go home.
Instead:
Check Gate
↓
Wait
↓
Check Again
↓
Wait
↓
Check Again
↓
Repeat
This repetitive action is similar to a loop.
3. Why Do We Need Loops?
Loops help us:
✅ Reduce code length
✅ Save memory
✅ Improve readability
✅ Automate repetitive tasks
✅ Create efficient programs
Without loops, programming would become very difficult.
4. Types of Loops in Arduino
Arduino supports:
for Loop
while Loop
do-while Loop
These are the most commonly used loops.
5. The for Loop
The for loop is used when we know how many times we want to repeat something.
Syntax
for(initialization; condition; update)
{
// Code
}
Understanding Each Part
Initialization
Runs once at the beginning.
Example:
int i = 0;
Condition
Checked before each repetition.
Example:
i < 5
Update
Runs after each repetition.
Example:
i++
Example 1
for(int i=1; i<=5; i++)
{
Serial.println(i);
}
Output:
1
2
3
4
5
How It Works
Step 1
i = 1
Print 1
Step 2
i = 2
Print 2
Step 3
i = 3
Print 3
Continue until:
i = 5
After that:
Condition becomes false.
Loop stops.
6. LED Blink Using for Loop
for(int i=0; i<10; i++)
{
digitalWrite(13,HIGH);
delay(500);
digitalWrite(13,LOW);
delay(500);
}
Result:
LED blinks 10 times.
7. What is a Counter Variable?
In most loops we use:
i
This variable is called a:
Counter Variable
Its job is to count repetitions.
Example
for(int count=1; count<=10; count++)
More meaningful than simply using i.
8. The while Loop
Used when we do not know exactly how many times repetition will occur.
The loop continues while a condition remains true.
Syntax
while(condition)
{
// Code
}
Example
int count = 1;
while(count <= 5)
{
Serial.println(count);
count++;
}
Output:
1
2
3
4
5
How while Works
Arduino checks:
count <= 5
If TRUE:
Run code.
If FALSE:
Stop loop.
Real-Life Example
Imagine filling a water tank.
Continue pumping water
WHILE
Tank is not full.
When tank becomes full:
Stop.
9. Infinite Loop Problem
A common beginner mistake:
while(true)
{
Serial.println("Hello");
}
This never stops.
Called:
Infinite Loop
Why is it Dangerous?
Program becomes stuck.
Arduino cannot execute other instructions.
Always ensure the loop condition can eventually become false.
Wrong Example
int count = 1;
while(count <= 5)
{
Serial.println(count);
}
Problem:
count never increases.
Condition remains true forever.
Correct Version
count++;
must be included.
10. The do-while Loop
Similar to while loop.
Difference:
Code executes at least one time.
Syntax
do
{
// Code
}
while(condition);
Example
int count = 1;
do
{
Serial.println(count);
count++;
}
while(count <= 5);
Output:
1
2
3
4
5
Difference Between while and do-while
while
Checks condition first.
May execute zero times.
do-while
Executes once first.
Then checks condition.
At least one execution is guaranteed.
Example
int count = 10;
while(count < 5)
{
Serial.println("Hello");
}
Output:
Nothing
do-while Version
int count = 10;
do
{
Serial.println("Hello");
}
while(count < 5);
Output:
Hello
Runs once before checking.
11. Nested Loops
A loop inside another loop.
Example:
for(int i=1;i<=3;i++)
{
for(int j=1;j<=2;j++)
{
Serial.println("Arduino");
}
}
Output:
Arduino
Arduino
Arduino
Arduino
Arduino
Arduino
Applications of Nested Loops
- LED Matrix Displays
- Pattern Generation
- Advanced Robotics
- Display Systems
12. Real Arduino Project Examples
LED Blink
for(int i=0;i<10;i++)
Blink 10 times.
Automatic Water Dispenser
while(handDetected)
Keep pump running.
Robot Movement
while(distance > 10)
Move forward.
Traffic Signal
for(int i=10;i>0;i--)
Countdown timer.
13. Common Beginner Mistakes
Mistake 1
Missing update statement.
while(count < 10)
{
}
Creates infinite loop.
Mistake 2
Wrong condition.
for(int i=0;i>10;i++)
Loop never runs.
Mistake 3
Using semicolon incorrectly.
Wrong:
for(int i=0;i<5;i++);
{
Serial.println(i);
}
Mistake 4
Forgetting braces.
Can create unexpected results.
14. Best Practices
✅ Use meaningful variable names
✅ Keep loops simple
✅ Avoid infinite loops
✅ Use comments for complex loops
✅ Test loop conditions carefully
15. Choosing the Right Loop
| Situation | Recommended Loop |
|---|---|
| Known repetitions | for |
| Unknown repetitions | while |
| Must execute once | do-while |
📊 Summary
In this lesson, we learned:
✅ What loops are
✅ Why loops are needed
✅ for loop
✅ while loop
✅ do-while loop
✅ Infinite loops
✅ Nested loops
Loops allow Arduino to perform repetitive tasks efficiently and are essential for automation, robotics, and embedded systems programming.
📖 Key Terms
Loop
A structure that repeats code.
for Loop
Used when repetitions are known.
while Loop
Runs while condition remains true.
do-while Loop
Executes at least once.
Counter Variable
Variable used to count repetitions.
Infinite Loop
A loop that never ends.
Nested Loop
A loop inside another loop.
🎯 Quiz
1. Which loop is best when the number of repetitions is known?
A. while
B. do-while
C. for ✅
D. if
2. Which loop guarantees at least one execution?
A. for
B. while
C. do-while ✅
D. loop()
3. What is an infinite loop?
A. A loop that executes once
B. A loop that never stops ✅
C. A loop with an error
D. A loop inside another loop
4. Which statement updates the counter?
A. if
B. Serial
C. i++ ✅
D. setup
5. A loop inside another loop is called:
A. Conditional Loop
B. Infinite Loop
C. Nested Loop ✅
D. Logic Loop
🏠 Assignment
Task 1
Write a for loop that displays numbers from 1 to 20 in Serial Monitor.
Task 2
Write a for loop that displays even numbers from 2 to 20.
Task 3
Create a program that blinks an LED 15 times using a for loop.
Task 4
Write a while loop that counts down from 10 to 1.
Task 5
Explain the difference between for, while, and do-while loops with examples.