Go Down

Topic: Using Nextion displays with Arduino (Read 15656 times) previous topic - next topic


Nov 22, 2019, 04:17 pm Last Edit: Nov 22, 2019, 04:26 pm by Redpiperbob
Hi Perry
I have just started with the Nextion Displays and I am trying to your demo pro to work without much success. I am using an Arduino Mega and an Enhanced  Nextion  NX4024K032-011
First of all with your sketch as it is on the web site and the nextion library config.h file modified as :-

Code: [Select]
//#define dbSerial Serial
#define nexSerial Serial1

And the Mega to Nextion linked as:-
Nextion RX (yellow) to TX1 (pin18) on the Mega
Nextion TX (yellow) to RX1 (pin19) on the Mega

I can get the page buttons to swop pages and the + and - buttons to change colour when pressed and the slider to slide. But that all.  No time.

If I change the the config.h to :-
Code: [Select]
#define nexSerial Serial
and change your sketch to serial
and the mega to Pins 0 and 1 (TX0)

and open the serial monitor I can see the time , if I press the + or - on the display the digits alter in the serial monitor and I get a message if I move the slider.
Can you help please?


Hi Bob,
The code you have posted is not from the code I use in the demonstration.
First of all with your sketch as it is on the web site and the nextion library config.h file modified as :-
My code does not use the Nextion library.

Go back, remove all references to any Nextion libraries and use my code exactly as provided.

Nothing in my code should change the colour of anything. The + and - buttons set the time on the first page.


Ok Perry gone back to the start downloaded the hmi and ino and uploaded to Nextion and Mega. Run nothing comes out on the display.. The buttons seem to work and the slider moves.
If I change  all the "Serial1" in the ino to "Serial" then move the display leads to the pin 0 and 1 fo the Mega and open the serial monitor i can alter the time in the monitor by pressing buttons and i get a message if i move the slider.
which proves your program is working fine.
I just dont know what I am dooing wrong


Hi Bob,
Don't connect the Nextion to 0 and 1, these are used by the serial monitor. On a Mega you should have blue on 19 (RX1) and yellow on 18 (TX1).

Can you confirm that the Arduino code you are using is the code in:
Using Nextion displays with Arduino part 3 ?
The code there is both shown in code tags in the main message and as a downloadable file at the end of the post.
Also confirm that you have selected Board: Arduino / Genuino Mega or Mega 2560 under Tools in the IDE.

Also confirm that the Nextion HMI file you are using is the one in 2019-07-25 Arduino Nextion demo 43.zip , which can be found at the end of the first part of the tutorial.

In order to check that I've not made any mistakes with my code I have just downloaded both the Arduino code in part 3 and the HMI file from part 1 and uploaded them to a Mega and my Nextion and checked they work.

When you reset the Mega you should see the initial message on the Nextion at the bottom, which reads:

Nextion demonstration by Perry Bebbington. For support go to https://forum.arduino.cc/index.php?board=7.0
And the clock in the middle should start running from 00:00:00


Nov 22, 2019, 09:16 pm Last Edit: Nov 22, 2019, 09:19 pm by Redpiperbob
Hi Perry
I tryied all four TX/RX positions. but i knew that the correct two were 18/19.
Yes I am using the HMI from part one and the INO from part three.
After pluging in and reseting the display does not change.
I think I may have a fualty display. I only have the one I will order a new one tomorrow.



Is it possible to send the Nextion a command from an Arduino to set the page?  Sending something like "page 3" doesn't work, although it does work locally on the Nextion.

Theron Wierenga


Dec 04, 2019, 02:46 pm Last Edit: Dec 04, 2019, 09:13 pm by PerryBebbington
Hi Theron,
Here is some demonstration code for a Nextion with 5 pages, this changes the pages 0, 1, 2, 3, 4 then back to 0. You can change the number of pages by changing pageCount to however many pages you have. I have set the baud rate to 9600 as that is the default for a Nextion.

Code: [Select]

/* Demonstrates changing pages with the Arduino telling the Nextion which page to change to */
/* Uses the Nextion configuration 'Nextion_additional_features', which can be found in part 4 of this tutorial */
/* Tested on a Mega using serial port 1 connected to a NX4827T043_11 */

#define waitTime 1000       // Delay between each change of page
#define pageCount 5         // Number of pages on the Nextion
#define serialBaud 9600     //Baud rate for serial monitor
#define NextionBaud 9600    //Baud rate for Nextion

void setup() {
  Serial.begin(serialBaud);       // For the serial monitor
  Serial1.begin(NextionBaud);     // For the Nextion

void loop() {

void change_page() {
  static uint8_t page = 0;    //Current page on the Nextion
  static uint32_t previousMillis;
  uint32_t currentMillis = millis();
  if (currentMillis - previousMillis >= waitTime) {
    previousMillis = currentMillis;
    Serial.print("Page changing to ");      // Prints onto the serial monitor
    Serial1.print("page ");                 // Tells the Nextion to change pages
    page = ++page % pageCount;


If you have a question about THIS TUTORIAL then feel free to reply and ask it here.

If you have a question about YOUR PROJECT that happens to include a Nextion display then read 'how to use this forum - please read', choose a suitable forum and post a new question there.

Go Up