Arduino Mega 2560 R3 - Serial Port Basics

Arduino Mega 2560 R3 - Serial Port Basics © Apache-2.0

How to connect a serial port Arduino Mega 2560 R3 to an Arduino UNO R3 compatible and to an Arduino Leonardo clone.

  • 29,134 views
  • 0 comments
  • 1 respect

Components and supplies

Apps and online services

About this project

This tutorial we are covering Serial Port Basics on the Arduino Mega 2560 R3.

If you ever wonder how to use the other Serial ports on the Arduino Mega 2560 R3, but don't know where to start? This short tutorial we'll go over the basics, we'll connect to an Adafruit Metro 328 = its an Arduino compatible; works just like an Arduino UNO R3, and we will connect to an Arduino Leonardo clone...

The connections will be UART to UART, SoftwareSerial to SoftwareSerial, UART to SoftwareSerial, and ALL 3 connected together!! :)

Before you make the physical wire connections.... LOAD the code to your Arduinos first!

Please follow this step because your Arduinos use the "DEFAULT" SerialPort to upload your code from the Computer, once you make the physical wire connections the "Default" SerialPort will be used on the Arduino UNO (Adafruit Metro 328) to communicate with the Arduino Mega 2560 R3's SerialPort #1 which is pin19 RX1 and pin18 TX1 on the Mega. This will not allow the Arduino UNO(Adafruit Metro 328) to communicate with the outside world; AKA your computer. The Leonardo will use the SoftwareSerial example but if using the UART; same will apply as the Arduino UNO's....

MEGA to Adafruit 328(Arduino UNO) UART to UART connection.

  • Upload the the codes to Arduino Mega and Arduino UNO(Adafruit 328)
  • Connect one end of a wire to Mega's pin19 RX1 and the other end to Arduino UNO's(Adafruit 328) pin1 TX...
  • Connect one end of a wire to Mega's pin18 TX1 and the other end to Arduino UNO's(Adafruit 328) pin0 RX
  • Connect the ground pin on the Mega to the ground pin on the Arduino UNO(Adafruit 328).
  • Connect Arduino UNO to a Stable/Reliable power source.
  • Connect Arduino Mega to the Computer from the USB cable.
  • On the Arduino Mega UART1 program window of the Arduino IDE, make sure your COM is selected to the Arduino Mega before opening the Serial Console, Open the Serial Console set to 9600 baud COM Speed.
  • On the Top window in the text box enter the letter x … not the capital X … the LED on the Arduino UNO(Adafruit 328) goes ON.
  • On the Top window in the text box enter the letter a … not the capital A … the LED on the Arduino UNO(Adafruit 328) goes OFF.
// This is the Arduino Mega Code UART1


void setup() {
Serial.begin(9600);
Serial1.begin(9600);
}
void loop() {
// Check for received Characters from the computer
if (Serial.available())
{
// Write what is received to the default serial port
Serial1.write(Serial.read());
}
}

What we are doing in this code is calling on the Arduino Mega's default UART Serial port by calling Serial.begin at a 9600 baud speed rate. This is going to listen to the Computer for instructions through the USB cable. We are also calling upon the 2nd UART Serial port by calling Serial1.being at a 9600 baud rate. This is going to communicate with the Arduino UNO(Adafruit Metro 328).

What happens.... when you type the letter x or a on the text window on the Serial Console it sends it to the Arduino Mega, then the Arduino Mega sends it to the Arduino UNO(Adafruit Metro 328) through the 2nd UART Serial port with the Serial1.write(Serial.read()); command.

// This is the Arduino UNO UART code



//LED pin
int LED = 13;


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



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

In this code we are using the Arduino UNO (Adafruit Metro 328) default Serial UART to communicate with the Mega, The Arduino waits for the Serial UART to receive a message, if the data is a refence what is in the code, in this case is a letter x or a the code will turn ON or OFF the LED on the Arduino UNO (Adafruit Metro 328).

Arduino Mega to Arduino Leonardo …. SoftSerial to SoftSerial connection.

  • Load code to Arduinos
  • One end of one wire goes to pin9 of Mega, the other side of wire goes to pin8 of Arduino Leonardo.
  • One end of one wire goes to pin9 of Mega, the other side of wire goes to pin9 of Arduino Leonardo.
  • One end of one wire goes to Ground pin of Mega, the other side of the wire goes to the Ground pin of Arduino Leonardo.
  • Connect a reliable power supply to your Leonardo
  • Connect the computer USB cable to your Mega
  • On the Arduino Mega SoftSerial window in the Arduino IDE, make sure your comm port is set for the Arduino Mega.
  • Open your Serial Console, set it to 9600 baud speed rate if its not there yet, type the letter a on the text box on the top, the light on the Leonardo should go ON, type x on the text box and the light on the Leonardo should go OFF.
// this is the code for the Mega Software Serial as Master



#include <SoftwareSerial.h>
SoftwareSerial softSerial(8, 9); // 8=RX, 9=TX


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



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

In this code we use software serial instead of the hardware UART serial, to communicate with the Leonardo, Pin8 will be RX and pin9 will be TX. Hardware Serial UART is used to communicate with the computer. The same process hapens here; type something in the serial console text window and the computer sends it to the Mega, then the Mega sends it to the Leonardo.

// This is the code for the Leonardo as slave software serial

#include <SoftwareSerial.h>
SoftwareSerial softSerial(8, 9); // 8=RX, 9=TX
//LED pin
int LED = 13;


void setup() {
softSerial.begin(9600);
pinMode(LED, OUTPUT);
}



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

Here we use software serial instead of the hardware serial; pin8 is RX, pin9 is TX, and the same thing happens as the other example; if the letter a is received the LED on the Leonardo is turned ON if the letter x is received the LED is turned OFF.

Arduino Mega Hardware UART to Arduino Leonardo Software Serial

  • Keep the same program on the Arduino Leonardo
  • Load Arduino Mega UART3 code on the Mega
  • connect pin14 TX3 on the Mega to pin8 RX on the Leonardo
  • connect pin15 RX3 on the Mega to pin9 TX on the Leonardo
  • connect the Leonardo to a reliable power source
  • connect the Mega to the computer USB cable
  • On the Arduino Mega UART3 code in the Arduino IDE make sure your using the Mega port, Open the serial console when you type a the LED turns ON the Leonardo, when you type an x the LED on the Leonardo turns OFF.
// This the Mega's code UART MAster UART3


void setup() {
Serial.begin(9600);
Serial3.begin(9600);
}


void loop() {
// Check for received Characters from the computer
if (Serial.available())
{
// Write what is received to the serial1 port
Serial3.write(Serial.read());
}
}

In this code we use UART3 on the Mega to communicate with the Leonardo. Same thing happens as the other example we only used UART #3 on the Mega. The code on the Leonardo remains the same as the previous example.

This is the LAST Example!!!

We will connect ALL 3 together!!!

You may be wondering why would you want to do that?!??!

Because you may find yourself in a situation where your using your default hardware UART for a device and then you need another UART connection; this is where you use the software serial on the same board!!!... you may say.... well I will just use a Mega... Well, … my little programmer, not ALL shields or sensors are compatible with the Mega; that's why were are going to connect the Arduino UNO (Adafruit Metro 328) as an In-between microcontroller just for this reason.... You'll thank me later when the issue arrives..

  • To save time and energy, the Mega keeps the same program and the Leonardo keeps the same program from the last example.
  • We just need to upload the new program to the Arduino UNO (Adafruit Metro 328)
  • Upload Arduino UNO UART___SoftSerial code to the Arduino UNO(Adafruti Metro 328)
  • connect pin9 TX on the Leonardo to pin8 RX on the Arduino UNO(Adafruit Metro 328) this is for Software Serial
  • connect pin8 RX on the Leonardo to pin9 TX on the Arduino UNO(Adafruti Metro 328) this is for Software Serial
  • connect pin14 TX3 on the Mega to pin0 RX on the Arduino UNO(Adafruit Metro 328) this is for Hardware Serial UART
  • connect pin15 RX3 on the Mega to pin1 TX on the Arduino UNO (Adafruit Metro 328)
  • connect ALL 3 Micros ground pins together
  • connect Leonardo and UNO (Adafruit Metro 328) to a reliable power source
  • connect Mega to computers USB cable
  • On the Arduino Mega UART3 code IDE make sure your Mega comm port is selected.
  • Open the Serial Monitor use 9600 baud speed, when you type the letter v on the text window the LED on the UNO (Adafruit Metro 328) goes ON. Whe you type the letter b the LED goes OFF.
  • When you type the letter a on the text window the computer sends it to the Mega then the Mega sends it to the UNO (Adafruti Metro 328) then the UNO (Adafruit Mero 328) sends it to the Leonardo to turn ON the LED on the Leonardo. Type the letter x and the LED on the Leonardo goes OFF
// this is the code that is needed to connect ALL 3 micros together


#include <SoftwareSerial.h>
SoftwareSerial softSerial(8, 9); // 8=RX, 9=TX

//LED pin
int LED = 13;


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



void loop() {
// Check if there is anything in the Serial Buffer
if (Serial.available())
{
// Read one value from the Serial buffer and store it in the variable com
int com = Serial.read();
// Act according to the value received
if (com == 'b')
{
// stop the led
digitalWrite(LED, LOW);
}
else if (com == 'v')
{
// Start the LED
digitalWrite(LED, HIGH);
}
else if (com == 'x')
{
softSerial.write('x');
delay(100);
}
else if (com == 'a')
{
softSerial.write('a');
}
}
}

Here you can see how softSerial is declared and used, when the letter x is recieved, softSerial will send a letter x to the Leonardo, same for the letter a.

Code

Adruino Mega Master UARTArduino
void setup() {
  Serial.begin(9600);
  Serial1.begin(9600);

}

void loop() {
  // Check for received Characters from the computer
  if (Serial.available())
  {
    // Write what is received to the serial1 port
    Serial1.write(Serial.read());
  }

}
Arduino UNO (Adafruit Metro 328) SLAVE UARTArduino
//LED pin
int LED = 13;




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

}

void loop() {
  // Check if there is anything in the Serial Buffer
  if (Serial.available())
  {
    // Read one value from the Serial buffer and store it in the variable com
    int com = Serial.read();

    // Act according to the value received
    if (com == 'x')
    {
      // stop the led
      digitalWrite(LED, LOW);
    }

    else if (com == 'a')
    {
      // Start the LED
      digitalWrite(LED, HIGH);
    }

    
  }

}
Arduino Mega Master SoftSerialArduino
#include <SoftwareSerial.h>

SoftwareSerial softSerial(8, 9); // 8=RX, 9=TX


void setup() {
  Serial.begin(9600);
  softSerial.begin(9600);

}

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

}
Arduino Leonardo Slave SoftSerialArduino
#include <SoftwareSerial.h>

SoftwareSerial softSerial(8, 9); // 8=RX, 9=TX

//LED pin
int LED = 13;




void setup() {
  softSerial.begin(9600);
  pinMode(LED, OUTPUT);

}

void loop() {
  // Check if there is anything in the softSerial Buffer
  if (softSerial.available())
  {
    // Read one value from the softSerial buffer and store it in the variable com
    int com = softSerial.read();

    // Act according to the value received
    if (com == 'x')
    {
      // stop the led
      digitalWrite(LED, LOW);
    }

    else if (com == 'a')
    {
      // Start the LED
      digitalWrite(LED, HIGH);
    }

    
  }

}
Arduino Mega UART #3Arduino
void setup() {
  Serial.begin(9600);
  Serial3.begin(9600);

}

void loop() {
  // Check for received Characters from the computer
  if (Serial.available())
  {
    // Write what is received to the serial1 port
    Serial3.write(Serial.read());
  }

}
Arduino UNO UART___SoftSerialArduino
#include <SoftwareSerial.h>

SoftwareSerial softSerial(8, 9); // 8=RX, 9=TX

//LED pin
int LED = 13;




void setup() {
  softSerial.begin(9600);
  Serial.begin(9600);
  pinMode(LED, OUTPUT);

}

void loop() {
  // Check if there is anything in the Serial Buffer
  if (Serial.available())
  {
    // Read one value from the Serial buffer and store it in the variable com
    int com = Serial.read();

    // Act according to the value received
    if (com == 'b')
    {
      // stop the led
      digitalWrite(LED, LOW);
    }

    else if (com == 'v')
    {
      // Start the LED
      digitalWrite(LED, HIGH);
    }

    else if (com == 'x')
    {
      softSerial.write('x');
      delay(100);
    }

    else if (com == 'a')
    {
      softSerial.write('a');
    }
    
  }

}

Schematics

Arduino Mega To Arduino UNO (Adafruit Metro 328)
How to wire the two together... Fritzing file..
uploads2ftmp2f0d50ef77-6185-4601-942d-4247ff8283162fproject_2_arduinos_uart_to_uart_Fz1loFMYvo.fzz
Arduino Mega to Arduino Leonardo
How to connect Arduino Mega to Arduino Leonardo... Fritzing file..
uploads2ftmp2ff24e9ff1-b8a2-4e5d-bf6e-3145c47c040b2fproject_2_arduinos_softwareserial_to_softwareserial_fAMQ55FFEd.fzz
ALL 3 Arduinos
uploads2ftmp2f6ad8f826-fb59-49dd-a156-a81436d9f7162fproject_all_3_XamcrhTrwn.fzz

Comments

Similar projects you might like

View Serial Monitor Over Bluetooth

by millerman4487

  • 47,875 views
  • 13 comments
  • 40 respects

Serial Communication between NodeMCU and Arduino

Project showcase by Pawan Kumar

  • 111,937 views
  • 13 comments
  • 38 respects
Add projectSign up / Login