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.