Arduino

What are the Arrays in Arduino? Explain with the Examples.

An array is a data structure used to store multiple values of the same data type in it; for example an array declared with the integer data type can store multiple integers in it. In Arduino, sometimes we have to use multiple values, for example, we are supposed to blink five LEDs, we will declare an integer array that will store the 5 pin numbers where we will attach the LEDs.

The use of Arrays in Arduino is similar to the other programming languages so in this write-up, we will explain the arrays and the way of utilizing them in Arduino.

What are arrays in Arduino

The arrays in Arduino use the multiple contiguous locations on the memory and store the multiple variables of the same data type in it. The advantage of using an array is; it saves the memory of the system as well as if the size of the array becomes insufficient while storing the elements during the execution of code, it stores them on contiguous memory locations.

What is the structure of the array in Arduino

The structure of arrays in Arduino is similar to the other programming languages. Consider the following image:


In the above figure, the elements of the array[5] are explained. We have an array[5] that has 5 memory locations. The name of the array is “array”, the values stored in the array are;55, 145, 250, 0, and 193. This array has elements on positions array[0], array[1], array[2], array[3], and array[4] where the values of the array will be stored. The position number is also known as an index number and it will always start from the zero position and will end at one less than the total size of an array.

How to declare array in Arduino

Like other variables of different data types are declared on Arduino, the array is also declared. The array is declared either globally or in the function with the specified data type of which values are to be stored in it. There are three different ways to declare the array which are:

Method 1: The arrays can be declared by mentioning their size and the values, for example, we declared an array with the name “arr1”, which has 5 values; 1,2,3,4, and 5. The declaration of the array will be:

int arr1[5]={1,2,3,4,5};

Method 2: The other way of declaring the array is without mentioning the size of an array, for example, we declare the above array, arr1, without mentioning its size so we can store more than 5 elements in the array:

char arr1[]={‘a’,’b’,’c’};

Method 3: The last way of declaring the array is without mentioning the size and values of the array like:

int arr1[];

How to access the elements of an array

We can access any particular value of the array by using its index number, for example, we have an array, arr1[5]={11,22,33,44,55}, in this array if we want to access and print value “33” which is on index number “2”, we will use array name with index number in the square brackets “[]”.

We will consider some practical examples of using the array.

Example 1: We will declare three arrays using the int, string, and char data types. Then will display these arrays on the serial monitor:

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

int a[5]={ 1, 2, 3, 4, 5};

Serial.print("The elements of array a are: ");

for(int i=0; i<5; i++){

Serial.print(a[i]);

}

Serial.println();

String b[3]={ "Resistor ", "Capacitor ", "LED" };

Serial.print("The elements of array b are: ");

for(int i=0; i<3; i++){

Serial.print(b[i]);

}

Serial.println();

char c[]={ 'a','b','c','d'};

Serial.print("The elements of array c are: ");

for(int i=0; i<4; i++){

Serial.print(c[i]);

}

Serial.println();

}

void loop(){

}

In the above code, we have declared an array a[] of integer data types by defining its size five and values. Then printed this array using a for loop. Similarly, we have declared a string array b[] without its size three and inserted some values. And in the end, we declared a char array without any size but gave the values. All these arrays are printed on the serial monitor output using the serial communication at a baud rate of 9600.

Example 2: In this example, we will access a particular value of the array using the code:

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

int a[5]={ 1, 2, 3, 4, 5};

Serial.print("The element of array at a[0] is: ");

Serial.print(a[0]);

}

void loop(){

}

In the above code, we declared an integer array, a[5], and stored some values in the array and then we accessed the 1st element of the array by using the index of the first element that is always 0. And also displayed the output on the serial monitor output by serial communication.

Conclusion

In Arduino, the arrays are the data structures that are used to store the multiple values of the same data types. The arrays are contiguous memory allocations where the values. In this write-up, we have discussed the arrays by defining their structure and declaration methods. We also explained examples so that the use of arrays in Arduino can be understood easily.

About the author

Hammad Zahid

I'm an Engineering graduate and my passion for IT has brought me to Linux. Now here I'm learning and sharing my knowledge with the world.