Lesson P8 – Arrays in Arduino Programming
🎯 Learning Objectives
After completing this lesson, students will be able to:
✅ Understand what arrays are
✅ Understand why arrays are needed
✅ Create and use arrays
✅ Access array elements
✅ Use loops with arrays
✅ Store multiple values efficiently
✅ Apply arrays in real Arduino projects
1. Introduction
Suppose you want to store the marks of 5 students.
Without arrays:
int mark1 = 80;
int mark2 = 75;
int mark3 = 90;
int mark4 = 85;
int mark5 = 70;
This works for 5 values.
But what if you need:
- 50 values?
- 100 values?
- 1000 values?
Creating separate variables becomes difficult.
To solve this problem, programmers use:
Arrays
2. What is an Array?
An array is a collection of multiple values stored under a single variable name.
Think of an array as a row of lockers.
Example:
Locker 0 → 80
Locker 1 → 75
Locker 2 → 90
Locker 3 → 85
Locker 4 → 70
All lockers belong to one array.
Real-Life Example
Imagine a classroom attendance register.
Instead of creating a separate register for every student:
- Student 1
- Student 2
- Student 3
All names are stored in a single list.
Arrays work similarly.
3. Why Do We Need Arrays?
Arrays help:
✅ Store multiple values
✅ Save memory
✅ Simplify programs
✅ Work efficiently with loops
✅ Manage sensor readings
Without arrays, large projects become difficult to manage.
4. Declaring an Array
Syntax:
dataType arrayName[size];
Example:
int marks[5];
This creates an array capable of storing 5 integers.
5. Array Index
Every element in an array has a position called:
Index
Important:
Array indexing starts from:
0
Not 1.
Example
int marks[5];
Positions:
| Index | Element |
|---|---|
| 0 | First |
| 1 | Second |
| 2 | Third |
| 3 | Fourth |
| 4 | Fifth |
Visual Representation
Index:
0 1 2 3 4
Values:
80 75 90 85 70
6. Initializing an Array
Values can be assigned during creation.
Example:
int marks[5] = {80,75,90,85,70};
Now:
marks[0] = 80
marks[1] = 75
marks[2] = 90
marks[3] = 85
marks[4] = 70
7. Accessing Array Elements
Use index numbers.
Example:
Serial.println(marks[0]);
Output:
80
Serial.println(marks[3]);
Output:
85
8. Modifying Array Values
Array values can be changed.
Example:
marks[2] = 95;
Now:
marks[2] = 95
instead of:
90
9. Displaying Array Values
Example:
int marks[5] = {80,75,90,85,70};
void setup()
{
Serial.begin(9600);
Serial.println(marks[0]);
}
Output:
80
10. Using Arrays with Loops
Arrays become powerful when combined with loops.
Example:
int marks[5] = {80,75,90,85,70};
void setup()
{
Serial.begin(9600);
for(int i=0;i<5;i++)
{
Serial.println(marks[i]);
}
}
Output:
80
75
90
85
70
Why Use a Loop?
Without a loop:
Serial.println(marks[0]);
Serial.println(marks[1]);
Serial.println(marks[2]);
Serial.println(marks[3]);
Serial.println(marks[4]);
More code.
Less efficient.
11. Finding Sum of Array Elements
Example:
int marks[5]={80,75,90,85,70};
int sum=0;
for(int i=0;i<5;i++)
{
sum = sum + marks[i];
}
Result:
400
Practical Applications
- Sensor averaging
- Data logging
- Student marks
- Robot path storage
12. Finding Maximum Value
Example:
int values[5]={10,50,20,80,30};
int maximum=values[0];
for(int i=1;i<5;i++)
{
if(values[i] > maximum)
{
maximum = values[i];
}
}
Result:
80
13. Arrays and LEDs
Suppose 5 LEDs are connected:
| LED | Pin |
|---|---|
| LED1 | 2 |
| LED2 | 3 |
| LED3 | 4 |
| LED4 | 5 |
| LED5 | 6 |
Instead of creating separate variables:
int ledPins[5]={2,3,4,5,6};
Now LEDs can be controlled using loops.
Example
for(int i=0;i<5;i++)
{
digitalWrite(ledPins[i],HIGH);
}
All LEDs turn ON.
14. Arrays with Sensors
Example:
Multiple IR Sensors
int sensorPins[5]={A0,A1,A2,A3,A4};
Useful in:
- Line Follower Robots
- Smart Monitoring Systems
- Industrial Automation
15. Character Arrays
Arrays can store characters.
Example:
char grade[5]={'A','B','C','D','E'};
Output:
Serial.println(grade[0]);
Displays:
A
16. String Arrays
Example:
String names[3]={"Rahul","Shiv","Amit"};
Output:
Serial.println(names[1]);
Displays:
Shiv
17. Memory Considerations
Arrays use memory.
Example:
int values[100];
Consumes:
100 × 2 Bytes
=
200 Bytes
Arduino Uno has limited memory.
Therefore avoid unnecessarily large arrays.
18. Common Beginner Mistakes
Mistake 1
Array index starts from 0.
Wrong:
int values[100];
In a 5-element array.
Valid indexes:
0–4
Mistake 2
Accessing beyond array size.
Example:
marks[10]
when only 5 elements exist.
Can cause unexpected behavior.
Mistake 3
Forgetting loop limits.
Wrong:
for(int i=0;i<=5;i++)
Correct:
for(int i=0;i<5;i++)
19. Real-World Applications
Arrays are used in:
Line Follower Robots
Store multiple IR sensor values.
Weather Stations
Store temperature history.
LED Patterns
Store LED pin numbers.
Data Loggers
Store readings.
Automation Systems
Store device states.
20. Best Practices
✅ Use meaningful array names
sensorValues
instead of:
a
✅ Keep array size appropriate
✅ Always check index limits
✅ Use loops with arrays
✅ Comment large arrays
📊 Summary
In this lesson, we learned:
✅ What arrays are
✅ Array declaration
✅ Array initialization
✅ Array indexing
✅ Using arrays with loops
✅ Finding sums and maximum values
✅ LED and sensor arrays
Arrays help Arduino store and manage multiple related values efficiently, making programs shorter, cleaner, and more powerful.
📖 Key Terms
Array
A collection of multiple values stored under one variable name.
Index
Position of an element inside an array.
Element
Individual value stored in an array.
Array Size
Total number of elements.
Iteration
Repeating through array elements using loops.
🎯 Quiz
1. What is an array?
A. Function
B. Collection of values under one name ✅
C. Sensor
D. Operator
2. Array indexing starts from:
A. 1
B. 2
C. 0 ✅
D. 10
3. What is the index of the first element?
A. 0 ✅
B. 1
C. 2
D. 5
4. Which loop is commonly used with arrays?
A. if
B. switch
C. for ✅
D. else
5. In a 5-element array, the last valid index is:
A. 5
B. 4 ✅
C. 3
D. 6
🏠 Assignment
Task 1
Create an integer array containing 10 numbers and display them using a for loop.
Task 2
Find the sum of all elements in an array.
Task 3
Find the largest value in an array.
Task 4
Create an array containing 5 LED pin numbers.
Task 5
Research how arrays are used in line follower robots and explain their importance.