ESP32 — Arrays and Strings
Arrays and strings are very important when working with ESP32 because IoT projects often need to store multiple sensor values, device states, Wi-Fi names, messages, IDs, etc.
1. What is an Array?
An array is a collection of multiple values of the same data type stored under one variable name.
Instead of:
int sensor1 = 10; int sensor2 = 20; int sensor3 = 30; int sensor4 = 40;
You can use:
int sensors[4] = {10, 20, 30, 40};
Think of an array like a row of boxes:
Array: sensors Index: 0 1 2 3 ┌─────┬─────┬─────┬─────┐ Value: │ 10 │ 20 │ 30 │ 40 │ └─────┴─────┴─────┴─────┘
Important
Array indexing starts from 0, not 1.
sensors[0] // 10 sensors[1] // 20 sensors[2] // 30 sensors[3] // 40
2. Creating an Array
Method 1 — Declare and initialize
int numbers[5] = {10, 20, 30, 40, 50};
Here:
int→ data typenumbers→ array name[5]→ 5 elements{10,20,30,40,50}→ values
Method 2 — Let compiler count the size
int numbers[] = {10, 20, 30, 40, 50};
The compiler automatically understands that there are 5 elements.
Method 3 — Create empty array
int numbers[5];
You can later assign values:
numbers[0] = 10; numbers[1] = 20; numbers[2] = 30;
3. Accessing Array Values
int temperature[] = {25, 27, 30, 28}; void setup() { Serial.begin(115200); Serial.println(temperature[0]); Serial.println(temperature[1]); Serial.println(temperature[2]); Serial.println(temperature[3]); } void loop() { }
Output:
25 27 30 28
4. Changing an Array Value
You can modify individual elements.
int numbers[] = {10, 20, 30}; numbers[1] = 100;
Now:
Index: 0 1 2 Value: 10 100 30
5. Arrays + Loops
This is where arrays become very useful.
Instead of writing:
Serial.println(numbers[0]); Serial.println(numbers[1]); Serial.println(numbers[2]); Serial.println(numbers[3]); Serial.println(numbers[4]);
Use a loop:
int numbers[] = {10, 20, 30, 40, 50}; void setup() { Serial.begin(115200); for (int i = 0; i < 5; i++) { Serial.println(numbers[i]); } } void loop() { }
Output:
10 20 30 40 50
How it works
i = 0 → numbers[0] i = 1 → numbers[1] i = 2 → numbers[2] i = 3 → numbers[3] i = 4 → numbers[4]
6. ESP32 Practical Example — Multiple Sensors
Suppose you have 4 analog sensors:
int sensorPins[] = {32, 33, 34, 35};
You can read all of them using a loop:
void setup() { Serial.begin(115200); } void loop() { for (int i = 0; i < 4; i++) { int value = analogRead(sensorPins[i]); Serial.print("Sensor "); Serial.print(i); Serial.print(": "); Serial.println(value); } delay(1000); }
This is much cleaner than writing four separate analogRead() statements.
7. Array of GPIO Pins
This is extremely useful for LEDs.
int ledPins[] = {13, 14, 25, 26};
Turn all LEDs ON:
void setup() { for (int i = 0; i < 4; i++) { pinMode(ledPins[i], OUTPUT); } } void loop() { for (int i = 0; i < 4; i++) { digitalWrite(ledPins[i], HIGH); } }
Turn all OFF:
for (int i = 0; i < 4; i++) { digitalWrite(ledPins[i], LOW); }
8. Array of Device States
You can also store ON/OFF states.
bool deviceState[] = { true, false, true, false };
For example:
Light 1 → ON Light 2 → OFF Fan → ON Pump → OFF
9. Two-Dimensional Arrays
An array can have rows and columns.
For example:
int matrix[2][3] = { {1, 2, 3}, {4, 5, 6} };
Visual representation:
Col0 Col1 Col2 ┌────┬────┬────┐ Row0 │ 1 │ 2 │ 3 │ ├────┼────┼────┤ Row1 │ 4 │ 5 │ 6 │ └────┴────┴────┘
Access:
matrix[0][0] // 1 matrix[0][1] // 2 matrix[1][2] // 6
10. What is a String?
A string is a sequence of characters.
Example:
Hello ESP32 Uday Smart Home
In Arduino/ESP32 programming, you commonly encounter two ways of storing text:
Arduino String
String name = "ESP32";
Character array
char name[] = "ESP32";
11. Arduino String
For beginners, the String class is easier.
String name = "ESP32";
Print it:
Serial.println(name);
Output:
ESP32
12. Combining Strings
You can combine text using +.
String name = "ESP32"; String message = "Hello " + name; Serial.println(message);
Output:
Hello ESP32
Another example:
String temperature = "28"; String message = "Temperature: " + temperature + " C"; Serial.println(message);
Output:
Temperature: 28 C
This is very useful for displaying sensor information.
13. String + Sensor Value
For example:
int temperature = 28; String message = "Temperature: " + String(temperature) + " C"; Serial.println(message);
Output:
Temperature: 28 C
For a smart-home project:
int temperature = 28; int humidity = 65; String message = "Temperature: " + String(temperature) + " C, Humidity: " + String(humidity) + "%"; Serial.println(message);
Output:
Temperature: 28 C, Humidity: 65%
14. Useful String Functions
.length()
Returns the number of characters.
String name = "ESP32"; Serial.println(name.length());
Output:
5
.toUpperCase()
String name = "esp32"; name.toUpperCase(); Serial.println(name);
Output:
ESP32
.toLowerCase()
String name = "ESP32"; name.toLowerCase(); Serial.println(name);
Output:
esp32
.indexOf()
Finds a character or text.
String message = "Hello ESP32"; int position = message.indexOf("ESP32"); Serial.println(position);
.substring()
Extracts part of a string.
String message = "Hello ESP32"; String part = message.substring(6); Serial.println(part);
Output:
ESP32
15. Comparing Strings
You can compare strings using:
String command = "ON"; if (command == "ON") { Serial.println("Light ON"); }
Example:
String command = "OFF"; if (command == "ON") { Serial.println("Light ON"); } else if (command == "OFF") { Serial.println("Light OFF"); }
This is very useful when receiving commands from:
- Web server
- Bluetooth
- Serial Monitor
- MQTT
- Wi-Fi applications
16. Character Array (char[])
A string can also be stored as characters:
char name[] = "ESP32";
Internally:
E S P 3 2 \0
The \0 is called the null terminator.
It tells the program:
"The string ends here."
Therefore "ESP32" actually requires 6 character positions, not 5.
char name[6] = "ESP32";
17. String vs char[]
Arduino String
String name = "ESP32";
Easy to use.
Character array
char name[] = "ESP32";
More traditional C/C++ style and can be useful when working with APIs, libraries, buffers, and lower-level memory handling.
For beginner ESP32 projects:
String
is usually easier.
But as your projects become larger, understanding char[] and memory becomes important.
18. Array of Strings
You can store multiple names in an array.
String devices[] = { "Light", "Fan", "Door", "Pump" };
Then:
for (int i = 0; i < 4; i++) { Serial.println(devices[i]); }
Output:
Light Fan Door Pump
19. Practical Smart Home Example
Suppose your ESP32 has:
Light Fan Door Security
We can store their names:
String devices[] = { "Light", "Fan", "Door", "Security" };
And their states:
bool states[] = { true, false, true, false };
Display them:
void setup() { Serial.begin(115200); String devices[] = { "Light", "Fan", "Door", "Security" }; bool states[] = { true, false, true, false }; for (int i = 0; i < 4; i++) { Serial.print(devices[i]); Serial.print(": "); if (states[i]) { Serial.println("ON"); } else { Serial.println("OFF"); } } } void loop() { }
Output:
Light: ON Fan: OFF Door: ON Security: OFF
This concept can later be used in an ESP32 web control dashboard.
20. Common Mistake — Wrong Index
If you have:
int numbers[5] = {10,20,30,40,50};
Valid indexes are:
0 1 2 3 4
This is wrong:
numbers[5]
Because there is no index 5.
Remember:
Array size = 5 First index = 0 Last index = size - 1 = 4
21. Common Mistake — Array Size
Don't accidentally access outside the array:
int ledPins[4] = {13, 14, 25, 26}; for (int i = 0; i <= 4; i++) {
❌ Wrong.
Use:
for (int i = 0; i < 4; i++) {
✅ Correct.
22. Important ESP32 Concept
Arrays become extremely useful when your project has many similar things.
For example:
Multiple LEDs Multiple sensors Multiple motors Multiple GPIO pins Multiple devices Multiple Wi-Fi clients Multiple readings Multiple commands
Instead of creating many variables:
int led1; int led2; int led3; int led4;
Use:
int leds[] = {13, 14, 25, 26};
Then control them using loops.
Quick Summary
Arrays
int numbers[] = {10, 20, 30};
Access:
numbers[0] numbers[1] numbers[2]
Loop:
for (int i = 0; i < 3; i++) { Serial.println(numbers[i]); }
Strings
String name = "ESP32";
Combine:
String message = "Hello " + name;
Length:
name.length();
Compare:
if (name == "ESP32")
Character array:
char name[] = "ESP32";
🧠 The Big Picture
ESP32 PROGRAMMING │ ┌─────────────┴─────────────┐ │ │ ARRAYS STRINGS │ │ Multiple values Text/data │ │ ┌──────┼──────┐ ┌──────┼──────┐ │ │ │ │ │ │ Sensors LEDs GPIO WiFi Commands Messages │ │ │ │ │ │ └──────┴──────┘ └──────┴──────┘ │ │ └────────── ESP32 ──────────┘