Introduction
Serial communication is a method of sending data between devices serially, one bit at a time. This is typically done using the RX (Receive) and TX (Transmit) pins.
In this lesson, we will learn how to use serial communication in Arduino and how to send and receive data between Arduino and a computer or other devices.
What is Serial Communication?
Serial communication is a method of transferring data between devices using electrical signals. Data is sent bit by bit over a single wire (or a pair of wires).
In Arduino, the UART (Universal Asynchronous Receiver/Transmitter) port is used for serial communication. Most Arduino boards have at least one serial port.
Some of the most common types of serial communication include:
- UART: Used in Arduino to communicate with a computer via USB.
- I2C: Used to communicate with multiple devices using only two wires.
- SPI: Used for fast communication with peripheral devices.
RX and TX Pins
In Arduino, the RX and TX pins are used for serial communication:
- RX (Receive): Receives data from another device.
- TX (Transmit): Sends data to another device.
When using the USB port to communicate with a computer, the serial signals are converted to USB via a controller chip.
Simple Example: Sending Data to the Computer
In this example, we will send a counter value from Arduino to the computer via serial communication.
int counter = 0;
void setup() {
Serial.begin(9600); // Start serial communication at 9600 baud rate
}
void loop() {
Serial.print("Counter: ");
Serial.println(counter); // Send the counter value
counter++; // Increment the counter
delay(1000); // Wait for one second
}
You can open the "Serial Monitor" in Arduino IDE to see the sent data.
Example: Receiving Data from the Computer
In this example, we will receive data from the computer and use it to control the built-in LED on the Arduino.
int ledPin = 13; // Built-in LED on pin 13
void setup() {
Serial.begin(9600); // Start serial communication
pinMode(ledPin, OUTPUT); // Set the LED pin as output
}
void loop() {
if (Serial.available() > 0) { // If data is available
char command = Serial.read(); // Read the command
if (command == '1') {
digitalWrite(ledPin, HIGH); // Turn on the LED
Serial.println("LED ON");
} else if (command == '0') {
digitalWrite(ledPin, LOW); // Turn off the LED
Serial.println("LED OFF");
}
}
}
You can send "1" or "0" via Serial Monitor to control the LED.
Example: Sending Numeric Values
In this example, we will send a number from the computer to Arduino to make the LED blink a certain number of times.
int ledPin = 13; // Built-in LED on pin 13
void setup() {
Serial.begin(9600); // Start serial communication
pinMode(ledPin, OUTPUT); // Set the LED pin as output
}
void loop() {
if (Serial.available() > 0) { // If data is available
char number = Serial.read(); // Read the number
if (number >= '1' && number <= '9') {
int times = number - '0'; // Convert the number to an integer
for (int i = 0; i < times; i++) {
digitalWrite(ledPin, HIGH);
delay(200);
digitalWrite(ledPin, LOW);
delay(200);
}
}
}
}
You can send a number from 1 to 9 via Serial Monitor to make the LED blink that many times.