100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Programming

Arrays in Arduino

Store and manage collections of related values using arrays, and combine them with loops to control multiple pins efficiently.

Control Flow & FunctionsBeginner8 min readJul 10, 2026
Analogies

What Is an Array?

An array is a single variable that holds many values of the same type, stored in order and accessed by an index. Instead of declaring led1, led2, led3, and led4 separately, you declare int leds[4] = {2, 3, 4, 5}; and reach each value with leds[0] through leds[3]. Arrays shine on microcontrollers because they let one loop process many pins, sensor readings, or LED states with just a few lines of code.

🏏

Cricket analogy: An array is a batting order: one lineup holding eleven players in a fixed sequence. You refer to 'position 3' rather than naming a separate variable for every batter — index instead of individual names.

Declaring and Indexing Arrays

You declare an array with a type, a name, a size in square brackets, and optionally an initializer list: int values[3] = {10, 20, 30};. Indexing is zero-based, so the first element is values[0] and the last is values[size - 1]. Reading and writing use the same bracket syntax — int x = values[1]; sets x to 20, and values[1] = 99; overwrites it. The size is fixed at compile time and cannot grow later.

🏏

Cricket analogy: Zero-based indexing is like an over numbered 0 to 5 for six balls: the first delivery is ball 0 and the last is ball 5. Ask for ball 6 and you have overstepped the over.

cpp
int leds[4] = {2, 3, 4, 5};
const int NUM_LEDS = 4;

void setup() {
  // Set every LED pin to OUTPUT with one loop
  for (int i = 0; i < NUM_LEDS; i++) {
    pinMode(leds[i], OUTPUT);
  }
}

void loop() {
  // Chase effect across the four LEDs
  for (int i = 0; i < NUM_LEDS; i++) {
    digitalWrite(leds[i], HIGH);
    delay(150);
    digitalWrite(leds[i], LOW);
  }
}

Arduino does NOT check array bounds. Writing to leds[4] in a 4-element array (valid indices 0-3) reads or corrupts adjacent memory, causing unpredictable glitches or crashes that are very hard to debug. Always keep indices between 0 and size - 1.

Looping Over Arrays

The real power of arrays appears when you pair them with a for loop, using the loop counter as the index. This lets you initialize many pins, read a bank of sensors, or animate a row of LEDs with a handful of lines. A common pattern is to store the array's length in a const int and use it as the loop bound, so if you add a fifth pin you change the data and the count in one place, and the loop adapts automatically.

🏏

Cricket analogy: Looping over an array is a coach running the same fielding drill down the batting order, player by player, index by index, until the whole lineup has had a turn — one routine, everyone processed.

A String or text is often handled as a char array (a C-string) ending with a null character '\0'. sizeof(array) gives the total bytes, so for a non-char array the element count is sizeof(array) / sizeof(array[0]). Storing the length in a const int is clearer and avoids this calculation.

Multi-Dimensional Arrays

Arrays can have more than one dimension, which is useful for grids and tables. A two-dimensional array like int grid[3][4] models 3 rows of 4 columns and is accessed with two indices, grid[row][col]. This is handy for driving an LED matrix or storing patterns. Multi-dimensional arrays consume memory as the product of their dimensions, so on a memory-limited board keep them small and prefer byte over int when the values fit.

🏏

Cricket analogy: A 2D array is a full scorecard grid: rows for batters, columns for runs, balls, and fours. You read a cell by [batter][stat], addressing the table with two coordinates.

  • An array holds many values of the same type in order, accessed by a numeric index.
  • Indexing is zero-based: valid indices run from 0 to size - 1.
  • Array size is fixed at compile time and cannot grow at runtime.
  • Arduino does not check bounds; an out-of-range index corrupts memory.
  • Pairing arrays with for loops lets a few lines control many pins or sensors.
  • Store the length in a const int so the data and loop bound stay in sync.
  • Multi-dimensional arrays model grids and tables but consume dimension-product memory.

Practice what you learned

Was this page helpful?

Topics covered

#Programming#ArduinoProgrammingStudyNotes#ArraysInArduino#Arrays#Arduino#Array#Declaring#DataStructures#StudyNotes#SkillVeris