What are arrays in Arduino Programming??

What is an array in programming, how to use an array in programming, and how do arrays works are some questions while programming an Arduino.

Hello guys! Welcome back to my blog today we are going to see about Arrays in Arduino.

What are Arrays??

Arrays are a collection of elements, and objects just like variables are used to store value, our Arrays are used to store lists. let's take a beautiful example of the bus to understand the array concept. we have a bus which consists of serval seats and each within bus has its seat number which helps passenger to recognize their seats. so compare a bus with an array and each seat within the bus is an element in the array and the seat number is the index number except in the array the element number starts with 0 (zero). To understand better we will take another example of array lets took an example there is a room where you have a sudden visit of guests in your home and your room is filled with toys scattered everywhere in the room. You need to clean up the mess so you put everything in a box. Now the room looks clean. Here you use boxes to store your things and toys are just like elements. Just like inbox where you can keep (add) or remove your toys similarly in the array, you can also do the same (add, remove or replace) except the count of elements will start from zero not from 1 (one).

Arduino array example

Now you have understood what is array now we will see how to use an array. You can create an array for int, character, and name.

Array creates a list of elements

String Fruits[5] = {apple, mango, banana, pineapple, kiwi}

                      Or

char Fruits[] = {apple, mango, banana, pineapple, kiwi}

Arduino string array

We can initialize Array in two ways. The first one tells the number of elements our list or array will have. Both lists we have created are the same.

As we know how to create arrays & now to access elements inside the array, now we will use for-loops to access elements in a loop.

Note - the index number of the array always starts with 0 (zero).

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

{

Serial.print (Fruits[i]);

}

Here we want to print all elements in an array name fruits, so we used for loop to get 0 to 4 index numbers. As the array start with 0 now each time values of I get incremented until their value becomes greater than 5.

For i = 0
Fruits[0] = apple
For i = 1
Fruits[1] = mango
For i = 2
Fruits[2] = banana
For i = 3
Fruits[3] = pineapple
For i = 4
Fruits[4] = kiwi

To know the total character length or a number of bytes stored in a variable of the array we have sizeof() function, sizeof() returns the total length. These functions take the array name or variable name as a parameter. In the above example, we have the array name Fruits having the character or byte length of 30 as the array starts with zero.

sizeof(Fruits);

Now, We will see an example of the array for glowing LED using for-loop, but what is for loop we will discuss in the next upcoming chapter.

Arduino array LED

Arduino led array


Connect the negative terminal of all LEDs to the GND pin of the Arduino
Connect positive pin of LED 1 to PIN no 2 of Arduino
Connect positive pin of LED 2 to PIN no 3 of Arduino
Connect positive pin of LED 3 to PIN no 4 of Arduino
Connect positive pin of LED 4 to PIN no 5 of Arduino
Connect the positive pin of LED 5 to PIN no 6 of Arduino
Connect positive pin of LED 6 to PIN no 7 of Arduino
Connect positive pin of LED 7 to PIN no 8 of Arduino
Connect positive pin of LED 8 to PIN no 9 of Arduino
Connect positive pin of LED 9 to PIN no 10 of Arduino
Connect the positive pin of LED 10 to PIN no 11 of Arduino

Code - 

int ledpins[] = {2,3,4,5,6,7,8,9,10,11};
int total_ledpin = 10;
void setup() 
{
 Serial.begin(115200);
 for (int i=0;i<=total_ledpin;i++)
 {
  pinMode(ledpins[i],OUTPUT);
  }
}
void loop() 
{
  
for(int i=0; i<=total_ledpin; i++)
{
Serial.println(ledpins[i]);
digitalWrite(ledpins[i],HIGH);
delay(1000);
}
for(int i=total_ledpin; i>=0; i--)
{
Serial.println(ledpins[i]);
digitalWrite(ledpins[i],LOW);
delay(1000);
}
}

Code explanation -

int ledpins[] = {2,3,4,5,6,7,8,9,10,11};
int total_ledpin = 10;

Here we have created an array and assigned a name of the array as ledpins. Here we are using a total of 10 pins of Arduino to connect 10 LEDs. We are connecting LED to Arduino pin numbers 2, 3, 4, 5, 6, 7, 8, 9, 10, 11. We have declared the total LED pin count as 10 as in this example we are using 10 LED.

void setup() 
{
 Serial.begin(115200);
 for (int i=0;i<=total_ledpin;i++)
 {
  pinMode(ledpins[i],OUTPUT);
  }
}

In the void setup, we have configured the baud rate to 115200. We configure pins as INPUT or OUTPUT. We have to declare the LED pin as output. Here we have 10 LEDs so instead of declaring each 10 LED pin as output. We will use for loop to declare LED pins. We will discuss for loop in detail. In for loop, we initialize the value of i as 0 first and later increment till the value of i reaches the total length of LED which we have declared as 10 (i = 0 to i =10). As we know

ledpins[i],

for i = 0 ledpins[0] = 2,

for i = 1 ledpins[1] = 3,

 for i = 2 ledpins[2] = 4,

for i = 3 ledpins[3] = 5,

for i = 4 ledpins[4] = 6,

for i = 5 ledpins[5] = 7,

 for i = 6 ledpins[6] = 8,

for i = 7 ledpins[7] = 9,

 for i = 8 ledpins[8] = 10,

for i = 9 ledpins[9] = 11   

So using for loop we have declared each pin of Arduino as OUTPUT

void loop() 
{
  
for(int i=0; i<=total_ledpin; i++)
{
Serial.println(ledpins[i]);
digitalWrite(ledpins[i],HIGH);
delay(1000);
}
for(int i=total_ledpin; i>=0; i--)
{
Serial.println(ledpins[i]);
digitalWrite(ledpins[i],LOW);
delay(1000);
}
}

In the void loop, we continuously turn all LEDs ON in a sequential manner and turn OFF LEDs in opposite direction. As above in void setup, as we use for loop to declare all LEDS pins as OUTPUT, the same we will do in the void loop to turn LED ON and turn LED OFF using the digitalWrite() function. The value of i corresponds to the index number of elements inside the array (LED pins). To turn LED ON we will increment the value of i with keyword HIGH inside digitalWrite() and to turn LED OFF we will decrement the value of i with keyword LOW inside digitalWrite(). 

Conclusion –

Today we will learn about arrays, arrays in Arduino, what is arrays, and how to use arrays in programming. we have seen an example of an array using multiple LEDs. arrays are most commonly used in each and every programing language. arrays contain a list of elements with its index number representing its element which helps them to access them easily.


"I hope you find this IOT blog very helpful to you. In the upcoming lesson, we will see about for loop till then bye. See you all in my next blog."  

Close Menu