Alex Fraga
Published © Apache-2.0

Arduino Mega 2560 R3 - Serial Port Basics

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

BeginnerProtip3 hours63,606
Arduino Mega 2560 R3 - Serial Port Basics

Things used in this project

Story

Read more

Schematics

Arduino Mega To Arduino UNO (Adafruit Metro 328)

How to wire the two together... Fritzing file..

Arduino Mega to Arduino Leonardo

How to connect Arduino Mega to Arduino Leonardo... Fritzing file..

ALL 3 Arduinos

Code

Adruino Mega Master UART

Arduino
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 UART

Arduino
//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 SoftSerial

Arduino
#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 SoftSerial

Arduino
#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 #3

Arduino
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___SoftSerial

Arduino
#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');
    }
    
  }

}

Credits

Alex Fraga

Alex Fraga

1 project • 1 follower
Robot Hobbyist,
Thanks to Techmirtz.

Comments