Skip to content

Arduino Loops

Arduino Loops with Examples | Arduino Tutorial 4

In the previous project (Tutorial 3), we learned In this Arduino tutorial, we will explore Arduino loops, their syntax, and practical examples. Learn how to use for loop, while loop, and do while loop in Arduino programming with step-by-step circuits, Arduino code, and explanations for beginners.


Introduction

Hi friends! 
In the previous tutorial of this Arduino series, we learned how to use digital pins of Arduino as input and created some basic projects.

In this project, we are going to explore one of the most important programming concepts: loops in Arduino. Loops allow us to repeat commands and control the flow of our program effectively. Along the way, we’ll also build simple circuits and upload working codes to the Arduino.

So, without further delay, let’s dive into Arduino loops and make some example projects.


Before We Begin: Variables in Arduino

Before learning about loops, it’s important to understand variables, because loops often use them.

  • A variable is a container for storing data.

  • It has:

    • Name → identifier (e.g., i)

    • Type → data type (e.g., int)

    • Value → stored data (e.g., 0)

  •  

Example:

 
int i = 0;
  • int → Type (integer)

  • i → Name of the variable

  • 0 → Value

Key Notes on Variables:

  • Variables must be declared before use.

  • Default value of an uninitialized variable is 0.

  • Different types of variables can store different forms of data (integer, float, character, etc.).


What Are Loops?

A loop is a program structure that repeats a set of commands until a specific condition is met.

In Arduino, we commonly use three types of loops:

  1. for loop

  2. while loop

  3. do…while loop

Each has its own syntax and use case, which we’ll discuss with examples.


The for Loop in Arduino.

Syntax:

 
for(initialization; condition; increment/decrement) {
// commands to be repeated
}
  • Initialization → Set starting point (e.g., int i = 0)

  • Condition → Loop continues until this condition is false (e.g., i < 5)

  • Increment/Decrement → Updates the variable (e.g., i++)


Example 1: Blinking LED with for Loop

let’s make an example circuit and program to better understand for loop.

 Materials for the Project

ComponentQuantityBuy Link
Arduino UNO1Buy Here
LED (5mm)1Buy Here
100Ω Resistor1Buy Here
Breadboard1Buy Here
Jumper WiresAs neededBuy Here

Circuit Diagram Explanation

first example

  • Connect Arduino 5V pin → Breadboard positive rail.

  • Connect Arduino GND pin → Breadboard negative rail.

  • Connect LED anode (long pin) → Arduino pin D2.

  • Connect LED cathode → One end of 100Ω resistor → other end of resistor → GND.

  1. First connect the positive supply rail of the breadboard to 5 volt pin of arduino.
  2. Now connect the negative supply rail of the breadboard to ground pin of the arduino.
  3. Now connect an led and connect its anode to pin d2 of arduino.
  4. Now connect 100 ohm resistor and connect its one pin to cathode of led and its other pin to ground.
  5. So we have completed the circuit.

Example Code:

int led = 2;

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

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

In this example:

  • The LED blinks 5 times quickly using the for loop.

  • After that, it blinks with a 1-second delay outside the loop.

 

Example 2: Multiple LEDs with for Loop.

 Materials for the Project

ComponentQuantityBuy Link
Arduino UNO1Buy Here
LED (5mm)6Buy Here
100Ω Resistors6Buy Here
Breadboard1Buy Here
Jumper WiresAs neededBuy Here

Circuit Diagram Explanation.

let’s make another example circuit.

  • Connect 6 LEDs to pins D2, D3, D4, D5, D6, D7 of Arduino.

  • Each LED cathode is connected to GND through a 100Ω resistor.

  1. First connect the positive supply rail of the breadboard to 5 volt pin of arduino.
  2. Now connect the negative supply rail of the breadboard to ground pin of the arduino.
  3. Now connect an led and connect its anode to pin d2 of arduino.
  4. Now connect 100 ohm resistor and connect its one pin to cathode of led and its other pin to ground.
  5. Now connect another led and connect its anode to pin d3 of arduino.
  6. Now connect 100 ohm resistor and connect its one pin to cathode of led and its other pin to ground.
  7. Now connect another led and connect its anode to pin d4 of arduino.
  8. Now connect 100 ohm resistor and connect its one pin to cathode of led and its other pin to ground.
  9. Now connect another led and connect its anode to pin d5 of arduino.
  10. Now connect 100 ohm resistor and connect its one pin to cathode of led and its other pin to ground.
  11. Now connect another led and connect its anode to pin d6 of arduino.
  12. Now connect 100 ohm resistor and connect its one pin to cathode of led and its other pin to ground.
  13. Now connect another led and connect its anode to pin d7 of arduino.
  14. Now connect 100 ohm resistor and connect its one pin to cathode of led and its other pin to ground.
  15. So we have completed the circuit.

Multiple LEDs with for Loop.


Example Code:

void setup() {
for (int i = 2; i < 8; i++) {
pinMode(i, OUTPUT);
}
}

void loop() {
for (int i = 2; i < 8; i++) {
digitalWrite(i, HIGH);
delay(1000);
digitalWrite(i, LOW);
delay(1000);
}
}

In this example:

  • All pins D2 to D7 are set as outputs in a for loop inside setup().

  • Then, each LED is turned ON and OFF one by one in the loop() function.


Practical Uses of Loops in Arduino

  • Controlling multiple LEDs or outputs.

  • Reading sensor data repeatedly.

  • Running time delays in structured patterns.

  • Automating tasks like motors, relays, buzzers.


FAQs on Arduino Loops

Q1: What is the difference between for and while loops?

  • for → Used when the number of iterations is known.

  • while → Used when the iterations depend on a condition.

Q2: Which loop guarantees at least one execution?

  • do…while loop.

Q3: Can we use nested loops in Arduino?

  • Yes, loops can be placed inside one another for advanced tasks.

Q4: Why should I use loops instead of writing repeated code?

  • Loops reduce program size, improve readability, and make code efficient.

Q5: Can loops cause Arduino to freeze?

  • Yes, if the condition never becomes false, the loop can become infinite. Always design carefully.


Conclusion

In this tutorial, we explored Arduino loops in detail.

  • We started by understanding variables.

  • We then studied for loop, while loop, and do…while loop with real examples.

  • We built practical circuits using LEDs and push buttons.

Loops are fundamental in Arduino programming. They help us repeat tasks, save time, and create efficient projects. Mastering loops will make you confident in handling bigger and more complex Arduino projects.

Leave a Reply

Your email address will not be published. Required fields are marked *