Mastering Serial Communication: Arduino Mega 2560 (Master) and Uno (Slave) UART Tutorial

Alef
3 min readJan 21, 2024

--

Introduction

In the realm of Arduino projects, serial communication is a fundamental skill. It’s like learning to have a conversation in a new language, but instead of words, we’re using electrical signals. In this tutorial, we’ll explore how to set up UART (Universal Asynchronous Receiver-Transmitter) communication between two popular Arduino boards: the Mega 2560 as the master and the Uno as the slave. By the end of this guide, you’ll have a clear understanding of how these two boards can ‘talk’ to each other.

What You Need

  • 1 x Arduino Mega 2560
  • 1 x Arduino Uno
  • Jumper wires
  • Arduino IDE

Understanding the Master: Arduino Mega 2560

The Setup

Initializing Serial Ports: Our Mega 2560 uses two serial ports. Serial for debugging and Serial1 for communicating with the Uno.

Serial.begin(9600);
Serial1.begin(9600);

LED as an Indicator: We use the built-in LED to visually represent the data received.

pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, HIGH);

The Loop

Receiving Data: The Mega waits for data on its Serial port.

if (Serial.available() > 0) {
char received = Serial.read();
...
}

Forwarding Data: Upon receiving data, it forwards the same data to the Uno via Serial1.

Serial1.write(received);

Acting on Data: If the received data is ‘1’, the LED turns off; if ‘2’, it turns on.

if (received == '1') {
digitalWrite(LED_BUILTIN, LOW);
} else if (received == '2') {
digitalWrite(LED_BUILTIN, HIGH);
}

Understanding the Slave: Arduino Uno

The Setup

Software Serial: Since the Uno has only one hardware serial port, we use SoftwareSerial to create a virtual serial port.

SoftwareSerial Serial1(10, 9); // RX, TX

Initializing Serial Ports: Both hardware and software serial ports are initialized.

Serial.begin(9600);
Serial1.begin(9600);

The Loop

Listening for Data: The Uno listens on the software serial port for data from the Mega.

if (Serial1.available() > 0) {
char received = Serial1.read();
...
}

Responding to Data: Similar to the Mega, the Uno uses the received data to control its LED.

if (received == '1') {
digitalWrite(LED_BUILTIN, LOW);
} else if (received == '2') {
digitalWrite(LED_BUILTIN, HIGH);
}

Let’s put it all together

Arduino Mega 2560 (Master)

#include <SoftwareSerial.h>

void setup() {
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, HIGH);
Serial.begin(9600);
Serial1.begin(9600);
}

void loop() {
if (Serial.available() > 0) {
char received = Serial.read();
Serial1.write(received);

if (received == '1') {
digitalWrite(LED_BUILTIN, LOW);
} else if (received == '2') {
digitalWrite(LED_BUILTIN, HIGH);
}
}
}

Arduino Uno (Slave)

#include <SoftwareSerial.h>

SoftwareSerial Serial1(10, 9); // RX, TX

void setup() {
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, HIGH);
Serial.begin(9600);
Serial1.begin(9600);
}

void loop() {
if (Serial1.available() > 0) {
char received = Serial1.read();

if (received == '1') {
digitalWrite(LED_BUILTIN, LOW);
} else if (received == '2') {
digitalWrite(LED_BUILTIN, HIGH);
}
}
}

Wiring

5V to 5V Connection:

  • Connect the 5V pin of the Arduino Mega to the 5V pin of the Arduino Uno. This step powers the Uno from the Mega.

Ground to Ground Connection:

  • Connect a GND (ground) pin of the Arduino Mega to a GND pin of the Arduino Uno. This common ground is crucial for proper communication.

TX to RX Connection:

  • Connect the TX1 (transmit) pin of the Arduino Mega to pin 10 on the Arduino Uno. In your code, pin 10 is set as the RX (receive) pin of the SoftwareSerial on the Uno.

Computer Connection and Serial Monitor:

  • Plug the Arduino Mega into your computer using a USB cable.
  • Open the Arduino IDE and select the correct COM port for the Mega.
  • Open the Serial Monitor in the Arduino IDE. This will be used to send data from the Mega to the Uno.

Your setup is now ready for testing. When you type ‘1’ or ‘2’ into the Serial Monitor of the Arduino IDE (with the Mega selected), the Mega will send this data to the Uno via the TX1 to pin 10 connection. The Uno will then react by turning its LED on or off based on the received data.

--

--