Variables & Constants

Lesson 12 - Understanding Arrays in Arduino

Learn how arrays store multiple values of the same type, how indexing works, and when arrays are better than many separate variables.

Progress indicator

Lesson 12 of 12

Learning Objectives

  • Understand that an array stores multiple values of the same data type.
  • Learn array syntax, declaration, initialization, size, and indexing basics.
  • Access and modify array elements using indexes.
  • Compare arrays with using many separate variables.
  • Use arrays for repeated values like pins, delays, and sensor samples.
  • Avoid index mistakes and understand why arrays use zero-based indexing.

Concept Explanation

What is an Array

An array is a group of values stored under one variable name. All values in the array must be the same type (for example, all int values).

Arrays are useful when you have repeated data like multiple LED pins, multiple sensor readings, or several timing values.

Instead of creating many separate variables, one array lets you manage a whole group of related values in a consistent way.

Array Syntax

Basic syntax uses square brackets:

int values[4];
int values[4];
int pins[3] = {2, 4, 5};
float temps[2] = {26.5, 27.1};

The number inside square brackets is the array size (how many items the array can hold).

Declaring Arrays

Declaring an array means choosing the data type, the array name, and how many elements it can store.

int sensorValues[5];
byte ledLevels[4];

All values in one array must use the same type. You cannot mix int and float values in the same array.

Initializing Arrays

Initializing means giving values when the array is created.

int pins[3] = {2, 4, 5};
int delays[3] = {200, 400, 600};

Values are written inside curly braces in order.

The first value becomes index 0, the second becomes index 1, and so on.

Array Size and Indexing

If an array has size 3, it has indexes 0, 1, and 2. Arrays use zero-based indexing.

The last valid index is always size - 1.

Accessing Array Elements

Use square brackets with an index, like ledPins[1], to read one element.

You can use that value anywhere the original type is accepted, such as in pinMode(), digitalWrite(), or delay().

Modifying Array Values

You can update one element by index, for example delaysMs[1] = 500;. This changes only that element, not the whole array.

This is helpful when you want to update one setting (for example, one LED delay) without changing the others.

Array vs Single Variable

If you have many related values, arrays are cleaner than making many separate variables like ledPin1, ledPin2, and ledPin3.

Single variables

Easy for 1-2 values, but messy when values repeat.

Arrays

Better for grouped values and index-based access.

When to Use Arrays

  • Multiple LED pins
  • Lists of delays or timing values
  • Sensor sample buffers
  • Repeated settings of the same type

Arrays become even more powerful when used with loops (you will learn that in control-structure lessons).

Real-Life Example

A traffic-light controller may store pin numbers for red, yellow, and green LEDs in an array so the program can access them by index.

Example Code

This example stores LED pins and delay values in arrays, then uses an index to control one LED.

int ledPins[3] = {2, 4, 5};
int delaysMs[3] = {200, 400, 600};
int activeIndex = 1;

void setup() {
  Serial.begin(115200);

  pinMode(ledPins[0], OUTPUT);
  pinMode(ledPins[1], OUTPUT);
  pinMode(ledPins[2], OUTPUT);

  delaysMs[1] = 500;  // modify one array value
  Serial.println("Arrays ready");
}

void loop() {
  digitalWrite(ledPins[activeIndex], HIGH);
  delay(delaysMs[activeIndex]);
  digitalWrite(ledPins[activeIndex], LOW);
  delay(delaysMs[activeIndex]);
}

What this example teaches

  • How to create and initialize integer arrays
  • How to read array elements using indexes
  • How to modify one array value
  • How arrays reduce repeated variables

Important idea

The same index (activeIndex) is used for both arrays, which keeps related values (pin and delay) aligned.

Example Code Explanation

  1. int ledPins[3] = {2, 4, 5}; stores three LED pin numbers in one array.
  2. int delaysMs[3] = {200, 400, 600}; stores matching delay values.
  3. int activeIndex = 1; selects which array position to use.
  4. pinMode(ledPins[1], OUTPUT); uses an array element as a pin value.
  5. delaysMs[1] = 500; updates one delay value inside the array.
  6. digitalWrite(ledPins[activeIndex], ...) and delay(delaysMs[activeIndex]) show how one index can control related values in multiple arrays.

Beginner reading tip

Read arrays by asking: What is stored? (pins or delays), What is the size?, and Which index is being used right now?

Real-life example

A lighting controller may store many LED pin numbers and timing settings in arrays so it can switch behavior by index instead of hard-coding each one.

Common Mistakes with Arrays

  • Using an invalid index (for example, index 3 in an array of size 3).
  • Forgetting arrays start at index 0.
  • Mixing unrelated values into one array instead of using separate arrays.
  • Using many separate variables when an array would be cleaner and easier to manage.
  • Using one index for one array and a different index for a related array by mistake.

Best Practices for Arrays

  • Use arrays when you have multiple related values of the same type.
  • Use clear names like ledPins, delaysMs, and sensorValues.
  • Keep track of array size and valid indexes.
  • Group related arrays by matching indexes (for example, pin and delay arrays).
  • Comment the meaning of indexes when it helps beginners (for example, 0=Red, 1=Yellow, 2=Green).

Practice Task

  1. Change activeIndex from 1 to 0 and explain which LED pin and delay value are used.
  2. Update delaysMs[2] to 800 and explain what changed.
  3. Add a fourth LED pin and delay value to both arrays, then update the array size.
  4. Explain why arrays are cleaner than separate variables for this example.

Try it now

Open the simulator workspace and observe variables while stepping through examples.

Run in Simulator