Start a new topic

Arduino and Nextion Display

 Hi every one.
I got a nextion display recently to learn and it work and create projects with it, I was wondering how can I control the page shown on the display from arduino, meaning that if there is page 0, page 1, and page 2, how can I switch between them from an arduino input like a sensor or a switch.
I tried but I does not work, I do not know why, can any body help me with that, including the library link you are using for that, because I there might be a different library for nextion.

From within the Nextion Editor

  Help menuitem of the Help menu

   - launches Nextion HMI Solutions page

First link of Related Downloads

  Itead's Arduino Library for Nextion 

examples folder contains CompPage.ino

 - uses Release Event of Page

   Send Component ID checked in HMI designed file to trigger if viewed in NexPage.h and NexPage.cpp

  basically parses out to serially send page page0ÿÿÿ

See example code, see library code compare 
Must declared properly
  NexPage page0 = NexPage(0, 0, "page0");

well I downloaded the library. 
once I try uploading an example, I get error compiling regarding dbserial : dbSerialPrintln("setup begin");

I think it has to do with serail 1 and 2, because I am using Arduino Uno which has only serial 1.
I do not know what to do about that either 

Consult the Uno documentation.  Consult the library files.

Perhaps the instructions in the library (text file)

   may shed light on how to configure for Uno.

Certainly when serials are not configured correctly

  one can indeed see failures as an expected behaviour.

Examine file named NexConfig.h

When Uno has only one serial it cannot be used for two purposes

   so dbserial to Serial Monitor will need to be disabled.

nexSerial will need to be set to the correct Serial as per MCU.

Thank you for the help I re configured the file, and the function is working easily.
just right now the other function I was using " Dim" for brightness stopped working because it uses another version of the library.  


  //Dim display.Power save.

    int bright = map(15, 0, 1024, 0, 100);

  String dim = "dim=" + String(bright);



is there any other function for dimming display than this one ? 

Why is there another function needed?

the Iteadlib library's sendCommand is able to send 

how ? 
when I use :  Nextion.sendCommand(dim.c_str());
it give me an error compiling that : 

exit status 1

'Nextion' was not declared in this scope

Examine the code as told earlier.

Even in NexPage.cpp you'll see how to use sendCommand.

Forget myNextion and just use the Iteadlib library

Examine the code

O got it to work. 
sending serial data dim=.... followed by 0xff 0xff 0xff 
thanks again for the help 

 I have a question about-

if (strcmp(buffer, "ON")

Is it reading the actual text within the b0 button?


is that just how Nextion reads to say that the b0 button has been pressed?

?  what you presented was a partial code snippet

what its function is needs more code, source and reason.

Nextion uses a 0x65 Return Data (as per Instruction Set) to tell if touch is pressed and released.


void b0PushCallback(void*ptr)


   Nexbutton *btn = (NexButton *)ptr;

   memset(buffer, 0, sizeof(buffer));

   btn->getText(buffer, sizeof(buffer));

   if (strcmp(buffer,"ON"))    


      digitalWrite(9, HIGH);

      digitalWrite(7, HIGH);

      strcpy(buffer, "ON");


The button b0 text says "ON"  is Nextion reading the text within the button?


Nextion Fish Tank Project


button b0 to turn on relay


So i can get a simple relay to turn on.

Read the code ...

strcmp compares two strings, in the case of

  if (strcmp(buffer,"ON"))

the contents of buffer is compared to constant text of ON

In the previous lines

  btn is declared as NexButton


  button->getText() calls member function in NexButton.cpp NexButton::getText();

  NexButton::getText() function issues get command to retrieve button text

  (as seen in the library code)

  retrieved text is stored in variable buffer (char array)

  strcmp then compares

Answer: code requests Button .txt attribute, Nextion sends it.

Relays are not turned on/off by text of ON/OFF

Relays are turned on/off by voltages set at required

Therefore digitalWrite is what turns relay on or off.

Button Text of ON or OFF is showmanship - irrelevant

Understood....Appreciate it

Thank you

  I use code attached from Nextion Arduino example and the only thing that happens, is my relay 1 turns on by itself and does not turn off.  I have a MEGA 2560 connected at TX2/RX2. I use an 8 channel relay module(thought that may be a problem),so i used a 1 channel relay module. I get same result.

  I remember this being my problem in the beginning which sent me on a 2 month reading and writing athon to come back full circle with the same relay doing the same thing. 

  The only thing I change in the code is the bit of code posted below with the buttons. 


NexButton b0 = NexButton(1, 1, "b0");

NexButton b1 = NexButton(1, 2, "b1");

NexButton b2 = NexButton(1, 3, "b2");

NexButton b3 = NexButton(1, 4, "b3");

my page 0 is a title page with page button to go to page 1 with menu buttons and i am trying to get those buttons to turn on relays. I can not see anything I am doing wrong because I have yet to do anything right to see what is wrong.

  I am extremely happy with the way my HMI looks and want to spend more time improving it, but I am stuck trying to get it to work with my Arduino.  (if) i can even figure out how to get it to work, i know my next issue will be formatting other code examples to fit within my buttons. But, I believe if I can get passed this issue, I can have a better understanding of how it all may fit and work together.  

Again I appreciate any time and assistance for help trying to figure this out.  

Thank you 

Login or Signup to post a comment