Arduino & Nextion - Serial1, 2 and 3 do not work

My apologies if this is a non-problem and I'm just being totally thick... saying that, I'm stuck and need some assistance.

I have a two Mega2560's, an original Arduino and the other is an Elegoo. Both exhibit the same issue when sending commands to my Nextion 3.5" Enhanced screen.

If connected to Serial everything works great, I receive events from the screen and can send to the screen. However, if I simply change the port used to Serial1, Serial2 or Serial3, and switching the cables to the respective connections, data sent to the screen does not work. Interesting note is that receiving from the screen continues to work, only the sending to the screen doesn't.

As a very simple test setup until I resolve this, I've got the Mega2560, the screen and a button on a breadboard. The button when pressed sends text to "t0".

I can't see any way of adding an image to this post! In the help, there is clearly an image icon in the toolbar, but that is not on the toolbar now. If someone can tell me how, I'll edit the post and add it.

And the code is:

#define NEXTION Serial1
#define NEXTION_SPEED 9600

const int buttonPin = 2;
int buttonState = 0;
int oldButtonState = 0;
int counter = 0;

void sendText(String text) {
  NEXTION.print(text);
  NEXTION.write(0xff);
  NEXTION.write(0xff);
  NEXTION.write(0xff);  
}

void setup() {
  //Serial.begin(9600);

  pinMode(buttonPin, INPUT_PULLUP);

  NEXTION.begin(NEXTION_SPEED);
  sendText("");
}

void loop() {
  buttonState = digitalRead(buttonPin);
  if (buttonState != oldButtonState) {
    oldButtonState = buttonState;
    
    if (buttonState == HIGH) {
      counter += 1;
      sendText("t0.txt=\"hello" + String(counter) + "\"");
    }
  }
}

The NEXTION definition makes it easier to switch between serial ports, but I have actually placed Serial1,2,3 in the code where I'm sending/receiving to/from the screen so it's not that.

I make no other change other than switching to a different serial port. On Serial it works, and on any of the other ports it does not.

I'm literally pulling my hair out here as I cannot for the life of me get a handle on why ports 1, 2 or 3 do not work. I must be missing something really obvious.

Thank you in advance for any help you can provide.

I assume that you have changed the connection to the Nextion to use the appropriate pins for the Serial interface that you are using at the time

That is not very simple, I can do it simpler :wink:

void loop()
{
  NEXTION.print( "t0.txt=\"hello1\"\xFF\xFF\xFF");
  delay(1000);
}

ok, you win :slight_smile:

Yes, and have the wires crossed: RX > TX, TX > RX

IIRC this define has to go inside the NexConfig.h file.

Thanks, but I'm not using a library Whandall

OK, I had a moment of clarity, inspiration, sixth sense... call it what you may.

I was powering the Nextion screen using the included power adapter, which was in turn connected to a 6V power plug, so only the blue and yellow wires were part of the arduino circuit.

I thought, "When all else fails....", so I attached an extra cable from the spare ground pin on the power adapter to my breadboard, and what do you know, everything suddenly started working :slight_smile:

Hopefully this will save any other newbies from hair pulling anguish.

Well, that's exactly why I wrote this tutorial: Common ground and why you need one

Thank you for writing these tutorials.

In order to find such tutorials however, you need to know what you don't know in order to ask the right questions. Catch 22.

My assumption (and I'm sure this is true for many) was the "there's something wrong with my code" or "the circuit looks correct".

My situation was odd, in that it worked on Serial, but not when using Serial 1, 2 or 3, and then only when sending data to the Nextion. Reading from the Nextion worked on all Serial ports.

Anyway, problem solved.

I completely agree, and I too have had the problem of not finding a tutorial I wanted because I didn't know what to search for.

What puzzles me about your problem is that you say it worked with serial port 0, it should not have worked on any of the serial ports.

You might find this useful: Using Nextion displays with Arduino

And experience gained, well done :smiley: