Understanding Loops in Arduino: for, while, and do while Explained
When you start writing Arduino code, one of the first things you’ll come across are loops. Loops are a way to repeat a section of code multiple times — automatically — so you don’t have to copy and paste the same commands over and over.
Let’s break down the three main types of loops you’ll use: for, while, and do while.
1. The for Loop
Think of the for loop as your “counting” loop. You use it when you know exactly how many times you want something to repeat.
For example, if you want an LED to blink 10 times, you can tell the for loop to do just that.
It has three parts inside the parentheses:
- A starting point (where to begin counting)
- A condition (when to stop)
- An update (how to move to the next step)
It’s neat, compact, and great for predictable tasks.
2. The while Loop
A while loop keeps running as long as a certain condition is true. It doesn’t care how many times it loops — it just checks the condition each time.
This is useful when you don’t know exactly when to stop, like waiting for a button press or a sensor value to change.
But be careful: if the condition never becomes false, your program could get stuck in an infinite loop!
3. The do while Loop
The do while loop is like the while loop, except it runs at least once, no matter what.
That’s because the condition is checked after the loop runs. It’s great for things like menus, input checks, or anything where you want your code to run once before testing a condition.
Which Loop Should You Use?
- Use a for loop when you know how many times to repeat something.
- Use a while loop when you need to wait for something to happen.
- Use a do while loop when you want to run code at least once before checking the condition.
Final Thoughts
1. The for Loop
The for loop is great when you already know how many times something needs to happen.
For example, let’s blink an LED 10 times:
int ledPin = 13; // built-in LED on most Arduino boards
void setup() {
pinMode(ledPin, OUTPUT);
}
void loop() {
for (int i = 0; i < 10; i++) {
digitalWrite(ledPin, HIGH); // turn LED on
delay(500);
digitalWrite(ledPin, LOW); // turn LED off
delay(500);
}
delay(2000); // pause before repeating the whole sequence
}
🟢 What’s happening:
int i = 0→ start counting from 0i < 10→ keep looping untilireaches 10i++→ add one each time
The LED blinks exactly 10 times — no more, no less.
🔁 2. The while Loop
A while loop keeps running as long as a certain condition is true.
Let’s say we want the LED to blink until a button is pressed:
int ledPin = 13;
int buttonPin = 7;
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT_PULLUP); // button connected to ground
}
void loop() {
while (digitalRead(buttonPin) == HIGH) {
digitalWrite(ledPin, HIGH);
delay(300);
digitalWrite(ledPin, LOW);
delay(300);
}
}
🟡 What’s happening:
The LED keeps blinking until you press the button.
When the button is pressed, the condition becomes false, and the loop stops.
⚠️ Be careful: if the condition never changes, the loop never ends — that’s called an infinite loop!
🔄 3. The do while Loop
The do while loop works like a while loop, but it always runs at least once before checking the condition.
Here’s an example:
int counter = 0;
void setup() {
Serial.begin(9600);
}
void loop() {
do {
Serial.print("Count: ");
Serial.println(counter);
counter++;
delay(500);
} while (counter < 5);
while (true); // stop after finishing
}
🟣 What’s happening:
The code inside the loop runs first, then the condition is checked.
Even if the condition is false at the start, it still runs once before stopping.
⚙️ Which Loop Should You Use?
Here’s a quick cheat sheet:
| Loop Type | Best For | Runs at Least Once? |
|---|---|---|
| for | Fixed number of repeats | No |
| while | Waiting for something to happen | No |
| do while | Doing something once before checking | ✅ Yes |
💡 Final Thoughts
Loops are the heartbeat of your Arduino sketches. They keep your code clean, flexible, and easy to control. Once you get comfortable with them, you’ll be able to handle sensors, lights, motors — anything — with simple logic and repetition.
Next time you find yourself copying and pasting code, stop and think: “Hey, maybe a loop can do this for me!”
So next time you blink an LED or read a sensor, think about which loop fits best — and let your code do the work for you!