Learn Arduino Tutorials Open the 3D lab

Beginner · 6 min read

Arduino for loops and arrays — repeat without copy-paste

Eight buttons, fifteen notes, six LEDs: writing the same line again and again gets long and full of mistakes. A for loop and an array do it in three lines. This guide shows how, using the code from two real projects.

An Arduino UNO wired to eight push buttons and a buzzer on a breadboard, a mini piano that reads its keys with a for loop
Build it yourself in 3DFree, in your browser — no board, no wires, nothing to buy.Open the 3D lab →

The short answer

A for loop runs the same block of code a set number of times, with a counter that changes each time. An array is a list of values under one name, numbered from 0. Together:

const int PINS[] = { 2, 3, 4, 5 };

for (int i = 0; i < 4; i++) {
  pinMode(PINS[i], OUTPUT);   // i = 0, 1, 2, 3 → pins 2, 3, 4, 5
}

Four pinMode calls in three lines, and adding a fifth pin means changing one list, not copying a line.

How a for loop counts

for (int i = 0; i < 4; i++) {
  Serial.println(i);
}

The round brackets hold three parts, separated by semicolons:

PartHereWhat it doesWhen
startint i = 0makes the counter and sets it to 0once, at the beginning
conditioni < 4keep going while this is truebefore every turn
stepi++add 1 to the counterafter every turn

Step by step, the board does this:

Turnii < 4?Prints
10yes0
21yes1
32yes2
43yes3
—4no → stop

It prints 0, 1, 2, 3: four numbers, starting at 0 and never reaching 4. That "start at 0, stop before the count" shape is the one you will write most often, because it matches how arrays are numbered.

Arrays: many values, one name

const int NOTES[] = { 262, 294, 330, 349 };   // C D E F, in hertz
  • The square brackets [] say "this is an array". The compiler counts the items for you.
  • NOTES[0] is 262, NOTES[1] is 294, … NOTES[3] is 349. The first item is number 0, not 1.
  • The number in brackets is called the index. An array of 4 items has indexes 0 to 3.
Index0123
NOTES[index]262294330349

All items in one array have the same type: all int, or all float, and so on. See variables and data types for which type to pick.

Together: play a tune

This is the core of our tune project. Two arrays line up side by side: the note and how many beats it lasts.

const int BUZZER = 8;
// "Ode to Joy" (Beethoven): E E F G G F E D C C D E E D D
const int NOTES[] = { 330, 330, 349, 392, 392, 349, 330, 294, 262, 262, 294, 330, 330, 294, 294 };
const int BEATS[] = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2 };
const int COUNT = 15;
const int BEAT = 300; // ms

void loop() {
  for (int i = 0; i < COUNT; i++) {
    int ms = BEATS[i] * BEAT;
    tone(BUZZER, NOTES[i], ms - 30);
    delay(ms);
  }
  delay(1500);
}
  • i goes 0 to 14, one turn per note.
  • NOTES[i] and BEATS[i] use the same index, so note number i always gets its own length.
  • ms - 30 stops each note 30 ms early, so two equal notes in a row sound like two notes and not one long one.

Want a new song? Change the two lists and COUNT. The loop stays the same.

The for loop plays each note in the array in turn (turn sound on in the toolbar)
The for loop plays each note in the array in turn (turn sound on in the toolbar) · open it full screen

Together: read eight keys

The mini piano has eight buttons on pins 2 to 9. Instead of eight digitalRead lines, it scans them:

const int KEYS[] = { 2, 3, 4, 5, 6, 7, 8, 9 };

void setup() {
  for (int i = 0; i < 8; i++) pinMode(KEYS[i], INPUT_PULLUP);
}

void loop() {
  int down = -1;                      // -1 = no key
  for (int i = 0; i < 8; i++) {
    if (digitalRead(KEYS[i]) == LOW) down = i;   // pressed keys read LOW
  }
  // down is now 0…7 for the key that is pressed, or -1
}

The loop asks every key "are you pressed?" and remembers which number answered yes. That index then picks the note from a second array, NOTES[down]. One loop, eight keys, and the if inside does the deciding.

Counting the items for you

Writing COUNT = 15 by hand works, but it is easy to forget to update. This line lets the compiler count:

const int COUNT = sizeof(NOTES) / sizeof(NOTES[0]);

sizeof(NOTES) is the size of the whole array in bytes (15 × 2 = 30 on an UNO, where an int is 2 bytes). Divide by the size of one item and you get 15.

Common mistakes

  • Going one past the end. for (int i = 0; i <= 4; i++) on a 4-item array reads NOTES[4], which does not exist. The board does not warn you: it reads whatever memory comes next. Use <, not <=.
  • Starting at 1. NOTES[1] is the second item. Arrays start at 0.
  • A semicolon after the ). for (int i = 0; i < 8; i++); loops eight times doing nothing, then runs the block once.
  • Arrays of different lengths. If NOTES has 15 items and BEATS has 14, the last note reads a random length. Keep paired arrays the same size.
  • A long delay() inside a scanning loop. The piano scans all eight keys thousands of times a second. Add delay(500) inside and the keys feel dead. See millis() vs delay().

Check yourself

  1. How many times does for (int i = 0; i < 5; i++) run?

  2. With const int PINS[] = { 7, 8, 9 }; what is PINS[1]?

  3. Why is i <= 3 wrong for a 3-item array?

Questions people ask

Can a for loop go on forever?

Only if its condition never becomes false, like for (;;). You rarely need that on an Arduino, because loop() already repeats forever.

How big can an Arduino array be?

The UNO has 2 KB (2048 bytes) of RAM for all your variables. An int array of 100 items uses 200 bytes, which is fine; 1000 items would fill it. Fixed lists such as notes can be stored in flash memory with PROGMEM when they get large.

What is the difference between a for loop and a while loop?

A for loop is best when you know how many times to repeat: 8 keys, 15 notes. A while loop repeats until something happens, such as "while the button is held". Each can do the other's job, but the right one reads more clearly.

Can I change the values in an array while the sketch runs?

Yes, if you leave out const. int scores[3] = {0, 0, 0}; then scores[1] = 42; changes the second item. Keep const for lists that never change, like pins and notes.

What's next

Build the mini piano and change its notes, then make every key press count exactly once with button debounce.

Build it yourself in 3DFree, in your browser — no board, no wires, nothing to buy.Open the 3D lab →
Next: Arduino button debounce — one press, one count, every time →

Your progress stays on this device — no account, nothing sent anywhere.

Last updated 2026-11-11

Read next