Serial1 not available when USB cable not connected/powered from barrel

Hi all!

Setup:

  • Arduino Mega 2560
  • Nextion tft screen connected to RX1 and TX1. Both GND sides are connected as well as the 5v line.

Test sketch:

void setup() {
  Serial1.begin(9600);
  delay(500);
  Serial1.print("baud=115200");
  Serial1.write(0xff);
  Serial1.write(0xff);
  Serial1.write(0xff);
  Serial1.end();
  Serial1.begin(115200);
  Serial.begin(115200);
  pinMode(13, OUTPUT);
  digitalWrite(13, LOW);
}

void loop() {
    if (Serial1.available() > 0) {
      digitalWrite(13, HIGH);
      Serial.println(Serial1.read());
    }
}

Problem: If I power up the Arduino board using the barrel jack connector or an only-power USB (such as a USB charger), Serial1 does not become available despite I'm sending serial data from the Nextion screen to the Arduino (with a button)

Workaround: If I connect the USB port to my computer and then open the serial monitor, Serial1 becomes available when I push the button on the Nextion screen and everything works great.

What am I missing here?

I found several topics about this, double checked everything but seems to be ok on my setup.

Thanks in advance

** New tests **
I've been curious on this problem, so I did a DTR reset (setting the RESET pin low for a moment) and bingo! It works. May there's something that's occurring too fast during the setup?

You use both Serial and Serial1. I assume You talk about serial comm to a Pc or similiar.
You miss how the controller serial is transferred to the reciever or miss which one, Serial or Serial1 is the link.

Well, that's the simplest example I can give to show the behavior. In normal operation I'm just using Serial1 connected to the Nextion screen and there's no interaction on the USB port A.K.A. Serial. The button I press on the Nextion screen drives a led on the Arduino and that's pretty much it. But I'm not being able to do that since Arduino doesn't "recognize" the Serial1 sent data

Serial1 should work regardless of how the Mega is powered. It has nothing to do with the USB connection. Look for another cause of the problem.

Maybe the USB connection is inadvertently creating a GND connection to something?

...R

after a few more debugging hours I found the issue. Seems like the Serial1 baud rate change needs a bit of delay on the last step

  Serial1.begin(9600);
  delay(500);
  Serial1.print("baud=115200");
  Serial1.write(0xff);
  Serial1.write(0xff);
  Serial1.write(0xff);
  Serial1.end();
  Serial1.begin(115200);
  Serial.begin(115200);
  Serial1.flush();
  delay(1000); // This delay does the trick

Thanks everyone!