Using Nextion displays with Arduino

Hi,

I am using a Nextion Display with an Uno. I was able to get my project working perfectly on the Nextion Simulator connected to the Uno. Once I use the actual display connected to pins 0 and 1, I can only read values and text from the Arduino. The commands don't seem to make it from the display to the Arduino. I tried your Arduino clock program with the HMI file you posted and I am getting the same result (your clock counts, but button presses don't respond). Do you have any troubleshooting advice for me? I've looked at every forum and researched as much as I can to this point.

Also, another thing to note: I also have an Arduino due. When I load the same program to it and connect the Nextion to pins 0 and 1, the exact opposite happens. data and text on the Nextion no longer appear, but some of the buttons work. I also have two different Nextion screens and same results on both.

Any help on this would be greatly appreciated.

Please see the last few responses between me and Squid171 above and see if that solves your problem.

[EDIT] See Using Nextion displays with Arduino

I have followed your advice to Squid171 before I posted here. I have also tried some other troublshooting techniques you've suggested to others throughout this forum. I have recently tested continuity from the tx/rx connections of the Nextion to the back of the arduino circuit board for pins 0,1 and that came back fine. Your program results exactly the same as mine. It works perfectly on the NextionEditor Simulator, but only shows the time values when I just plug in the display(after commenting out the above said line of code). I'm also powering the Uno externally and unplugging the USB hoping that would make a difference.

I asked the mods to separate your question from the tutorial as I think there are enough questions and answers in there for most things.

Please can you post your code, not my original code, as modified by you and exactly as you are using it?

Please use code tags, not using code tags is one of my pet hates!

void setup() {
  //This is for the serial monitor. Remove the // if you need to use it.
  //Serial.begin(9600);
  

  //Serial.println("Serial On"); //Print this messages when the serial port is connected
  //This is for serial port 1, which is the one used for the Nextion in this demonstration. You might need to change this depending on which serial port you are using for your display.
  Serial.begin(9600);

  //For  demonstration
  HMI_startup_message();
}

void loop() {
  HMI_read();
  clock_run();
}

struct CLOCK {
  int8_t hour;
  int8_t minute;
  int8_t second;
};

struct CLOCK clk;

//This displays the clock
void HMI_display_clock() {
  char timestring[9];
  sprintf(timestring, "%02d:%02d:%02d ", clk.hour, clk.minute, clk.second);
  //Serial.println(timestring);
  Serial.print(F("t1.txt=\""));
  Serial.print(timestring);
  Serial.print("\"");
  Serial.write(0xff);
  Serial.write(0xff);
  Serial.write(0xff);
}

//This displays the page number
void HMI_display_page(uint8_t page) {
  Serial.print(F("t0.txt=\""));
  Serial.print(F("This is page "));
  Serial.print(page);
  Serial.print(F("\""));
  Serial.write(0xff);
  Serial.write(0xff);
  Serial.write(0xff);
}

void HMI_P1_display_slider(uint8_t slider_d1, uint8_t slider_d0) {
  uint16_t slider_val = (slider_d1 <<8 | slider_d0);
  
  //This displays byte 1 of the slider value in HEX
  Serial.print(F("t2.txt=\""));
  Serial.print(slider_d1, HEX);
  Serial.print(F("\""));
  Serial.write(0xff);
  Serial.write(0xff);
  Serial.write(0xff);
  
  //This displays byte 0 of the slider value in HEX
  Serial.print(F("t3.txt=\""));
  Serial.print(slider_d0, HEX);
  Serial.print(F("\""));
  Serial.write(0xff);
  Serial.write(0xff);
  Serial.write(0xff);

  //This displays the complete slider value in decimal
  Serial.print(F("t4.txt=\""));
  Serial.print(slider_val);
  Serial.print(F("\""));
  Serial.write(0xff);
  Serial.write(0xff);
  Serial.write(0xff);
}

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

//HMI_read takes the data sent from the Nextion to the serial port and processes it depending on what has been sent
//There are 3 levels of nested switch statements corresponding to the page, the type of object and the index of the object.
void HMI_read() {
  static uint8_t HMI_read_data[10];         //This is a temporary buffer to hold the data from the display. Space for 10 bytes although this demonstration only uses 6 bytes
  static uint8_t HMI_read_data_i;           //This is a count of how many bytes have been received from the display.
  static uint8_t a5count;                   //0xa5 repeated 3 times is used as a start indicator, this is a count of how many times it has been received.
  uint8_t readtemp;                         //This is to hold the last received byte to ensure that it is only read from the receive buffer once.
  
  while (Serial.available() > 0) {         //Read every byte in the receive buffer
    readtemp = Serial.read();
    if (readtemp == 0xa5) {                 //Count the number of times 0xa5 has been received
      ++a5count;
      if (a5count > 2) {
        a5count = 0;
        HMI_read_data_i = 0;
      }
    }
    else {
      a5count = 0;
    }
    HMI_read_data[HMI_read_data_i] = readtemp;
    if (HMI_read_data_i == 5) {
      switch (HMI_read_data[1]) {             //HMI_read_data[1] contains the page the data has come from
        case 0:                               //Case 0 means the data has come from page 0
          switch (HMI_read_data[2]) {         //HMI_read_data[2] contains the type of object on the page that the data has come from
            case 0:                           //In this demonstraton case 0 selects a page
              HMI_display_page(HMI_read_data[3]);
              break;
            case 1:                            //In this demonstration case 1 is a button for setting the clock
              switch (HMI_read_data[3]) {      //HMI_read_data[3] is the index of the type of button, so 0 to 5 as there are 6 buttons for setting the clock. Each case is a different button.
                case 0:
                  ++clk.hour;
                  if (clk.hour > 23) {
                    clk.hour = 0;
                  }
                  break;
                case 1:
                  --clk.hour;
                  if (clk.hour < 0) {
                    clk.hour = 23;
                  }
                  break;
                case 2:
                  ++clk.minute;
                  if (clk.minute > 59) {
                    clk.minute = 0;
                  }
                  break;
                case 3:
                  --clk.minute;
                  if (clk.minute < 0) {
                    clk.minute = 59;
                  }
                  break;
                case 4:
                  ++clk.second;
                  if (clk.second > 59) {
                    clk.second = 0;
                  }
                  break;
                case 5:
                  --clk.second;
                  if (clk.second < 0) {
                    clk.second = 59;
                  }
                  break;
              }
              HMI_display_clock();
              break;
          }
          break;
        case 1:                               //Case 1 means the data has come from page 1
          switch (HMI_read_data[2]) {
            case 0:                           //Data from the page itself, this is the post initialisation request to update the page, which displays the page number
              HMI_display_page(HMI_read_data[3]);
              break;
            case 1:                           //Data from the slider on page 1
              HMI_P1_display_slider(HMI_read_data[5], HMI_read_data[4]);      //HMI_read_data[5] is byte 1 of the slider value, HMI_read_data[4] is byte 0 of the slider value
              
              
              
              break;
          }
          break;
      }
    }
    ++HMI_read_data_i;
    if (HMI_read_data_i > 9) {
      HMI_read_data_i = 9;
    }
  }
}

void clock_run() {
  static unsigned long previousMillis;
  unsigned long currentMillis = millis();
  const unsigned long interval = 1000;
  if (currentMillis - previousMillis >= interval) {
    previousMillis = currentMillis;
    ++clk.second;
    if (clk.second > 59) {
      clk.second = 0;
      ++clk.minute;
      if (clk.minute > 59) {
        clk.minute = 0;
        ++clk.hour;
        if (clk.hour > 23) {
          clk.hour = 0;
        }
      }
    }
    HMI_display_clock();
  }
}

The only place I modified was changing the clock struct base tag to clk since it wasn’t compiling. I did not change the HMI program at all.

Thanks for your code.

Your code works perfectly on a Uno with a Nextion connected with my demonstration Nextion configuration.

To check:
You should have the blue wire connected to Rx (0) and the yellow wire to Tx (1).
While uploading your code to the Uno you must disconnect at least the blue wire otherwise it will interfere with the upload as it shares the same serial port.

Have you edited the HMI file at all?

Other than that I don’t know what to suggest.

That is how I have it wired. I've tried two different Nextion screens with an Uno and a Due. I find it odd that I get different results with different boards.

I have not edited the HMI file at all. I figured it wasn't a code problem since the NextionEditor simulator works perfectly with the board. Maybe it's possible that my Rx pin on the Uno isn't working properly, but I doubt it. I just don't know what my next step should be since we know everything should work.

Thanks for your help.

Maybe it's possible that my Rx pin on the Uno isn't working properly, but I doubt it.

Maybe, or the connection isn't as good as you think it is. Never, ever ever assume something that could be causing the problem is OK.