Nithin Varghese
Nithin Varghese

Reputation: 923

Unable to do Arduino Mega to Arduino Mega serial communication

Based on the circuit below, I tried hooking up two Arduino Mega for serial communication.

enter image description here

The code for sender:

char mystr[3] = "Hello"; //String data

void setup() {
  // Begin the Serial at 9600 Baud
  Serial.begin(9600);
}

void loop() {
  Serial.write(mystr, 5); //Write the serial data
  delay(1000);
}

The code for receiver:

char mystr[5]; //Initialized variable to store received data

void setup() {
  // Begin the Serial at 9600 Baud
  Serial.begin(9600);
}

void loop() {
  Serial.readBytes(mystr, 5); //Read the serial data and store in var
  delay(1000);
}

There is no output in the Serial console of Arduino. Could someone please inform me of the possible cause and solution for the same. If I've missed out anything, over- or under-emphasized a specific point let me know in the comments.

Upvotes: 0

Views: 1540

Answers (2)

Lars
Lars

Reputation: 68

If I understood this right you have one Arduino connected to your pc and to another Arduino?

The problem is that you need to specify which Serial port to use: That is rather easy, just type Serial1 or Serial2 instead of just Serial. That allows you to open 2 Serial ports: One to your other Arduino and one to your Computer for Displaying the results !

LINK: https://www.arduino.cc/en/Tutorial/MultiSerialMega

Upvotes: 1

Ivan Sheihets
Ivan Sheihets

Reputation: 830

You need to check available data from serial:

void loop() {
    if (Serial.available() > 0) {
            // read the incoming byte:
            Serial.readBytes(mystr, 5);
            Serial.print("I received: ");
            Serial.println(mystr, DEC);
    }
}

Upvotes: 0

Related Questions