Arduino’s language is packed full of goodies that make things quite a bit easier to accomplish. Without loops, we would rewrite code over and over again, and it would be the ugliest code you’ve ever seen.

It takes a while before some people even start using loops, you’re in the right place if that’s you. Using loops will change the way you structure your sketches completely. You’ll find this is a huge time saver. Some times a little effort goes a long way!

for loops

What is it?

A for loop is a loop that runs a set amount of times. It loops until a predetermined condition is met, you set this condition at the beginning of the loop. Once the condition is met, it breaks out of the loop, and continues on with the code.

Most of the time, it’s good practice to keep the loops in a function or an if statement. Doing this avoids slowing down your sketch, by running through the loop every single time your main loop starts over.


One of my favorite uses, is using for loops to cycle through each value in an array. One example of a for loop that I used recently, was in a weather station. I designed a weather station that had 16 underground sensors. Instead of running a function 16 times, I ran a for loop that counted up to 16, and ran the function to read from each one individually. This took much less code than it would have otherwise.


This is the syntax from

for (initialization; condition; increment) {

Let’s pick that apart.

You of course state that you’re using the for loop by starting with “for”. The arguments to the for loop can be tricky at first, but they are fairly easy to understand. You start with initialization. I prefer to start with “int i”. You initialize an int here generally, because you’re going to use that int to count with. This is the variable that you’ll use to determine when the loop is done.

The condition that you’re setting here is what needs to be true for this loop to be done. You could put i<=10 and it will run until it is equal to 10.

The last portion here is increment. Normally I’d simply put i++ here. This is where you’ll add to the int that was defined.

An important note: If you use this to count things in an array, arrays start with an index of 0. So if you programmatically have your sketch determining the length of an array, you need to make sure you’re calling array[i-1]. This would start your index at 0, and make sure you don’t go over the amount of values that’s in the array.


This example will count to 10, waiting 1 second in between each number. You can speed this up by changing the delay at the end of the loop:

void setup() {
Serial.begin(9600); //Initialize serial output

void loop() {
Serial.println("For loop starting");

for (int i=0; i <= 10; i++) { //initialize the for loop, it will count i up to 10, then be done
 Serial.print("Counting:");//Write the text counting
 Serial.println(i);//Print out the value of i and start a new line after
 delay(1000);//Wait 1 second

Serial.println("For loop complete");


Let’s see what the output gives us on the serial monitor.

There you have it! Now we can count to 10, without having to use our brain!


1 Comment

  1. Hi there,I check your blogs named “Arduino – For Loop – Your Epic Tech” daily.Your humoristic style is witty, keep doing what you’re doing! And you can look our website about free proxy.

Leave a Reply

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