5

Lesson 5: Loops and Repetition with Arduino

Use `loop()` and `for` loops to repeat actions clearly and on purpose

32-Lesson Course • Semester 1

Lesson 4 taught you how to make decisions with `if` and `else`. Now you will learn how Arduino repeats actions. Repetition is one of the most important ideas in programming because computers are very good at doing the same kind of task again and again.

Learning Objectives

By the end of this lesson, you will:

  • Understand that `loop()` automatically repeats forever
  • Use a `for` loop to repeat an action a specific number of times
  • Trace what happens during each repetition
  • Combine loops with LEDs, delays, and simple conditionals
  • Recognize when repeated code should become a loop

Key ideas:

  • Repetition saves time and reduces repeated code
  • `loop()` is built into every Arduino sketch
  • `for` loops are useful when you know how many times to repeat
  • A loop variable changes as the repetitions continue
  • Loops often work together with `if` statements

What Does `loop()` Do?

Every basic Arduino sketch has `setup()` and `loop()`. `setup()` runs once when the program starts. `loop()` runs again and again for as long as the board has power.

Simple idea

If you place a line of code inside `loop()`, Arduino does not run it only one time. It keeps repeating that code over and over.

void setup() {
  Serial.begin(9600);
}

void loop() {
  Serial.println("Arduino is repeating this line.");
  delay(1000);
}

This message prints once each second because `loop()` keeps starting over after it reaches the end.

Why Loops Matter

Without loops, you would have to write the same lines again and again. That becomes messy very quickly.

Without a loop

digitalWrite(ledPin, HIGH);
delay(500);
digitalWrite(ledPin, LOW);
delay(500);
digitalWrite(ledPin, HIGH);
delay(500);
digitalWrite(ledPin, LOW);
delay(500);

With a loop

for (int i = 0; i < 2; i++) {
  digitalWrite(ledPin, HIGH);
  delay(500);
  digitalWrite(ledPin, LOW);
  delay(500);
}

Understanding a `for` Loop

A `for` loop repeats a block of code a specific number of times. It is very useful when you already know how many repetitions you want.

for (starting value; condition; change) {
  // code to repeat
}

Start

`int i = 0` creates the loop variable and gives it a starting value.

Condition

`i < 5` means “keep going while `i` is less than 5.”

Change

`i++` adds 1 after each repetition.

Blink an LED Five Times

This is a classic beginner use of a `for` loop. The code repeats the blink pattern exactly five times.

Use an LED on pin `8` with a resistor to ground.
const int ledPin = 8;

void setup() {
  pinMode(ledPin, OUTPUT);
}

void loop() {
  for (int i = 0; i < 5; i++) {
    digitalWrite(ledPin, HIGH);
    delay(300);
    digitalWrite(ledPin, LOW);
    delay(300);
  }

  delay(2000);
}

What to notice

  • The blink code runs five times because `i` takes the values `0, 1, 2, 3, 4`
  • When `i` becomes `5`, the condition `i < 5` is false
  • Then the program continues to the next line after the loop

Loops and Conditionals Together

Loops and conditionals often work together. For example, a button can decide whether an LED should blink multiple times.

const int ledPin = 8;
const int buttonPin = 2;

void setup() {
  pinMode(ledPin, OUTPUT);
  pinMode(buttonPin, INPUT_PULLUP);
}

void loop() {
  int buttonState = digitalRead(buttonPin);

  if (buttonState == LOW) {
    for (int i = 0; i < 3; i++) {
      digitalWrite(ledPin, HIGH);
      delay(200);
      digitalWrite(ledPin, LOW);
      delay(200);
    }
  }
}

Here the button decision uses `if`, and the repeated blinking uses a `for` loop.

Common Beginner Mistakes

Mistake 1

Forgetting that `loop()` already repeats forever.

Mistake 2

Writing a `for` loop condition that never becomes false.

Mistake 3

Not tracing the loop variable one step at a time.

Mistake 4

Repeating many lines manually instead of using a loop.

Hands-On Activity: Button Makes the LED Blink

Build a simple circuit where pressing the button makes the LED blink three times. If the button is not pressed, the LED stays off.

Required parts

  • Arduino Uno
  • Breadboard
  • 1 LED
  • 1 resistor (`220Ω` or `330Ω`)
  • 1 pushbutton
  • Jumper wires

Activity steps

  1. Wire the LED to digital pin `8` and ground through a resistor
  2. Wire the button to digital pin `2` and ground
  3. Set the button pin to `INPUT_PULLUP`
  4. Use `if` to check whether the button is pressed
  5. Use a `for` loop to blink the LED three times
  6. Test the circuit and count the blinks carefully
Extension: change the loop so the LED blinks five times, then change the speed by editing the delay values.

Assessment and Homework

Lesson 5 quiz topics

  • What `loop()` does
  • How a `for` loop works
  • Tracing the loop variable
  • Using loops with LEDs and delays
  • Combining loops with `if` statements

Homework ideas

  • Write a `for` loop that prints numbers 1 through 5
  • Change a blink loop from 3 repetitions to 10
  • Explain in words what `i++` means
  • Practice deciding when you need repetition in a program
When a program repeats the same task many times, stop and ask: should this be a loop?