Problem with serial communication between Arduino mega and Nextion display

I am having trouble with the communication between my Nextion display (Enhanced series) and my Arduino mega.
I am receiving strange characters when my nextion is not even supposed to be sending anything.

Here is my condensed code.

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

void loop() {
  if (Serial2.available() > 0) {
    String sFromDisplay = "";
    while (Serial2.available() > 0) {
      sFromDisplay += char (Serial2.read());
      delay(50);
      Serial.println(sFromDisplay);
    }
  }
}

The only wire in my Mega is the nextion's TX wire and it is in pin 17 Serial2's Rx

I want the code to display what I send over the nextion.

I think this might be a hardware problem but I want to be certain. Would someone please check my code?

I have added an example of my output as an attachment.

Example output .png

Example output .png

I’m using a mobile phone at the moment so can’t reply properly.

Try the examples in my tutorial ‘using Nextion Displays with Arduino’, at the top of the display section of the forum.

Random data suggests a bad connection. Are you sure you have a good connection of both the blue wire and the ground to the Mega?

I’ll post you some test code when I have my PC on. Too late now. Feel free to remind me if I don’t do it tomorrow.

Try this. You will have to change the serial port for the Nextion or wire it to serial port 1 instead of 2.

// Receives data on serial port 1 and sends to the serial monitor with an index.
// Each character is on a new line.
// Receiving 0x00 resets the count to 0. 


void setup() {
  char fileName[] = __FILE__;
  Serial.begin(115200);
  delay(2000);
  Serial.println("Serial monitor started");
  Serial1.begin(9600);
  Serial.println("Serial 1 started");
  Serial.println(" ");
  Serial.println(sizeof(fileName));
  Serial.println(fileName);  
}

void loop() {
  serialMonitor();
}

void serialMonitor() {
  char RxTemp;
  static uint8_t charCount;
  while (Serial1.available() > 0) {
    RxTemp = Serial1.read();
    Serial.print(charCount);
    Serial.print(" byte ");
    Serial.print((byte)RxTemp, HEX);
    Serial.print(" char ");
    Serial.println((char)RxTemp);
    ++charCount;
    if ((uint8_t)RxTemp == 0) {
      charCount = 0;
      Serial.println("charCount = 0");
    }
  }
}

Thank you Perry but I figured out what my problem is. My Nextions rx pin was not connected to my arduino because I think it is broken but when I tested my code again I tested it with the rx pin in and it worked.

Sorry for the runaround. I appreciate your diligence and help. It is rare to find someone who is came and willing to look past ons ameture mistakes.

Glad you got it fixed.
We are here to help beginners, so don’t worry about asking beginner type questions.

Something you should be aware of is not to use String (capital S), you will have problems. Lots of questions and answers on here explaining why, do some searching.

Use C strings; null terminated chat arrays.

This topic was automatically closed after 120 days. New replies are no longer allowed.