Using Nextion displays with Arduino part 2Once it has finished disconnect the power and then remove the SD card. Connect the power again and the display should start up and look something like this:

Whatever version of display and Arduino you use make sure you know how to reference the serial port you are using. Because there are many Arduinos and clones, with varying numbers of serial ports, and because the choice of serial port is up to you, I cannot cover all possibilities in this tutorial. To develop this tutorial I used serial port 1 on an Elegoo Mega2560. You will need to edit the code to match the serial port you are using.
By default the Nextion serial port is configured for 9600 Baud and if you are new to Arduino or Nextion or both I recommend you leave it at 9600 Baud so that is one less thing to worry about if something doesn't work.
All you need to set up the Arduino for a Nextion display is:
void setup() {
//This is for the serial monitor. Remove the // if you need to use it.
//Serial.begin(9600);
//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.
Serial1.begin(9600);
//For demonstration
HMI_startup_message();
}
void HMI_startup_message() {
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);
}
Demonstration sketch and Nextion configuration.The demonstration sketch creates a real time clock using millis()as the timing source. If you have read the various Arduino tutorials or are experienced with Arduino you will know that millis() is not accurate enough for an accurate real time clock. The purpose of this tutorial is to demonstrate an Arduino controlling a Nextion, and a clock seemed a simple way to do so. I leave it to you to create a more accurate clock.
Sending data to the display.Sending to the display requires an understanding of the Nextion instruction set and what the display expects to receive. In the demonstration configuration there is a text box at the bottom with objname t0. To send text to this the display needs to receive.
t0.txt="Your text here"0xff 0xff 0xff
This is typical of any instruction, text or data, sent to the display. The display expects 0xff 0xff 0xff to indicate the end of the instruction.
For example:
void HMI_display_page(uint8_t page) {
Serial.print(F("t0.txt=""));
Serial.print(F("This is page "));
Serial.print(page);
Serial.print(""");
Serial.write(0xff);
Serial.write(0xff);
Serial.write(0xff);
}
Puts the page number on t0.
Sending data from the Nextion display to the ArduinoI developed my own way of sending data from the display to the Arduino, which has proved flexible enough to adapt to all the applications I have so far tried. The basic idea is to identify each object on a page by the page it belongs to, its type and an index of that type.
'Type' is whatever you want it to be. Your project will have any number of buttons, sliders and whatever else on each page, you can group similar ones together and give them the same type, which is a number from 0 to however many you need. I reserve type 0 for the page itself.
'Index of type' is a number from 0 to 1 less than however many instances of the type there are on the page.
Unlike the displays, I chose not to use 0xff 0xff 0xff as a terminator for the data sent to the Arduino. Instead I use 0xa5 0xa5 0xa5 as a start indicator followed by 5 bytes. Sometimes the last byte or 2 is just padding to bring the total to 5. You can modify the code to expect more or fewer bytes, or use a different start sequence. The 5 bytes are the page number, the type, the index of the type and 2 data bytes.
The receive function looks for 0xa5 repeated 3 times, then saves the next 5 bytes in an array, then uses 3 levels of nested switch statements to identify which object on which page sent the data. Each case of the lowest level switch statement represents an individual object on the display and can be used to call whatever code you need for your project. The receive function should be called once each time in the main loop and will empty the receive buffer each time it is called.