Arduino + Nextion Display

Hi,

I recently started using a Nextion 3.5" Enhanced screen with an Arduino Uno. The idea is to build an alarm panel. The Nextion display will receive data from the Arduino to the display, this part I can do.

What I am struggling with is using the Nextion display as a button input. I just have two buttons on the display that will either mute the alarm or test it. I have tried several methods from using the standard RX, TX (0, 1) pins as well as using other pins for example Serial1 with an Arduino Mega.

I do not use the Nextion libraries.

I use an 'if' statement to see whether data is being sent from the Nextion display. My problems are as follows:

  • The 'if' statement is activated a) without me pressing any button b) I press a button on the screen which is not supposed to send data (e.g. changing pages on the display)
  • The 'if' statement is always being called even if nothing is pressed.
  • The data received from the Nextion display is not readable. I have tried sending it as HEX or as a string and neither works.

When using the Arduino Mega I have tried using Serial1 with the appropriate pins, but then I can not send data or receive data at all with the Nextion display.

I have used the following tutorials:

This is my code for the Arduino Uno:


int outputVal = 0; // int value for sensor

int pot = A0; // initialising analog pin for sensor


void setup() {
  pinMode(pot,INPUT);
  pinMode(11, OUTPUT);   //RGB Red Pin 
  pinMode(10, OUTPUT);   //RGB Green Pin
  pinMode(9, OUTPUT);    //RGB Blue Pin
  
  Serial.begin(9600);   


}

void loop() {

  

  if(Serial.available()){                               // If statement to see if data is available.                      
    Serial.println("If");                               // Print test to show if the 'if' statement was initialized.
    String data_from_display ="";                       // String variable to store data from display
    delay(30);                                          

    
    while(Serial.available()){      
     data_from_display  += char(Serial.read());        // Stores the data from the display
    }
    
    Serial.println(data_from_display);                 // Prints the received message from Nextion display on serial monitor
    
    
  }


  outputVal = map(analogRead(pot), 0, 800, 0, 100);   // Stores and converts sensor value
    
  Serial.print("t0.txt=\"");                          // Prints sensor value to Nextion Display
  Serial.print(outputVal);
  Serial.print("\"");
  Serial.write(0xff);
  Serial.write(0xff);
  Serial.write(0xff);                                 // Indicates end of message for Nextion Display  


  if(outputVal < 25)
  {
    
    Serial.print("t1.txt=\"LOW\"");                  // Prints status to Nextion Display
    Serial.write(0xff);
    Serial.write(0xff);
    Serial.write(0xff);
    
    Serial.print("t1.bco=63488");                    // Changes colour to Red on Nextion Display
    Serial.write(0xff);
    Serial.write(0xff);
    Serial.write(0xff);
    RGB_color(255, 0, 0);                             // Change RGB colour to Red
     
  } else if(outputVal > 75){
      
    Serial.print("t1.txt=\"HIGH\"");                // Prints status to Nextion Display
    Serial.write(0xff);
    Serial.write(0xff);
    Serial.write(0xff);
    
    Serial.print("t1.bco=63488");                   // Changes colour to Red on Nextion Display
    Serial.write(0xff);
    Serial.write(0xff);
    Serial.write(0xff);
    RGB_color(255, 0, 0);                           // Change RGB colour to Red
        
  } else {
    
    Serial.print("t1.txt=\"NORMAL\"");             // Prints status to Nextion Display
    Serial.write(0xff);
    Serial.write(0xff);
    Serial.write(0xff);
    
    Serial.print("t1.bco=2016");                  // Changes colour to Green on Nextion Display
    Serial.write(0xff);
    Serial.write(0xff);
    Serial.write(0xff);
    RGB_color(0, 255, 0);                          // Change RGB colour to Green
    
  }

  delay(100);                                     // Delay just to make reading on serial monitor easier will be removed
}



// Function to assign RGB Values

void RGB_color(int red_light_value, int green_light_value, int blue_light_value)
 {
  analogWrite(11, red_light_value);
  analogWrite(10, green_light_value);
  analogWrite(9, blue_light_value);
}

The code for the mega is very similar except I also initialize Serial1 and send the data to Serial1. But with the Mega, nothing works if I use Serial1. An example of the code for Arduino Mega:


Serial1.begin(9600);                

  Serial1.print("t0.txt=\"");                          
  Serial1.print(outputVal);
  Serial1.print("\"");
  Serial1.write(0xff);
  Serial1.write(0xff);
  Serial1.write(0xff);

The output on the Serial Monitor is as follows as it enters the 'if' statement and never exits. I understand the 255 (or hex 0xff) is the end of an instruction, but I do not understand why the 'if' statement is continuously being called and why I get random numbers like 126, 186, 90, and so on. The following is a screenshot of the Serial monitor

So, I am not sure if my code is the wrong way to receive input from the screen, or if my Nextion screen might be faulty. Any help will be much appreciated.

I've had a quick look at your code, you say you read my tutorial but you are not using my methods, no where do I use String and your code doesn't look like mine. Use my methods and I'll try to help if you get stuck.

While you can use a Uno I suggest you don't because you are then using the same serial port for the Nextion as the serial monitor, which is a pain.

Thank you for your response.

So I did try using the code from your tutorial. I even copied it directly from your files onto the display as well as onto the board. I used an Arduino Mega clone and a Nextion Enhanced 3.5" display.

My 1st issue is that if I use Serial 1, Serial 2, or Serial 3, the display does not receive any data. I am connecting the RX and TX pins correctly each time. But if I use the normal Serial port, (Pin 0 and 1) used for the Serial monitor it works, sort off. The clock updates, but the welcome text does not.

My 2nd issue is that I am not able to receive data from the display, it does not matter which Serial port I use.

I am sure that I did implement the display and the board correctly. I did a simple continuity test with all the connections and did not find any issue with any of them. So I am not sure what to do anymore? Could it be that I have a faulty screen or a faulty board?

My code and methods work, they work in my own projects and other people have used them successfully. If you are can't get them to work then something isn't right with what you are doing. If you want to pursue my methods then try again with my examples and if you get stuck ask on this topic and I will try to help. At the very least please post clear photos of your set up so I can check wiring, and upload your HMI file, you will have to put in a zip file format for the forum to accept it.

I do believe your method should work, but my problem is even if I take your code and HMI file and use it directly, it still does not work, and I do not understand why. I am confident my wiring is correct.

The first image are the wires from the display:

The second image are the wires from the display connected to the board for power and ground:

The third image are for the Serial1 communication connected to pins 19 and 18:

The HMI file I used is the same as the one in your tutorial. The only thing I changed was the display model.
HMI File.zip (136.6 KB)

Thank you, the wiring looks good but the file you sent me is the tft file, please upload the HMI file as you modified it so I can see what it does.

Please can you describe what happens when you use my examples. Please include photos of what is on the display.

How are you powering the Mega?

Thanks,

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.