Serial/UART Communication Between Two Arduino Boards

In this tutorial, we will perform UART or serial communication between two Arduino boards using UART software library of Arduino IDE. To debug and program Arduino using a USB port, the serial port which is known as Universal Asynchronous Receiver/Transmitter (UART) Communication is used. For most sensors and systems, the main communication method is considered to be UART. In order to share workload, information and perform different tasks; sometimes communication between to Arduino is necessary.

Communication between two Arduino boards using Software Serial

We will use a serial software library which is available in Arduino IDE. But the questions why we want to use the software UART library of Arduino when Arduino Uno has one UART port available on D0 and D1 pins.  Because, Arduino only have one UART port and sometimes in embedded projects, we require more than one UART communication port to interface different sensors and modules such as GSM, GPS, Bluetooth, Xbee, etc. 

Arduino Serial UART Pins

The other important reason to use the uart software library is that the UART port of Arduino is used by the onboard USB connection. This USB connection transfers data to the Arduino IDE serial monitor using Rx and Tx pins. 

Although Arduino Mega has up to four serial communication ports. But, maximum Ardiuno compatible boards do not have multiple serial ports. In this case, a specially implemented software library is used to simulate UART communication behavior on other digital input output pins of Arduino. It is very easy to simulate serial UART communication but it should be remembered that it does not have any dedicated hardware and it will utilize the resources of already given Arduino Board like memory and execution time. However, all the functions which are usually available in hardware serial ports can be simulated and utilized using software serial ports. 

Arduino SoftwareSerial Library

Arduino IDE has a built-in software serial library which allows use to perform serial communication using other digital input-output pins. By using SoftwareSerial library, we can communicate with multiple devices or sensors over the UART interface. 

To use this library, first include the header file of software serial library using this line:

#include <SoftwareSerial.h>

After that create an instant  or object of the software serial library with a name of your own choice. For example, this line creates an object with the name of UART0. As an argument, we pass the name of digital pins which we want to use for serial communication. First argument is a receiver pin(RX) and the second argument is a transmit pin (TX). Hence, this line sets pin2 as a Rx pin and pin3 as a Tx pin for UART0. 

SoftwareSerial UART0(2, 3); // RX, TX

Connection Diagram Between Two Arduino Boards via Serial Pins

Following figure shows a connection diagram between two Arduino boards using digital pins to simulate a UART communication port using a pre-built library.

Connection diagram between Two Arduio boards for serial communication

Consider we are using digital pin 11 and pin 10 as Tx and Rx for both Arduino boards. 

  • Connect virtual pin Tx of first Arduino board with pin Rx of second Arduino board.
  • Connect virtual pin Rx of first Arduino board with pin Tx of second Arduino board.
  • Virtual Tx pin of Arduino 1 transmit data to real buffer of receiver Arduino 2 and this data is written on virtual Tx pin of receiver Arduino 2 which is than shown on virtual terminal named as “Received Data” 
  •  In order to monitor the status of transmitter and receiver Arduino, virtual terminals have been used.
  • Connect the GND pin of both Arduino board together (if using real Arduino Boards)
  • In case both Arduinos are not powered up using USB port than it is necessary to connect 5V pins of both Arduinos and power up one Arduino with USB port. (if using real Arduino Boards)

Serial Communication Code

The computer sends commands to Master Arduino, it is written on soft serial pins as defined in master code then it travels to the second Arduino through hardware connections made in pic 1 and it is communicated with Slave Arduino.

Code for Master Arduino


#include <SoftwareSerial.h>
SoftwareSerial softSerial(10, 11);

void setup()  
{
  softSerial.begin(9600);
} 
void loop()  
{ 
  softSerial.write("UART Communication");
  delay (100);
}

Description of Code for Master Arduino

Now lets see how above code works.

 First includes the software serial library using its #include files. This line includes the SoftwareSerial.h library.

#include <SoftwareSerial.h>

A serial object named as “softSerial” is declared here. Pin 10 will be used as Rx pin and pin 11 will be Tx pin. Using this “softSerial” object all functions of a normal serial connection can be used such as read, write and so on. 

SoftwareSerial softSerial (10, 11);

Here the baud rate of 9600 is being set for the softSerial object defined above. One important point to note here is that the baud rate of both Arduino boards such as master and slave should be same. 

softSerial.begin (9600);   

Here the statement “UART Communication” is being written repeatedly with a time delay of 0.1 second on Tx pin 11 of Arduino 1 for transmitting it to other Arduino.

softSerial.write("UART Communication");

Now add a delay of 100ms and this Arduino master will keep sending a string “UART Communication” on the transmit pin after every 100ms. 

delay(100);

Code for Slave Arduino

#include <SoftwareSerial.h>
SoftwareSerial softSerial(10, 11);
char ip;
void setup()  
{
  Serial.begin(9600);
  softSerial.begin(9600);
} 
void loop()  
{ 
  if (Serial.available())
  {
    ip=Serial.read();
    softSerial.print(ip);
  }
}

Description of Code for Slave Arduino

  This line includes the SoftwareSerial.h library.

#include <SoftwareSerial.h>

SoftwareSerial softSerial () creates a serial object named as “softSerial” is declared here. Pin 10 will be used as Rx pin and pin 11 will be Tx pin. Using this “softSerial” object all functions of a normal serial connection can be used such as read, write and so on.

SoftwareSerial softSerial (10, 11);

 After that, this line sets the baud to 9600 for soft serial and serial communication.

softSerial.begin(9600);

Then presence of data is checked in buffer. if some data is available, it is stored in “ip” variable and then written on softserial transmit pin which is pin 11 of Arduino.

ip=Serial.read();
softSerial.print(ip);

Proteus Simulation

Figure above shows the circuit diagram for simulating this communication.

Below figure shows the data being sent by Master device through Tx pin 11 of Arduino 1.

Connection diagram between Two Arduio boards for serial communication proteus simulation

Below figure shows the data sent by Master device and data received by Slave device at pin 11 of Arduino 2.

Connection diagram between Two Arduio boards for serial communication proteus simulation output

So data is successfully being transmitted using serial communication and soft serial library of Arduino.

Three most important points should be kept in mind while performing simulated serial communication.

  • Tx pin of one Arduino board should be connected to Rx of other Arduino board. If opposite connections are made, Arduino board might get damage.
  • Both Arduino boards must share same ground connection from sup
  • Also, both Arduino boards should operate at same speed which means baud rate of both board should be same.

Limitations of Software Serial Library

While using software serial communication following limitations should be kept in mind for successfully transmitting and receiving required data.

  1. Useable Pins

Every pin on the Arduino Board cannot be used as a Tx and Rx pin. For Tx generally any of the available digital pins can be used but for Rx only pins which are interrupt enabled can be used.

  •  For Arduino Micro and Leonardo pin 8, 9, 10, 11, 14, 15, 16 can be used as Tx and Rx.
  • On the other hand, for Arduino Mega pin 10, 11, 12, 13, 50, 51, 52, 53, 62, 63, 64, 65, 66, 67, 68, 69 can be used as TX and Rx.
  1. Interference

Simulated software serial communication uses resources of the same hardware. It uses the same timer as used by some other libraries. So some other functions can get effected if simulated serial port is used. The best known interference is with Servo Library.

  1. Multiple Software Connection:

Using this library, we can have multiple software simulated serial ports. But in this case only one port can receive data at one time. This can cause data loss. However, another software serial library is available which resolves this issue.

Related UART Communication Tutorials:

Related Projects which used Arduino Serial communication:

Leave a Comment