Start a new topic

Creating a menu with nextion and arduino

Hi everyone!

I am starting to do things with nextion and I want to create a simple menu so that the arduino know in which page this.


If I use this code, should it work?


Thanks for the attention


#include "NexButton.h"
#include "Arduino.h"
#include "NexPage.h"
#include "Nextion.h"

NexButton b0 = NexButton(0, 1, "b0");
NexButton b1 = NexButton(1, 1, "b1");
NexPage page0 = NexPage(0, 0, "page0");
NexPage page1 = NexPage(1, 0, "page1");

NexTouch *nex_Listen_List[] = 
    &b0,  &b1, &page0,

void b0PopCallback(void *ptr)

void b1PopCallback(void *ptr)

void setup(void)

void loop(void)





Seems reasonable

 - when you examine Nextion.h, it includes all the other .h files

    so NexButton.h and NexPage.h become redundant lines in code.

Thanks for the reply

Removed the nextion library, the menu does not work either.

a greeting

I am certainly not going to compile in my head.

You must pay attention to errors given by compiler.

The compiler dont give me errors.


I think the problem is with the bookstores. If I do not use libraries, the screen works, if I use them, the screen does nothing.


I use a mega arduino, connecting RX to Arduino TX and TX to Arduino RX.


What can be wrong? In nexconfig it is in serial2

On Mega, Nextion needs to be in RX2 and TX2 for it to use Serial2?

1 person likes this

I connect the arduino to Serial2 pins (16 & 17). 

The serial monitor give me this: recvRetCommandFinished err

Whats happends??


When you review the code of the library

- such return is in NexHardware.cpp and is expecting a return value from Nextion

  if all was okay Nextion would return 0x01 0xFF 0xFF 0xFF

  if this was not received or another error code then it returned the result you see.

  (Refer to the Nextion Instruction Set for bkcmd and Nextion Return Data)

So why has it not received?

  is bkcmd set to 3 in nexInit() function

  is wiring correct, wires not fractured, grounds common, etc

  (pin numbers mean little to me, I do not/can not memorize 10000 boards)

  has library been altered or customized

  is set up and nexSerial as per NexConfig?

The library has not been modified. Wiring is correct. This is connected to rx2 and tx2.


I do not know what may be happening so it does not work correctly ..


Supposedly, unmodified any of the libraries should work in mega arduino?

Sorry, You are the one that MUST know.

Must know what you are dealing with

 - electrically, programming - when in doubt return to datasheets

- what is Nextion baud rate?

- what is MCU baud rate?

- how is your grounds connected?

- what is setup of power supply?

- Nextion Model I dug for from your HMI, you did not mention

But really can not solve on other side of globe with no details

Your code looks good if

 - wired correctly - also means supply and grounds, not just RX/TX

 - if Nextion device baudrate is same as nexInit()

 - MCU side code matches HMI design (it did match)

To just say doesn't work isn't good enough

- you must say what is not working and the why it is not working

- Multimeter: Amps and Volts, wire continuity

Can not tell anything of your setup from a code snippet.

Answering your questions:


The speed of nextion is 9600 baud.

MCU speed does not know what it is.

The ground are well connected because without libraries would  work well

The supply is correct because the same reason as before.

I use a nextion screen NX8048T050_011.


The baud rate is the same as nexInit ().


The code matches screen and arduino.

I am assuming Mega as it is status line of your Screenshot.

Start back at NexConfig.h

  - do you have a line of

    #define nexSerial Serial2

    You are stating those pins are your Serial2

    are they reversed?

Then go to NexHardware,cpp

  - locate nexInit() function

  - the line with dbSerialBegin() is baudrate for Serial Monitor


  - the line with nexSerial.begin() is baudrate MCU tries with Nextion


  - the line with bkcmd is your bkcmd setting, see Nextion Instruction Set


So code looks fine ... but what is your wiring?

  - I dug up you are using NX8048T050_011 from your HMI

     - I don't know how you have wired, but I assume this is so

  - The data sheet states it needs what power supply for this model?

    - this is found Help menuitem of Help Menu

      (launches Nextion HMI Solutions page in webbrowser - locate your model, select datasheet)

  - How you feed your Nextion supply is important.

     Itead clearly warns in big graphic about under supply  ... 5V 1A recommended

I can not tell any of your power supply or how it is connected

I also can not assume you made no mistakes in this area

  - as code looks okay, and you say it don't work

I am not going to look up datasheet for the Mega

  - I have a way different project going - but I am not librarian

-- got your new reply.

How do you know Nextion Baud is 9600?

  - did you set it to this with bauds=9600 command in an HMI before?

  - is this the reported baud rate you see when uploading project to Nextion?

  when it is not currently working - you need to confirm

The ground are well connected because:

   without libraries would work well. 

This is flawed, makes assumptions, but says nothing about connection

The supply is correct because same reason

Again tells nothing about connection.

Where is Nextion getting its power supply from?

  USB-to-TTL serial adapter, MCU, other?

     is this 5V 1A per datasheet for 5" model?

  Is it connected to MCU 5V line on MCU board?

     does the MCU datasheet say this is 5V 1A

Many boards are unable to provide 5V 1A on 5V output line

  many have limits, I assume your MCU also has limit

  such limit is found in datasheet for your MCU.

This is why where and details of such makes difference.

especially if MCU is feed with USB supply and Nextion pulls also from this

If Nextion and MCU have different power supplies

Their grounds need to be connected common

Why is so much being said about how this is configured?

The Code matches.  HMI and Arduino code is same.

So when error is not in code ... most likely outside of code

If you said it worked before without libraries

int toggle = {0};

byte ter[3] = {255,255,255};

void setup (void) {



void loop (void) {

   toggle = 1-toggle;

   if (toggle==1)


      Serial2.print("page 1");


   } else {

      Serial2.print("page 0");





Then this should toggle between page 1 and page 0

If it doesn't then issue is not in code

The problem was that the display connector was broken and not good connection. Now, everything works correctly.


Sorry for the problems.


Thank you very much for all the help you received.

Login or Signup to post a comment