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 P11 – Serial Communication and User Input

🎯 Learning Objectives

After completing this lesson, students will be able to:

βœ… Understand serial communication in detail

βœ… Understand communication between Arduino and Computer

βœ… Send data from Arduino to Serial Monitor

βœ… Receive data from Serial Monitor

βœ… Use Serial.begin()

βœ… Use Serial.print() and Serial.println()

βœ… Use Serial.available()

βœ… Use Serial.read()

βœ… Build interactive Arduino programs


1. Introduction

In previous lessons, we used Serial Monitor to display messages.

Example:

Serial.println("Hello");

But Serial Communication is much more powerful.

It allows:

  • Arduino β†’ Computer Communication
  • Computer β†’ Arduino Communication

This creates two-way communication.

Just like chatting on WhatsApp:

You send messages

↓

Other person replies

Similarly:

Computer and Arduino can exchange information.


2. What is Serial Communication?

Serial Communication is the process of sending data one bit at a time between two devices.

Example:

Computer

↔

Arduino

Data is transmitted through:

  • USB Cable
  • TX Pin
  • RX Pin

3. Why Serial Communication is Important

Without Serial Communication:

❌ Difficult to debug programs

❌ Cannot monitor sensor values

❌ Cannot send commands

❌ Difficult to troubleshoot

With Serial Communication:

βœ… Easy debugging

βœ… Easy testing

βœ… Interactive projects

βœ… Data monitoring


4. Communication Path

When Arduino sends data:

Β 
Arduino
↓
TX Pin
↓
USB Converter
↓
Computer
↓
Serial Monitor
Β 

When Computer sends data:

Β 
Computer
↓
USB Converter
↓
RX Pin
↓
Arduino
Β 

5. Starting Serial Communication

Before communication begins:

Serial.begin(9600);

must be used.


What Does It Mean?

Serial.begin()

starts serial communication.


9600

is called:

Baud Rate


6. What is Baud Rate?

Baud Rate determines communication speed.

Common values:

9600
19200
38400
57600
115200

For beginners:

9600

is most commonly used.


Important Rule

Both Arduino and Serial Monitor must use the same baud rate.

Example:

Serial.begin(9600);

Serial Monitor:

Β 
9600
Β 

Otherwise strange characters may appear.


7. Sending Data to Serial Monitor

Example:

void setup()
{
   Serial.begin(9600);

   Serial.println("Welcome");
}

void loop()
{

}

Output:

Welcome

8. Serial.print()

Displays data without moving to a new line.

Example:

Serial.print("Arduino");
Serial.print(" Uno");

Output:

Arduino Uno

9. Serial.println()

Displays data and moves to the next line.

Example:

Serial.println("Arduino");
Serial.println("Uno");

Output:

Arduino
Uno

Difference

Serial.print()

Same line


Serial.println()

Next line


10. Displaying Variables

Example:

int age = 22;

Serial.println(age);

Output:

22

Displaying Text and Variables Together

Example:

int distance = 25;

Serial.print("Distance = ");
Serial.println(distance);

Output:

Distance = 25

11. Monitoring Sensor Values

Example:

int sensorValue;

void loop()
{
   sensorValue = analogRead(A0);

   Serial.println(sensorValue);

   delay(500);
}

Output:

350
420
510
700

Values change continuously.


Why This is Useful

Used for:

  • Sensor testing
  • Calibration
  • Debugging

12. Receiving Data from User

Until now:

Arduino sends data.

Now:

Arduino receives data.

Example:

User types:

Β 
HELLO
Β 

Arduino receives it.


Checking for Incoming Data

Arduino uses:

Serial.available()

What Does It Do?

Checks:

“Has data arrived?”

Returns:

0

No Data

or

Greater Than 0

Data Available


Example

if(Serial.available())
{
   Serial.println("Data Received");
}

13. Reading a Character

Arduino uses:

Serial.read();

to read one character.


Example

char data;

void loop()
{
if(Serial.available())
{
data = Serial.read();

Serial.println(data);
}
}

User types:

A

Output:

A

14. Character-Based Control

Example:

char command;

void loop()
{
   if(Serial.available())
   {
      command = Serial.read();

      if(command == 'A')
      {
         Serial.println("LED ON");
      }
   }
}

User enters:

A

Output:

LED ON

Real Applications

Bluetooth robots often use:

Β 
F = Forward
B = Backward
L = Left
R = Right
S = Stop
Β 

15. Reading Entire Strings

Instead of one character:

Use:

Serial.readString();

Example

String name;

void loop()
{
   if(Serial.available())
   {
      name = Serial.readString();

      Serial.println(name);
   }
}

Input:

Shiv

Output:

Shiv

16. Building a Simple Command System

Example:

String command;

void loop()
{
if(Serial.available())
{
command = Serial.readString();

if(command == "ON")
{
Serial.println("LED ON");
}

else if(command == "OFF")
{
Serial.println("LED OFF");
}
}
}

User Input

ON

Output:

LED ON

Input:

OFF

Output:

LED OFF

17. Serial Communication in Real Projects

Bluetooth Robot

Commands:

F
B
L
R
S

Home Automation

Commands:

LIGHT ON
LIGHT OFF

Smart Door Lock

Commands:

OPEN
CLOSE

IoT Systems

Data exchange between devices.


18. Common Beginner Mistakes

Mistake 1

Forgetting:

Serial.begin(9600);

Result:

No communication.


Mistake 2

Wrong baud rate.

Result:

Unreadable characters.


Mistake 3

Reading data without checking:

Serial.available()

Mistake 4

Confusing character and String.

Character:

'A'

String:

"Arduino"

19. Best Practices

βœ… Start communication in setup()

βœ… Match baud rates

βœ… Check Serial.available()

βœ… Use meaningful messages

βœ… Test communication frequently


20. Why Serial Communication is Important?

Serial Communication is one of the most important tools in Arduino programming because it helps:

  • Debug Programs
  • Monitor Sensors
  • Receive Commands
  • Control Robots
  • Build IoT Systems

Almost every professional Arduino developer uses Serial Communication while developing projects.


πŸ“Š Summary

In this lesson, we learned:

βœ… Serial Communication

βœ… Baud Rate

βœ… Serial.begin()

βœ… Serial.print()

βœ… Serial.println()

βœ… Serial.available()

βœ… Serial.read()

βœ… Serial.readString()

βœ… Sending and Receiving Data

Serial Communication acts as a bridge between Arduino and the outside world, allowing monitoring, debugging, and interactive control of projects.


πŸ“– Key Terms

Serial Communication

Data transfer between devices one bit at a time.

Baud Rate

Communication speed.

Serial Monitor

Tool used to view serial data.

Serial.begin()

Starts serial communication.

Serial.read()

Reads one character.

Serial.available()

Checks if data is available.

Serial.readString()

Reads a complete text string.


🎯 Quiz

1. Which function starts serial communication?

A. Serial.read()

B. Serial.begin() βœ…

C. Serial.print()

D. Serial.start()


2. What does 9600 represent?

A. Voltage

B. Pin Number

C. Baud Rate βœ…

D. Memory Size


3. Which function checks for incoming data?

A. Serial.print()

B. Serial.read()

C. Serial.available() βœ…

D. Serial.begin()


4. Which function reads one character?

A. Serial.println()

B. Serial.read() βœ…

C. Serial.begin()

D. Serial.write()


5. Why is Serial Communication useful?

A. Debugging βœ…

B. Monitoring Sensors βœ…

C. Receiving Commands βœ…

D. All of the Above βœ…


🏠 Assignment

Task 1

Create a program that displays your name every second in Serial Monitor.

Task 2

Create a program that reads a character from Serial Monitor and displays it back.

Task 3

Build a simple ON/OFF command system using Serial.readString().

Task 4

Display temperature sensor values in Serial Monitor.

Task 5

Research how Bluetooth-controlled robots use serial communication for receiving commands.

Β 
Β 
Scroll to Top