Arduino output not showing up on Nextion 7" intelligent Display

I am using a Nextion Display for a project. I am attempting to get the display to show values off of sensors. I cannot get the arduino serial output to change display on the Nextion Display.

I am seeing n0.val=(value1), n1.val=(value2), n2.val=(value3) in the serial monitor when I run this and just set sensor1=(value1), sensor2=(value2), sensor3=(value3) but these values never show up on the display.

For this project I am using:
-Nextion NX804P0A70-011R-Y
Arduino Mega 2560

Here is my code.

void loop(){

  sensor1 = map(analogRead(A0),0,100,100,0);
  sensor2 = map(analogRead(A1),0,100,100,0);
  sensor3 = map(analogRead(A2),0,100,100,0);

 Serial.print("n0.val=");
  Serial.print("sensor1"); 
  Serial.write(0xff);  
  Serial.write(0xff);
  Serial.write(0xff);

    
 Serial.("n1.val=");
  Serial.print("sensor2"); 
  Serial.write(0xff);  
  Serial.write(0xff);
  Serial.write(0xff);


 Serial.print("n2.val=");
  Serial.print("sensor3");
  Serial.write(0xff);  
  Serial.write(0xff);
  Serial.write(0xff);
 

}

Hello,

This won't compile:

 Serial.("n1.val=");

The code you have is sending:

n1.val=sensor10xff0xff0xff

Try

Serial.print("n0.val=");
  Serial.print(sensor1);
  Serial.write(0xff); 
  Serial.write(0xff);
  Serial.write(0xff);

Also, how many times a second do you think loop goes round? Your code sends the same thing over and over again probably thousands of times per second.

Try reading Using Nextion displays with Arduino

++Karma; // For posting your code correctly in code tags on your first post.

Yes, thank you the, Serial.("n1.val="), error I found just after posting.

correct the code sends the same thing over and over, this is what I want because I want the values on the display to continuously update with the live values on the sensors.

This is my code now. I am having the same issue.

void setup() {
  Serial.begin(9600);
  delay(50);
  Serial.begin("baud=115200");
  Serial.write(0xff);
  Serial.write(0xff);
  Serial.write(0xff);
  Serial.end();
  Serial.begin(115200);

void loop(){

  sensor1 = map(analogRead(A0),0,100,100,0);
  sensor2 = map(analogRead(A1),0,100,100,0);
  sensor3 = map(analogRead(A2),0,100,100,0);

Serial.print("n0.val=");
  Serial.print(sensor1); 
  Serial.write(0xff);  
  Serial.write(0xff);
  Serial.write(0xff);


 Serial.print("n1.val=");
  Serial.print(sensor3);
  Serial.write(0xff);  
  Serial.write(0xff);
  Serial.write(0xff);
 

Serial.print("n2.val=");
  Serial.print(sensor3); 
  Serial.write(0xff);  
  Serial.write(0xff);
  Serial.write(0xff);


}

Correct the code sends the same thing over and over, this is what I want because I want the values on the display to continuously update with the live values on the sensors.

There's no point sending it faster than you can read it and if you send it faster than the serial port can cope with or faster than the Nextion can deal with it then you will have problems. I suggest no more than once per quarter second at least until you know it works OK.

Your setup function has no } at the end, I am surprised it compiles.

You are asking for trouble trying to change the baud rate through the serial port. Leave it at 9600 until everything works properly, then, if you really need to change it do so in the Nextion configuration and change it in setup on the Arduino. This is covered in my tutorial so please read that before asking anything else.

Thank you.

Okay, I have dropped it down to 9600 and have a delay of a 1/2 a second and it is still having the same issue even with it sending slower. I have read your tutorial and tried many things from it.

I see the below in your demo code and I am not sure I follow. Is this HMI_startup_message a preset command or can this be anything I want to call it? If this is a preset, what does it do?

  HMI_startup_message();
}
void HMI_startup_message() {

Below is my code now.

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

}

void loop(){

 sensor1 = map(analogRead(A0),0,100,100,0);
  sensor2 = map(analogRead(A1),0,100,100,0);
  sensor3 = map(analogRead(A2),0,100,100,0);


delay(500);

Serial.print(F("n0.txt=\"")); 
   Serial.print(F("test"));
   Serial.print(F("\""));
   Serial.write(0xff);  
   Serial.write(0xff);
   Serial.write(0xff);
   Serial.println("END");
}   
 Serial.print(F("n1.val="));
  Serial.print(sensor2); 
  Serial.write(0xff);  
  Serial.write(0xff);
  Serial.write(0xff);


 Serial.print(F("n2.val="));
  Serial.print(sensor3);
  Serial.write(0xff);  
  Serial.write(0xff);
  Serial.write(0xff);


}

I see the below in your demo code and I am not sure I follow. Is this HMI_startup_message a preset command or can this be anything I want to call it? If this is a preset, what does it do?

When you first run my code with my sample Nextion configuration there is (should be) a message at the bottom of the display telling you about the tutorial, this is the code that sends that message. You can change the text to anything you like.

   Serial.println("END");

Where in any of my examples have I put that?

BTW, how have you got your Nextion connected? Maybe a photo would help.

When I run your example code with your example Nextion configuration I don't see any of the effects from the code. It is as if they aren't connected.

I made sure to disconnect power and remove the SD card before powering on again. See pictures below for my connection and result.


I can't see your pictures...

All your serial prints are to serial port 0, I think you said you have a Mega, I would expect you to have connected it to serial port 1 as per my tutorial and your prints to be to serial port 1.

Sorry, I did not attach photos correctly. This is the result I go using your code and Nextion configuration. It doesnt appear that the Nextion is getting influenced at all by the arduino code.

How to post an image

Please show me how it is connected, as I asked in reply #5.

I could not post both in the same post so here is the second picture

Can you please follow the instructions for posting images?

Got it

That's better.

2 things:
The code you posted shows you are writing to the serial port 0, the Nextion is connected to serial port 1.
If the red and black connections in the top middle of the image are the power to the Nexiton then I don't see any ground between the Nextion and the Mega.
Common ground and why you need one

Alright, so the ground fixed the issue with running your code and example. However, now when we are trying to run our code it is not sending to the serial 1 port.

void setup() {
  Serial1.begin(9600);
    

}

void loop(){

  sensor1 = map(analogRead(A0),0,100,100,0);
  sensor2 = map(analogRead(A1),0,100,100,0);
  sensor3 = map(analogRead(A2),0,100,100,0);


delay(500);

Serial1.print(F("\nn0.txt=\""));
   Serial1.print(F("test"));
   Serial1.print(F("\""));
   Serial1.write(0xff); 
   Serial1.write(0xff);
   Serial1.write(0xff);
 
  
 Serial1.print(F("\nn1.val="));
  Serial1.print(sensor2);
  Serial1.write(0xff); 
  Serial1.write(0xff);
  Serial1.write(0xff);


 Serial1.print(F("\nn2.val="));
  Serial1.print(sensor3);
  Serial1.write(0xff); 
  Serial1.write(0xff);
  Serial1.write(0xff);

  }

Is there anything we need to be doing within Nextion Editor to bring these printed values into the display?

I think you need to walk away from this, get some rest then come back and have another look. Compare your non working code to mine, when you spot the difference you'll know why it doesn't work.

I have had multiple other people look this over for me. We are not sure what needs to be done for this. If there is something you see that can help it would be greatly appreciated.

It's obvious. Compare your print statements to mine.

My code is below

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

}

void loop(){
  int8_t sensor1;
  int8_t sensor2;
  int8_t sensor3;

  sensor1 = map(analogRead(A0),0,100,100,0);
  sensor2 = map(analogRead(A1),0,100,100,0);
  sensor3 = map(analogRead(A2),0,100,100,0);


delay(500);

   Serial1.print(F("\nt0.txt=\""));
   Serial1.print(F("Test"));
   Serial1.print(F("\""));
   Serial1.write(0xff); 
   Serial1.write(0xff);
   Serial1.write(0xff);

  
  Serial1.print(F("\nn0.val="));
  Serial1.print(sensor2);
  Serial1.print(F("\""));
  Serial1.write(0xff); 
  Serial1.write(0xff);
  Serial1.write(0xff);


  Serial1.print(F("\nn1.val="));
  Serial1.print(sensor3);
  Serial1.print(F("\""));
  Serial1.write(0xff); 
  Serial1.write(0xff);
  Serial1.write(0xff);

  }

Now, this below is a print from your code, there is no obvious difference between the two. Can you please help me understand what you are seeing.

  Serial1.print(F("t0.txt=\""));
  Serial1.print(F("Nextion demonstration by Perry Bebbington. For support go to https://forum.arduino.cc/index.php?board=7.0"));
  Serial1.print(F("\""));
  Serial1.write(0xff);
  Serial1.write(0xff);
  Serial1.write(0xff);

Right, have a look at the first line of your code and the first line of mine, they are not the same, you have extra characters in yours.