Close

Programming the Nextion 7" panel

A project log for Building the Thor robot

Building a 6-axis robot based on the Thor robot. When size DOES matter!

olaf-baeyensOlaf Baeyens 07/01/2017 at 21:041 Comment

For the Thor robot I use the Ultratronics v1.0 pro board which is basically an Arduino Due.

The touch panel that I want to use is the Nextion 7" HMI panel.

These are the steps that I had to take to make the panel work with the Ultratronics board.

The panel layout is develop with the Nextion editor. Then compiled, put on a micro SD card and inserted on the display. Power off-on and it gets loaded into flash. Remove the micro SD card and power off and on then the display is active.

The graphical layout depends on your graphical skills.

Important to that we have these buttons marked as b0, b1 and b2. This will come back in our Arduino programming.

Also make sure that for every button you have activated a event when the button gets released. Thsi event is then transmitted through the serial TxD and received by the Arduino.


Get the software for the Nextion found here: https://github.com/itead/ITEADLIB_Arduino_Nextion

Then copy it to the location of your Arduino libraries or your local project.

The Ultratronics is an Arduino Due, and this code is not compatible for the Arduino Due simply because SoftwareSerial.h does not exist for the Due. It took some time to find what I had to do with it.

We need to do 2 modifications

So open file NexUpload.cpp

Now comment out the #include SoftwareSerial.h, since this prevents you to compile for the Arduino Due.

And since we will use one of the additiopnal UART's let's comment out the USE_SOFTWARE_SERIAL.

Next step we will define what UART we will use so open NexConfig.h

As described in my previous log I have hooked it to Serial1. (note the "1"). Also note that Serial (without the number should maybe defined as Serial0)

Now how do we use it?

In your Arduino project

#include "Nextion.h"

/*
* Declare a button object [page id:0,component id:1, component name: "b0"].
*/
NexButton b0 = NexButton(0, 1, "b0");
NexButton b1 = NexButton(0, 2, "b1");
NexButton b2 = NexButton(0, 3, "b2");

char buffer[100] = { 0 };

/*
* Register a button object to the touch event list.
*/
NexTouch *nex_listen_list[] =
{
	&b0,
	&b1,
	&b2,
	NULL
};

/*
* Button component pop callback function.
* In this example,the button's text value will plus one every time when it is released.
*/
void b0PopCallback(void *ptr)
{
	uint16_t len;
	uint16_t number;
	NexButton *btn = (NexButton *)ptr;
	memset(buffer, 0, sizeof(buffer));

	/* Get the text value of button component [the value is string type]. */
	btn->getText(buffer, sizeof(buffer));

	number = atoi(buffer);
	number += 1;

	memset(buffer, 0, sizeof(buffer));
	itoa(number, buffer, 10);

	/* Set the text value of button component [the value is string type]. */
	btn->setText(buffer);
}

Notice in the project b0, b1 and b2 defined as in the Nextion editor.

This code actually increments and send the number to that button. On your Nextion display you get the incremented number on that button every time you press it.

void setup() {
	Serial1.begin(9600);

	/* Set the baudrate which is for debug and communicate with Nextion screen. */
	nexInit();

	/* Register the pop event callback function of the current button component. */
	b0.attachPop(b0PopCallback, &b0);
	b1.attachPop(b1PopCallback, &b1);
	b2.attachPop(b2PopCallback, &b2);
        ...
}

We attach the defined buttons and callback to execute for each button.

void loop() {
	/*
	* When a pop or push event occured every time,
	* the corresponding component[right page id and component id] in touch event list will be asked.
	*/
	nexLoop(nex_listen_list);

}

And finally the loop that searches for events and call the callback function.

This screen is not intended for streaming video, the fact that the HMI takes all the processing power makes he serial communication actually pretty efficient and low on CPU cycles on the Arduino.

Discussions

heitorbadotti2 wrote 02/20/2019 at 12:42 point

Hello, thanks a lot for your tutorial... You helped me to make my Nextion works in my Arduino DUE...

But I got another problem, when I was using Arduino MEGA, I was receiving commands from display without problem... Now in Arduino DUE 90% of sends fails... What should be the problem?

Thanks, Heitor.

heitorbadotti2@hotmail.com

  Are you sure? yes | no