Course Content
πŸ“˜ MODULE 9 – Automatic Dustbin
0/1
πŸ“˜ MODULE 10 – Obstacle Avoiding Robot
0/1
πŸ“˜ MODULE 11 – Edge Avoiding Robot
0/1
Arduino Hands-On Programming and Robotics Course

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.

Β 
Β 
Scroll to Top