Lesson P9 – Strings and Text Handling in Arduino
🎯 Learning Objectives
After completing this lesson, students will be able to:
✅ Understand what Strings are
✅ Understand the difference between char and String
✅ Create and use String variables
✅ Display text using Serial Monitor
✅ Combine Strings
✅ Find String length
✅ Convert text to numbers
✅ Use Strings in Arduino projects
1. Introduction
Until now we have worked with:
- Numbers
- Variables
- Arrays
- Operators
But many Arduino projects also need to handle text.
Examples:
- Displaying messages on LCD
- Sending Bluetooth commands
- Showing sensor status
- Displaying names
- IoT notifications
For these tasks we use:
Strings
2. What is a String?
A String is a collection of characters stored together as text.
Example:
HELLO
contains:
H
E
L
L
O
Multiple characters combined form a String.
Real-Life Example
Examples of Strings:
Arduino
Shiv
Temperature High
Motor ON
Welcome
All of these are Strings.
3. Why Are Strings Important?
Many projects communicate using text.
Examples:
Bluetooth Robot
Command:
FORWARD
LCD Display
Message:
WELCOME
IoT System
Notification:
Temperature High
Without Strings, handling text would be difficult.
4. String Data Type
Arduino provides:
String
for storing text.
Example:
String name = "Shiv";
Here:
name
contains:
Shiv
5. Displaying a String
Example:
String name = "Shiv";
void setup()
{
Serial.begin(9600);
Serial.println(name);
}
void loop()
{
}
Output:
Shiv
6. Creating Multiple Strings
Example:
String city = "Ranchi";
String country = "India";
Output:
Serial.println(city);
Serial.println(country);
Result:
Ranchi
India
7. String vs Character
Students often confuse:
char
and
char
Character
Stores only one character.
Example:
char grade = 'A';
Stores:
A
only.
String
Stores multiple characters.
Example:
String name = "Shiv";
Stores:
Shiv
Comparison
| Data Type | Example |
|---|---|
| char | ‘A’ |
| String | “Arduino” |
Important Rule
Character uses:
'A'
Single Quotes
String uses:
"Arduino"
Double Quotes
8. Combining Strings
Combining text is called:
Concatenation
Example:
String firstName = "Shiv";
String lastName = "Sahu";
Combine:
String fullName = firstName + " " + lastName;
Output:
String fullName = firstName + " " + lastName;
Practical Example
String city = "Ranchi";
String message = "Welcome to " + city;
Output:
Welcome to Ranchi
9. Finding String Length
Arduino provides:
.length()
Example:
String name = "Arduino";
Serial.println(name.length());
Output:
7
Because:
A r d u i n o
contains 7 characters.
Applications
Useful for:
- Password checking
- Input validation
- Communication systems
10. Accessing Individual Characters
Example:
String name = "Arduino";
Characters:
A r d u i n o
0 1 2 3 4 5 6
Access:
Serial.println(name[0]);
Output:
A
Serial.println(name[3]);
Output:
u
11. Converting Text to Uppercase
Example:
String name = "arduino";
name.toUpperCase();
Serial.println(name);
Output:
ARDUINO
12. Converting Text to Lowercase
Example:
String name = "ARDUINO";
name.toLowerCase();
Serial.println(name);
Output:
arduino
13. Comparing Strings
Example:
String password = "1234";
Check:
if(password == "1234")
{
Serial.println("Access Granted");
}
Output:
Access Granted
Application
Used in:
- Password Systems
- Smart Locks
- Login Systems
14. Converting String to Integer
Sometimes data arrives as text.
Example:
"25"
But we need:
25
as a number.
Use:
.toInt()
Example:
Now:
number = 25
Practical Example
Bluetooth receives:
"100"
Convert to:
100
for motor speed.
15. Reading User Input from Serial Monitor
Example:
String name;
void setup()
{
Serial.begin(9600);
}
void loop()
{
if(Serial.available())
{
name = Serial.readString();
Serial.println(name);
}
}
Whatever user types is displayed back.
Applications
- Bluetooth Commands
- User Input
- Serial Communication
16. Real Arduino Project Examples
Smart Door Lock
String password = "1234";
Compare entered password.
Bluetooth Robot
Commands:
FORWARD
BACKWARD
LEFT
RIGHT
STOP
Stored as Strings.
LCD Welcome Message
String msg = "Welcome";
Display on LCD.
IoT Notifications
String alert = "Gas Leakage Detected";
Send to mobile app.
17. Common Beginner Mistakes
Mistake 1
Using single quotes for Strings.
Wrong:
String name = 'Shiv';
Correct:
String name = "Shiv";
Mistake 2
Using double quotes for char.
Wrong:
char grade = "A";
Correct:
char grade = 'A';
Mistake 3
Ignoring case sensitivity.
Example:
"HELLO"
and
"hello"
are different.
Mistake 4
Creating very large Strings.
Can waste memory on Arduino Uno.
18. Best Practices
✅ Use meaningful names
userName
instead of:
x
✅ Keep Strings reasonably small
✅ Use comments
✅ Validate user input
✅ Test communication systems carefully
📊 Summary
In this lesson, we learned:
✅ What Strings are
✅ String declaration
✅ Displaying text
✅ String concatenation
✅ String length
✅ Character access
✅ String comparison
✅ Text-to-number conversion
Strings allow Arduino to handle text data, making them essential for communication, displays, user interfaces, Bluetooth systems, and IoT projects.
📖 Key Terms
String
A collection of characters forming text.
Character
A single letter, number, or symbol.
Concatenation
Combining multiple Strings.
Length
Number of characters in a String.
toUpperCase()
Converts text to uppercase.
toLowerCase()
Converts text to lowercase.
toInt()
Converts text into an integer.
🎯 Quiz
1. What is a String?
A. Number
B. Collection of characters ✅
C. Sensor
D. Function
2. Which quotation marks are used for Strings?
A. Single Quotes
B. Double Quotes ✅
C. Curly Braces
D. Parentheses
3. Which function returns String length?
A. size()
B. count()
C. length() ✅
D. strlen()
4. What does .toInt() do?
A. Converts number to text
B. Converts text to integer ✅
C. Converts to uppercase
D. Compares Strings
5. Which is a valid String?
A. 'Arduino'
B. "Arduino" ✅
C. Arduino
D. {Arduino}
🏠 Assignment
Task 1
Create a String variable containing your name and display it in Serial Monitor.
Task 2
Create two Strings and combine them into one message.
Task 3
Display the length of a String.
Task 4
Create a simple password checking program using Strings.
Task 5
Research how Bluetooth-controlled robots use Strings for receiving commands such as FORWARD, LEFT, RIGHT, and STOP.