Language: EN

arduino-puerto-serie

Arduino Communication with Serial Port

Serial ports are the primary way to communicate an Arduino board with a computer. Thanks to the serial port, we can, for example, move the mouse or simulate user keyboard input, send emails with alerts, control a robot by performing calculations on the computer, turn a device on or off from a webpage over the Internet, or from a mobile application via Bluetooth.

There are endless possibilities in which the use of the serial port is required. Therefore, the serial port is a fundamental component of a large number of Arduino projects and is one of the basic elements that we must learn to unleash the full potential of Arduino.

In this post, we will learn the basic operation of serial ports in Arduino. Several example codes are attached at the end of the post, but first, it is convenient to briefly explain some theory about what a serial port is and some terms that we will need to understand the operation of the serial port correctly.

You can check the rest of the tutorials on the serial port in Arduino, (receive numbers, texts, comma-separated arrays, bytes, and many more) in the Arduino serial port category

What is the serial port?

A port is the generic name given to the interfaces, physical or virtual, that allow communication between two computers or devices.

A serial port sends information by means of a sequence of bits. For this, at least two connectors are needed to carry out the data communication, RX (reception) and TX (transmission). However, there may be other conductors for voltage reference, clock synchronization, and so on.

On the other hand, a parallel port sends information through multiple channels simultaneously. For this, it needs a greater number of communication conductors, which vary depending on the type of port. There is also the possibility of additional conductors in addition to the communication ones. puerto-serie Historically, both types of ports have coexisted in computers, employing parallel ports in applications that required the transmission of larger volumes of data. However, as processors became faster, serial ports progressively 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 almost forgotten RS-232 (for old mice). However, within the field of computing and automation, there are a large number of additional types of serial ports, such as RS-485, I2C, SPI, Serial Ata, Pcie Express, Ethernet, or FireWire, among others.

Sometimes you will see serial ports referred to as UARTs. The UART (universally asynchronous receiver/transmitter) is a unit that certain processors incorporate, responsible for converting the 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 is carried out by variations in the signal between 0V and Vcc (where Vcc is usually 3.3V or 5V). On the contrary, other transmission systems use voltage variations from -Vcc to +Vcc (for example, RS-232 ports typically vary between -13V to 13V).

Before connecting two systems, we must check that the voltages used are compatible. If they are not, we will need a subsystem that adapts 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 0V / 5V level, so they are directly compatible with the USB connection. On the other hand, the Arduino Mega and Arduino Due boards have 4 UART units at TTL 0V / 5V level.

The serial ports are physically connected to different pins of the Arduino board. Logically, while we use the serial ports, we cannot use the pins associated with the serial port in use as digital inputs or outputs.

In Arduino UNO and Mini Pro, the pins used are 0 (RX) and 1 (TX). In the case of Arduino Mega and Arduino Due, which have four serial ports, serial port 0 is connected to pins 0 (RX) and 1 (TX), serial port 1 to pins 19 (RX) and 18 (TX), serial port 2 to pins 17 (RX) and 16 (TX), and serial port 3 to pins 15 (RX) and 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 do not really need to communicate with the computer. The libraries used for serial port usage occupy a considerable size, and we should only use them if we really need them. Additionally, it unnecessarily disables the associated digital pins.

Connecting Arduino to a computer

To make the connection via the serial port, it is only necessary to connect our Arduino board using the same port that we use to program it. Then open the Arduino Standard IDE and click on the “Serial Monitor” as indicated in the image. arduino-serial-monitor-IDE1 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 it has two areas, one that shows the received data, and another to send it. These areas are shown in the following image. arduino-serial-monitor Despite its simplicity, this serial port monitor is sufficient for the examples in this post and is very useful for conducting 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 increases every second. We can see how the values are received from the serial monitor.

int cont=0;

void setup(){
  //start the serial port
  Serial.begin(9600);
}

void loop(){
  //Print the value of the counter
  Serial.print("Counter: ");
  Serial.println(cont);
  
  //increase the counter and wait for a second
  cont++;
  delay(1000);
}

Sending information to Arduino

In this example, we use the serial port to turn on or off the LED integrated in the Arduino board. To do this, we send a character to the Arduino board using the serial monitor. If ‘a’ is sent, the Arduino board turns off the LED, and if ‘b’ is sent, it turns it on.

int option;
int led = 13;

void setup(){
  Serial.begin(9600);
  pinMode(led, OUTPUT); 
}

void loop(){
  //if there is data 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 from 1 to 9 through the serial monitor, and the Arduino board blinks the integrated LED the number of times indicated. The code is similar to the previous one, but note that since the data sent is 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 obtain the sent number
      option -= '0';
      for(int i=0;i<option;i++){
         digitalWrite(led, HIGH);
         delay(100);
         digitalWrite(led, LOW);
         delay(200);
      }
    }
  }
}

These are the basic examples. However, the serial port is not the only form of communication in Arduino. Other important communication systems between devices are the SPI bus and the I2C bus.

In future posts, we will see more complicated examples to create complete interfaces and how to transmit or receive data using Java, C# (communicate Arduino with C# and the serial port), Python (control Arduino with Python and PySerial), through the Processing environment, or directly from the console in Windows or Linux.

Update 10/11/2015: The new Arduino IDE v1.6.6 incorporates a tool to create graphs with the values received by the serial port, called “Serial Plotter”. You can see more about its use in the post Easily Create Graphs with Arduino IDE V1.6.6.

Download the code

All the code from this post is available for download on Github. github-full