Data send from arduino to Nextion not working

Hi all,

I try to send some data from my Arduino Mega to my Nextion NX4827T043_011.
I used the code found from this topic: https://forum.arduino.cc/index.php?topic=606094.0
But it did not work for me.

/*
This code is based on the software serial example on Arduino's website
 */

#include <SoftwareSerial.h> //Include the library

SoftwareSerial mySerial(15, 14); // RX, TX

void setup() {
  
  Serial.begin(57600); //open the serial port
  while (!Serial) { // wait for serial port to connect. Needed for native USB port only
  ;
  }

  Serial.println("Serial On"); //Print this messages when the serial port is connected
  mySerial.begin(9600); // set the data rate for the SoftwareSerial port

 
}

void loop() { // run over and over

  // Write "test" in the text box t0
  mySerial.print("t0.txt=\""); // Changing the value of box n1
  mySerial.print("test");
  mySerial.print("\"");
  mySerial.write(0xff);  // We always have to send this three lines after each command sent to the nextion display.
  mySerial.write(0xff);
  mySerial.write(0xff);
}

I try on 2 Nextion (exactly the same), no change.
I still can upload HMI design to my Nextion so I believe the RX/TX is working fine.

Any idea ? It’s driving me crazy ! The sketch is so simple, but I don’t find the trick…
Maybe I’m forgetting something on the Nextion editor side ?

There are a couple potential problems. Next time, please post a schematic of where you plugged everything into the Mega.

  1. Are your TX / RX pins backwards?
  2. Are your sketch TX and RX pins correct?
  3. Is everything plugged into the Mega correctly? Occasionally a sketch for an UNO will have different pins than a Mega.

And...
Have you connected the Nextion ground to the Mega?

SoftwareSerial mySerial(15, 14); // RX, TX

I don't understand why you are trying to use software serial on pins that already have a spare hardware serial port available. Seems daft.

Sorry for the schematic, forgot it !

EverythingTechPro:

  1. Are your TX / RX pins backwards?
  2. Are your sketch TX and RX pins correct?
  3. Is everything plugged into the Mega correctly? Occasionally a sketch for an UNO will have different pins than a Mega.

1 & 2: Yes
3: I thought so, but I’m not sure of anything now.

PerryBebbington:
And…
Have you connected the Nextion ground to the Mega?

SoftwareSerial mySerial(15, 14); // RX, TX

I don’t understand why you are trying to use software serial on pins that already have a spare hardware serial port available. Seems daft.

Nextion ground connected to Mega, yes.
I originally used the 0,1 (TX/RX) but it failed. The 14,15 test is just a desperate attempt to have something that works, I admit !

The Mega has four hardware serials including pins 0,1.

Circuit looks reasonable.

As you have connected to hardware serial port 3 I suggest you edit your sketch to use it and see if that helps.

Always assuming of course that your Nextion baud rate is also set at 9600 and has a text box t0.

Tested this (with 14/15 pin) and it failed:

void setup() {
  
  Serial3.begin(9600); //open the serial port
  Serial3.println("Serial On"); //Print this messages when the serial port is connected

}

void loop() {

  // run over and over
  // Write "test" in the text box t0
  Serial3.print("t0.txt=\""); // Changing the value of box n1
  Serial3.print("test");
  Serial3.print("\"");
  Serial3.write(0xff);  // We always have to send this three lines after each command sent to the nextion 
display.
  Serial3.write(0xff);
  Serial3.write(0xff);
  delay(100);

}

PerryBebbington:
Always assuming of course that your Nextion baud rate is also set at 9600 and has a text box t0.

Nextion has a text box t0.
How can I check the Nextion baud rate ? Is it fixed by the hardware ?
The datasheet of my Nextion says: speedbaud rate min 2400, typical 9600, max 115200.

It works !

Changed from:

Serial3.begin(9600)

To

Serial3.begin(115200)

The speed was the key.
Thanks for help !

But I still wonder how we can fix the speed rating on the Nextion side !

Drlow,

Well done for getting it working. Have some Karma for your first success.

  Serial3.begin(9600); //open the serial port
  Serial3.println("Serial On"); //Print this messages when the serial port is connected

You are starting serial port 3 for the Nextion (correct) then sending “Serial on” to the Nextion, which is not correct. The Nextion won’t respond to it and you won’t see it. You need to also start serial port 0, which is the one that goes to the serial monitor, and send the start up message to there (like you did in your initial code).

How can I check the Nextion baud rate ? Is it fixed by the hardware ?

It is 9600 by default. If it is working at 115200 then somewhere in the Nextion configuration you are using there will be:

baud=115200

or

bauds=115200

See Nextion instruction set
I deduce from this that that the Nextion configuration you are using is not one you created, otherwise you’d know what the baud rate was as you’d have set it.

I recommend you read Using Nextion displays with Arduino

Good luck and have fun :slight_smile: