Arduino 2D Array
- 2D Array Initialization in Arduino
-
Storing Data Into 2D
Array
in Arduino -
Replace a Value in 2D
Array
at a Specific Position in Arduino -
Retrieving a Value From a Specific Position in 2D
Array
in Arduino -
Arduino
MatrixMath
Library for 2D Arrays

In this tutorial, we will discuss 2D array in Arduino. We will discuss how we can initialize a 2D array and use it to store data.
2D Array Initialization in Arduino
2D array initialization is quite similar to 1d array initialization. In a 2D array, we have to define the number of rows and columns and then initialize it with some data. For example, see the code below.
void setup(){
int nRow = 2;
int nCol = 4;
int myArray[nRow][nCol] = {
{1, 2, 3, 4},
{5, 6, 7, 8}
};
}
In the above code, nRow
is a variable of type int
which is used to defines the number of rows in the 2D array. nCol
is a variable of type int
which is used to define the number of columns in the 2D array, and myArray
is an array of type int
, which is used to store the given int
values. You can change all these values according to the given data. Note that you can also define other data type arrays like float
in Arduino. Also, note that you have to define the number of rows and columns or at least the number of columns during the array initialization.
Storing Data Into 2D Array
in Arduino
If you want to store data into a 2D array, you have to use two loops for this purpose. To store data in a 2D array, you have to go to every position in a 2D array and store data there. A 2D array has many elements, so it will be time-consuming to store data at every position manually. To save time, you can use two loops to go to every position and store the given data at the specific position. For example, see the code below.
void setup(){
int data = 0;
int myArray[nRow][nCol];
for (int nr =0; nr < nRow; nr++) {
for (int nc =0; nc < nCol; nc++) {
myArray[nr][nc] = data++;
}
}
}
In the above code, we are using two loops to go to every position in a 2D array and store the given data there. In this example, the data is a variable whose value is zero, but you can change its value according to given data. This method will store or replace data at every position in the 2D array.
Replace a Value in 2D Array
at a Specific Position in Arduino
If you want to store or replace data at only one position, you only have to use the assignment operator for the specific position. For example, see the code below.
void setup(){
int nRow = 2;
int nCol = 4;
int myArray[nRow][nCol] = {
{1, 2, 3, 4},
{5, 6, 7, 8}
};
myArray[nRow][nCol] = 0;
}
In the above code, we are replacing the value present at the nRow
and nCol
position in the 2D array to 0
.
Retrieving a Value From a Specific Position in 2D Array
in Arduino
If you want to get a value from a specific position in 2D array, you can use the assignment operator. For example, see the code below.
void setup(){
int nRow = 2;
int nCol = 4;
int myArray[nRow][nCol] = {
{1, 2, 3, 4},
{5, 6, 7, 8}
};
int myValue = myArray[nRow][nCol];
}
In the above code, we are retrieving a value present at the nRow
and nCol
position in 2D array, which is 8
.
Arduino MatrixMath
Library for 2D Arrays
You can use the MatrixMath
library present in Arduino for many operations like addition, subtraction, multiplication, inverse, and printing of 2D arrays. Read the documentation of the library for more information and check an example here.