Nextion displays include either a resistive touch panel (RTP) or capacitive touch panel (CTP) and provide an easy way to create a touch screen interface, or human machine interface (HMI) for your project. The displays require a spare serial port to communicate with them. To get the best from them requires an understanding of how to drive them, and what works and doesn't work. The advice here is based on my work with multiple projects using Nextion displays with both PIC and Arduino hosts. I don't claim the ideas here are the best way or the only way to control one with an Arduino, just what I have found to work well in my projects. The sample code has been written by me and tested on an Elegoo Mega2560, and you are free to use it, modify or improve as much as you like. My methods and the examples shown here do not use any libraries as I never found them necessary, the displays are easy enough to drive without a library.
If you prefer to use a library have a look at Easy Nextion Library by Seithan, his methods are different to mine, choose which works best for you (I cannot help you with Seithan's methods).
If you prefer to use the official Nextion libraries please see Ray Livingston's improved Nextion libraries.
I do not represent Arduino or Nextion.
Everything that follows in this tutorial is about my methods.
The displays have their own instruction set which can be found here. The instructions provide the means to control the displays either through messages sent to the serial port or from using the touch screen. This tutorial and sample code uses some of the Nextion instructions and assumes you have made yourself familiar with them from the Nextion web site.
The displays are configured using a WYSIWYG editor, which you can download from here.The sample configuration attached to this tutorial can be opened with this editor, which you will need to use the example here. (Latest version is V1.61.2 added autumn 2020)
Compatibility with Arduino
I have used the basic 4.3" RTP version (NX4827T043) and the enhanced 7" CPT version (NX8048K070) with a WeMos M0, MKR WiFi 1010, Nano Every and Elegoo Mega2560 without problems. There are problems using one with an ESP8266, see 'using Nextion displays with Arduino part 4' further down this tutorial. If you have a Uno then you could try John Harrison's SerialWing, which allows the single serial port on the Uno to be shared with the display, and provides a separate serial port to configure the display. I have not tried other versions or other Arduinos. As the only requirement is a spare serial port on the Arduino to connect the display to, I would expect that any Arduino with a spare serial port would work with any Nextion display, but I've not tried combinations other than those mentioned here.
You will need:
• The attached 'Arduino Nextion demo 43V1 HMI', which can be opened with the Nextion editor.
• The attached 'Nextion_demonstration43.ino', which can be opened with the Arduino IDE.
• An Arduino with at least 1 spare serial port.
• A Nextion display.
• A micro SD card.
Connecting the Nextion display to your Arduino
The displays come with a cable with 4 wires. I have only ever seen them in the following colours but if you get one with different colours please let me know so I can update this part of the tutorial.
• Red - for connecting to +5V
• Black - for connecting to 0V
• Blue - Transmit data from the display to the Arduino, connect to RX on the Arduino
• Yellow - Receive data from the Arduino to the display. Connect to TX on the Arduino
Note, Arduinios use one serial port for communication with your PC, do not use this serial port for connection to your Nextion display, use a spare one.
The displays run off 5v. All the ones I have output 3.3v on their serial transmit (blue wire). I have not had any problems using them with a WeMos M0 at 3.3v, an Elegoo Mega2560 at 5V or a PIC running on 5v. However, please check the voltage on the display transmit (blue) wire is not higher than maximum voltage your Arduino can tolerate.
To connect a Nextion to an Arduino you need one free serial port on the Arduino. Connect the TX from the Arduino to the yellow wire and the RX to the Arduino to the blue wire. You will also need 0v to the black wire and +5v to the red wire. The current drawn by the display depends on the model and how bright the back light is. The 7" CPT display NX8048K070 requires up to 550mA, the smaller ones less. The 4.3" NX4827T043 used for this tutorial draws 235mA. In my experience they generate quite a bit of noise on the supply and, although not essential, I recommend a 470μF (or larger) capacitor across the supply soldered to the connector on the back (see reply #68 for more about this).
Please see photo below:
This shows the back of the display with a 470μF capacitor soldered to the outside 2 connections of the connector. Note that it is essential that the negative connection of the capacitor goes to GND and the positive to +5V, otherwise you will fill your room with nasty smelling smoke and scare your cat.
This demonstration was created for the basic 4.3" RTP version, NX4827T043. You can adapt my configuration for a different display using the Nextion editor.
Open the attached file 'Arduino Nextion demo 43V1.HMI' with the Nextion editor. If you are using a smaller display move the position of any objects that are outside the display area of the smaller display by changing X and Y for each object to a value that puts the object inside the boundaries of your display. Once you have done this select DEVICE ID from the menu and select your display from the list. If you have a bigger display you don't need to move anything around, just select the correct device ID.
Compile the display configuration by clicking 'compile'.
Click on File > Open build folder, this should open a window with a file 'Arduino Nextion demo 43V1.tft'
Copy this file, and only this file, to a micro SD card (There must be nothing else on the SD card)
Put the micro SD card into the card reader slot on the side of the display and connect the black wire to 0V and the red wire to 5V. You should see the display updating, which takes about 5 seconds.
2021-07-03 Arduino Nextion demo 43.zip (15.6 KB)