In this entry, we will learn the basic functioning of serial ports in Arduino. Serial ports are the main way to communicate an Arduino board with a computer or with another microcontroller.
Thanks to the serial port, we can, for example, move the mouse or simulate user typing on the keyboard, send emails with alerts, control a robot performing calculations on the computer, turn a device on or off from a web page via the Internet, or from a mobile application via Bluetooth.
There are endless possibilities where the use of the serial port is required!
Therefore, the serial port is a fundamental component of a large number of Arduino projects, and it is one of the basic elements we must learn to fully harness the potential of Arduino.
At the end of the entry, several example codes are attached, but first, it is advisable to briefly explain some theory about what a serial port is and some terms we will need to understand the functioning of the serial port correctly.
You can check the rest of the tutorials about the serial port in Arduino, (receive numbers, texts, comma-separated arrays, bytes, and many more) in the category Arduino serial port
What is a serial port?
A port is the generic name we use for interfaces, physical or virtual, that allow communication between two computers or devices.
A serial port sends information through a sequence of bits. For this, at least two connectors are needed to perform data communication, RX (reception) and TX (transmission) (however, there can be other conductors for voltage reference, clock synchronization, etc.).
In contrast, a parallel port would send information through multiple channels simultaneously. For this, they require a higher number of communication conductors, which vary depending on the type of port (there is also the possibility of additional conductors besides the communication ones).
Historically, both types of ports have coexisted in computers, with parallel ports being used in applications that required the transmission of larger volumes of data.
However, as processors became faster, serial ports gradually displaced parallel ports in most applications.
A conventional computer has several serial ports. The most well-known are the popular USB (Universal Serial Port) and the now almost forgotten RS-232 (the one for old mice).
However, within the scope of computing and automation, there are a large number of additional types of serial ports, such as RS-485, I2C, SPI, Serial ATA, PCI Express, Ethernet, or FireWire, among others.
Sometimes you will see serial ports referred to as UART. The UART (universally asynchronous receiver/transmitter) is a unit that certain processors incorporate, responsible for converting data into a sequence of bits and transmitting or receiving them at a specific speed.
On the other hand, you may also hear the term TTL (transistor-transistor logic). This means that communication occurs through variations in the signal between 0V and Vcc (where Vcc is typically 3.3V or 5V).
In contrast, other transmission systems use voltage variations from -Vcc to +Vcc (for example, typical RS-232 ports vary between -13V and 13V).
Before connecting two systems, we must verify that the employed voltages are compatible. If they are not, we will need a subsystem to adapt the signal levels, or we may damage one of the devices.
Arduino and the serial port
Practically all Arduino boards have at least one UART unit. The Arduino UNO and Mini Pro boards have a UART unit that operates at TTL level 0V / 5V, making them directly compatible with USB connection. Meanwhile, Arduino Mega and Arduino Due have 4 UART TTL 0V / 5V units.
The serial ports are physically connected to different pins of the Arduino board. Logically, while we are using the serial ports, we cannot use the pins associated with the serial port in use as digital inputs or outputs.
Arduino | Serial Port | Pin RX | Pin TX |
---|---|---|---|
Arduino UNO | Serial 0 | 0 (RX) | 1 (TX) |
Arduino Mini Pro | Serial 0 | 0 (RX) | 1 (TX) |
Arduino Mega | Serial 0 | 0 (RX) | 1 (TX) |
Arduino Mega | Serial 1 | 19 (RX) | 18 (TX) |
Arduino Mega | Serial 2 | 17 (RX) | 16 (TX) |
Arduino Mega | Serial 3 | 15 (RX) | 14 (TX) |
Arduino Due | Serial 0 | 0 (RX) | 1 (TX) |
Arduino Due | Serial 1 | 19 (RX) | 18 (TX) |
Arduino Due | Serial 2 | 17 (RX) | 16 (TX) |
Arduino Due | Serial 3 | 15 (RX) | 14 (TX) |
Many models of Arduino boards have a USB or Micro USB connector connected to one of the serial ports, which simplifies the connection process with a computer. However, some boards, such as the Mini Pro, do not have this connector, so the only way to connect to them is directly through the corresponding pins.
We should not get used to using the serial port if we really do not need to communicate with the computer. The libraries used for serial port use take up considerable space, and we should only use them if we really need them. Furthermore, it unnecessarily disables the associated digital pins.
Connecting Arduino to a computer
To connect via the serial port, it is only necessary to connect our Arduino board using the same port we use to program it. Next, we open the Arduino Standard IDE and click on “Serial Monitor” as shown in the image.
The serial port monitor is a small utility integrated into the Standard IDE that allows us to easily send and receive information through the serial port. Its use is very simple and has two areas, one that displays the received data and another for sending them. These areas are shown in the following image.
Despite its simplicity, this serial port monitor is sufficient for the examples in this entry and is very useful for quick tests or experiments.
Example codes
Receiving information from Arduino
In this first code, we will receive the value of a counter sent from the Arduino board. This value increments every second. We can see how values are received from the serial monitor.
int cont=0;
void setup(){
//initialize the serial port
Serial.begin(9600);
}
void loop(){
//Print the counter value
Serial.print("Counter: ");
Serial.println(cont);
//increment the counter and wait a second
cont++;
delay(1000);
}
Sending information to Arduino
In this example, we use the serial port to turn the integrated LED on or off on the Arduino board. To do this, we send a character to the Arduino board using the serial monitor. If we send ‘a’, the Arduino board turns off the LED, and if we send ‘b’, it turns it on.
int option;
int led = 13;
void setup(){
Serial.begin(9600);
pinMode(led, OUTPUT);
}
void loop(){
//if data is available, read it
if (Serial.available()>0){
//read the sent option
option=Serial.read();
if(option=='a') {
digitalWrite(led, LOW);
Serial.println("OFF");
}
if(option=='b') {
digitalWrite(led, HIGH);
Serial.println("ON");
}
}
}
Sending numerical values
Finally, in this example, we send a number between 1 and 9 through the serial monitor, and the Arduino board makes the integrated LED blink the indicated number of times. The code is similar to the previous one, but it should be noted that since the data is sent as ASCII characters, we must subtract the value ‘0’ from the received data to recover the sent numeric value.
int option;
int led = 13;
void setup(){
Serial.begin(9600);
pinMode(led, OUTPUT);
}
void loop(){
//if there is pending information
if (Serial.available()>0){
//read the option
char option = Serial.read();
//if the option is between '1' and '9'
if (option >= '1' && option <= '9')
{
//subtract the value '0' to get the sent number
option -= '0';
for(int i=0;i<option;i++){
digitalWrite(led, HIGH);
delay(100);
digitalWrite(led, LOW);
delay(200);
}
}
}
}
So far the basic examples. However, the serial port is not the only communication method in Arduino. Other important systems for communication between devices are the SPI bus and the I2C bus.
Update 10/11/2015: The new Arduino IDE v1.6.6 includes a tool to create graphs with values received via the serial port, called “Serial Plotter”. You can learn more about its use in the entry Easily create graphs with Arduino IDE V1.6.6.
Download the code
All the code from this entry is available for download on Github.