A Different Approach With Nextion

Introduction: A Different Approach With Nextion

In my first project with Arduino Nano connected to Nextion touch display, I had written a long series of commands to be communicated to Nextion via the serial port and this is inevitable if we need to send totally independent commands, in random moments.

I also have to admit that I spent more time in 'fighting' with Libraries than anything else. So I gradually came to work totally without the heavy ITEAD libraries.

I soon realized that I had no urgency to communicate to Nextion the changes to the attributes of visual objects, but I prefer to wait until I collected them and send them to Nextion as a whole, when I got a complete group.

I will try to explain myself better.

When in my project composed of 16 textual indications I want to turn some of them on or off, I do it by taking advantage of the 'bco' attribute that for switch on, passes (for example) from dark gray to white (if in a black rectangle), and vice versa to switch off.

In my application I found it useless to send 16 commands to the serial port in 16 different moments, one for each 'bco' of the 16 signals.

I prefer instead that Arduino collect which signals must be 'on' (HIGH) and which ones must be 'off' (LOW) in a 16-bit register, where each bit corresponds to one of the 16 signaling of Nextion.

After updating each bit of the register, I transmit its value to Nextion, a single message that contains a collective information concerning 16 elements.

In this way the communication from Arduino and Nextion is considerably reduced because in that single message transmitted on the serial to Nextion, information is collected that otherwise would have required the transmission of 16 messages.

True, it is not always necessary to update all reports, but I am sure that to do otherwise would waste more time.

Naturally each bit contained in the integer received by Arduino, the Nextion display will have to associate it to the desired attribute.

This means that code must be written in the Nextion display, but it is not to be frightened: if I succeeded ...

Then there is a double advantage: Arduino will have a lighter code and will be less engaged in serial communication with Nextion.

Nextion after receiving the data in a single message, will use them much more quickly than if it were to wait for 16 messages. The turning on or off of 16 signals will therefore be almost contemporaneous with respect to the most usual mode, in which the time for an unknown number of messages elapses between the execution of the command for the first signaling and the command for the last signaling.

In the Nextion display I created this system in the classic way, that is, turning a 'mask' register each time allows you to examine each of the 16 bits. When the bit examined is HIGH, the signal associated with that bit lights up on the display and turns off when a bit is LOW.

The 'negative' aspect of this system is that the code written in the Nextion display is less convenient to be documented than the Arduino code. Furthermore, the Nextion code risks being scattered over various objects. Care must be taken to document what you do right away.

I use Notepad ++ to write the code which I then copy into the Nextion object which is almost exclusively in tm0 of page 0.

The syntax of the Nextion language has numerous limitations, but it manages to overcome them or to get around them with a minimum of effort and to try to see the problems from points of view that are also unusual.

As an example, I report the way in which Arduino writes the register to be transmitted, written by me in the most elementary way possible.

Step 1: How the Register Is Transmitted

In the file ArduinoCode.PDF I show all my sketch. (Reading the code here down it's not so clear)
Here down, I want only to show in which way Arduino send the 16 bit Register to Nextion, without the help of the libraries, but jus respecting the syntax described by ITEAD.
<strong>void NexUpd()</strong>

SRSerial.print(InpReg);   // transmit the 16 collected bits to the Nextion Display
SRSerial.print(InpReg);   // transmit the 16 collected bits to the Nextion Display
  SRSerial.write(termin);   // 255 
 SRSerial.write(termin);   // 255 
 SRSerial.write(termin);   // 255

Step 2: .. But Before ....

Of course the code starts with all the declarations and the setup().

The Inputs are INPUT_PULLUP, so the input switches are normally open and when closed, they apply the GND to the correspondent input.

(This is my first Instructable and I'm sorry to show you my code in this bad way. Please download the file ArduinoCode.PDF that it's very clear.

Let me talk about it more

I have developed my own way to 'tell' to the Nextion display what it must do. Usually the MCU (Arduino in my case) send a message for every single variation to apply to the attribute of any single object.
This methode waste much time to do things not always so urgent to load continuously the Serial Line. I found more convenient that Arduino collect into 16 bit registers the information about the attributes to vary on Nextion. About every 500 mS, my Arduino send to Nextion one message containing the 16 bit contained in each register at time. Obviously in Nextion we need the code that handle what must be execute. This distribution of the task (and the code) let get many other advantages. For example, think how to make blink a light! With my approach it's easy: set a bit into the Arduino register and send it to Nextion. The Nextion twin registers could be updated from Arduino very rarely, because the blink frequency it's independent from the communication; the blink frequency is depending from a Timer object into Nextion and can run with the minimum time base near to 50 mS. So with my method we can blink a light in Nextion at frequency relatively high (suppose 2 Hz), even if my Arduino send messages every 10 seconds, just for an extreme example. This can suggest the opposite problem: how to do if the Communication fail? This isn't the object of this discussion, but I've already resolved this problem with a kind of Watch Dog: one inside the Arduino Code, another into the Nextion code.

The blinking is regulated by Nextion code, where every light follows his proper rules: ON/OFF or GREEN/RED or also changing the written inside (or other more). I could tell some other things about my project but I prefer to wait your questions, before to add too much words not so easy for me to translate well as I would.

Step 3: Editing the Nextion Objects

Here is a part of the code I wrote with Nextion Editor on the tm0 object.

It does not escape our notice that with the 16 bits received from Arduino, the Nextion display does not just turn on and off the signals. For the moment I omit the explanations so as not to complicate understanding.

I'm a beginner and so it's better to download the Nextion code.PDF page instead to read the confused code here down. (I'm sorry this is my first instructable)

If you desire you can download the complete code "HMI" for this my application. The filename of this code is POW1225.HMI. It can run into your Nextion display NX4024T032 but to understand it you have to swim into many objects and look the code inside the small window of the editor. So I think that will more easy to look the main code, written in the file Nextion code.PDF

// Project POW1225.HMI 15 May 2019

// vACC (va0) Accumulator

// vINP (va1) Input Register xxxx xxxx xxxx xxxx

tm0.en=1 // tm0 Start

tm0.tim=50 // tm0 Time base 50 mS

// RDY ***************

vACC.val=vINP.val&0x0001 // Mask

if(vACC.val!=0) // Test RDY


tRDY.pco=BLUE // RED



tRDY.pco=GRAY // dark GRAY


// PWR ***************


if(vACC.val!=0) // Test PWR


tPWR.pco=GREEN // light GREEN

tPON.txt="ON" // ON

tPON.pco=GREEN // light GREEN



tPWR.pco=GRAY // dark GRAY 33808

tPON.txt="OFF" // OFF

tPON.pco=GRAY // dark GRAY 33808


// DRY ***************


if(vACC.val!=0) // Test DRY






tDRV.pco=GRAY // dark GRAY 33808

tDRY.pco=GRAY // dark GRAY 33808


// RUN ***************


if(vACC.val!=0) // Test RUN


tRUN.bco=RED // MARCIA RED (on)

tRUN.pco=BLACK // on BLACK


tDIR.pco=BLACK // on BLACK



tRUN.bco=32768 // MARCIA GRAY (off)

tRUN.pco=GRAY // on GRAY

tDIR.bco=32768 // DIR dark GREEN 1024


tDIR.txt="---" // STOP


// LEFT **************


if(vACC.val!=0) // Test RUN Right


tDIR.txt="<<< " // DIR LEFT


// RIGHT *************


if(vACC.val!=0) // Test RUN Left


tDIR.txt=" >>>" // DIR RIGHT


// BOTH **************


if(vACC.val==24) // Test RUN both


tDIR.txt=">>!<<" // DIR BOTH


// TEST **************


if(vACC.val!=0) // Test TEST



tsw tTEST,1 // Enable Touch events



tTEST.pco=GRAY // dark GRAY 33808

tsw tTEST,0 // Disable Touch events


// FAULT *************


if(vACC.val==0) // Test FAULT


tFLT.pco=GRAY // FAULT absent




tFLT.pco=YELLOW // FAULT present


// EME ***************


if(vACC.val==0) // Test EME


tEME.pco=GRAY // EME absent




 tEME.pco=RED   // EME present


// FERMO *************

if(vACC.val!=0) // Test FERMO







tFER.bco=672 // dark GREEN


// *******************


I want to give my acknowledgements to Gideon Rossouwv because reading his Instructables I have gained quickly part of my objectives. Thank you mr. Gideon Rossouwv

Arduino Contest 2019

Participated in the
Arduino Contest 2019

Be the First to Share


    • Puzzles Speed Challenge

      Puzzles Speed Challenge
    • "Can't Touch This" Family Contest

      "Can't Touch This" Family Contest
    • CNC Contest 2020

      CNC Contest 2020