ad space

Serial communication between two Arduinos

 

Arduino Uno has one UART serial communication port which is used by the Arduino USB port to communicate with PC for uploading code and serial communication with PC via USB. So if we want communication between two Arduino Uno to share data this serial port cannot be used. Other Arduino board like Arduino Mega or other has upto four UART port and therefore one of the UART port can be used.

Arduino Uno board is based on ATmega328p microcontroller. The following picture shows how the Arduino Uno UART module is connected to the USB.

However, serial communication with Arduino Uno can be done using serial port library. The serial library emulates two pins as serial port. Here we show how we can perform serial communication between two Arduino Uno using such serial library. 

To illustrate we will setup one Arduino Uno as Master that is connected to the computer. Using Arduino IDE serial monitor on the computer, command which can be either y or n is sent to the master Arduino. The same command in turn is sent from the master Arduino to the slave Arduino Uno over serial link. The receiver slave Arduino interprets the received command. If the received command is y then the built in LED on pin 13 on the slave Arduino is turned on and if the received command is n then the LED is turned off.

The following shows circuit wiring diagram between two Arduino Uno.

The serial communication port Universal Asynchronous Receiver and Transmitter (UART) is actually pin 0 and pin 1 on Arduino Uno. To communicate between the two Arduinos without the inbuilt Universal Asynchronous Receiver and Transmitter (UART) serial hardware we will use a library called SoftwareSerial.h library.  But here we have configured to use pin 9 and pin 8 as Tx and Rx. That is pin 9 is the transmit pin(TX) and the pin 8 is the receive pin(RX) on both Arduino. The Tx pin of one Arduino is connected to the Rx of the other Arduino. You can use other pins as well. For transmit pin any digital pin can be used but for receive pin, pins with interrupt enable pin should be used. The ground and +5V pins are connected together as shown in the diagram above.

The code for the master Arduino is below.  

Transmitter code


// Include the Software Serial library
#include <SoftwareSerial.h>

// Define a Software Serial object and the used pins
SoftwareSerial softSerial(8, 9); // RX, TX

void setup(){
	Serial.begin(9600); // Normal Serial
	softSerial.begin(9600); // Soft Serial
}

void loop(){
	// Check for received characters from the computer
	if (Serial.available()){
		// Write what is received to the soft serial
		softSerial.write(Serial.read());
	}
}

The following is the code for the receiver

Receiver Code


// Include the Software Serial library
#include <SoftwareSerial.h>

// Define a Software Serial object and the used pins
SoftwareSerial softSerial(8, 9); // RX, TX

// LED Pin
int LED = 13;

void setup(){
	softSerial.begin(9600); // Soft Serial
	pinMode(LED, OUTPUT); // Define LED pin mode
}

void loop(){
	// Check if there is anything in the soft Serial Buffer
	if (softSerial.available()){
	// Read one value from the soft serial buffer and store it in the variable com
	int com = softSerial.read();
	// Act according to the value received
		if (com == 'n'){
		// Stop the LED
		digitalWrite(LED, LOW);
		}
		else if (com == 'y'){
		// Start the LED
		digitalWrite(LED, HIGH);
		}
	}
}

Once the codes are uploaded in the two Arduino Uno boards, the serial monitor on Arduino IDE should be opened. On the serial monitor either type in y or n to turn on or off the LED on the slave Arduino Uno.

 Here serial TTL communication between two Arduino Uno board was illustrated. There are other ways of communicating Arduino boards. One is to use Inter-Integrated Chip(I2C) communication which is illustrated in the tutorial I2C communication between Arduino.

Post a Comment

Previous Post Next Post