Mega not receiving data UART

I'm having some trouble figuring out why I'm not receiving any data on my Mega 2560. I have a lab that's tasked me to use UART communication between a UNO and Mega 2560. I got the Mega to send a character to the UNO, then the UNO prints it to the Serial Monitor, but when I try to send an acknowledgment char back to the Mega from the UNO it doesn't send.

Does anyone know what I'm doing wrong?

Edit;
Thanks for the help everyone! I'll remember not to use SoftwareSerial on UART pins

Welcome to the forum

Why use SoftwareSerial on a Mega when it has 4 hardware UARTs ?

why are you using SoftwareSerial with the Mega it has hardware serial ports
SoftwareSerial has problems transmitting and receiving at the same time

  1. same as above .
  2. Further, Pins 14 and 15 are (by default) TX/RX for serial3. (your sketch will work with no hardware change if you ommit softwareserial and add a "Serial3.begin(...)").
  3. Did you cross the wiring between uno and mega? (tx->rx, rx->tx)?

It should not be necessary to use the listen() function when there is only one softwareserial in use.
Only reading the software serial if the character received is the acknowledgement character is not a good technique, if for some reason you receive an incorrect character that will never get read and you will never see any subsequent acknowledgments.

Try the following simple sketches (untested but compiled) to establish communication between MEGA and UNO:

MEGA Sketch:

void setup()
{
  Serial.begin(9600);
  Serial3.begin(9600);  //RX3 = DPin-15, TX3 = DPin-14
}

void loop()
{
  Serial3.print("Hello");
  Serial.println();
  //--------------------
  while(Serial3.available() == 0)
  {
        ;  //wait for ACK
  }
  char x = Serial3.read();
  if(x == 'y');
  {
      Serial.println("ACK is received from UNO");
  }
  delay(1000);
}

UNO Sketch:

#include <SoftwareSerial.h>
const char RX_PIN = 2;
const char TX_PIN = 3;
SoftwareSerial unoUART(RX_PIN, TX_PIN);
char myArray[6];

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

void loop()
{
  byte n = unoUART.available();
  if(n != 0)
  {
    byte m = unoUART.readBytesUntil('\n', myArray, 10);
    myArray[m] = '\0';
    if(strcmp(myArray, "Hello") == 0)
    {
      Serial.println("Message received from MEGA.");
      unoUART.print('y'); //send ACK to MEGA
    }
  }
}

Homework outsourcing?

does it count as outsourcing? I had probably 99% of it working, i just couldn't figure out the last 1%

I would guess more like 30% copied and 70% unfinished.

I also guess it's your assignment.

thanks everyone! I guess i was confused about how/when to use SoftwareSerial.

So if i;m understanding this correctly, SoftwareSerial is used for pins that ARE NOT UART pins. If there are already provided UART pins, just use SerialX? From what I read Serial (no number) is used to communicate with the virtual communication between PC and Arduino. If I connected an external device to the Serial (no number), I would NOT be able to send data to the PC from Arduino?

well the only part i couldnt get to work was the acknowledge from UNO to Mega because I was using SoftwareSerial on the Mega's UART pins

Do you that Serial (no number) is actually Serial0? In code, the 0 is not written.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.