Writing From Arduino Uno to Nextion

Hi all,

In previous instructables (1, 2, 3) we looked at how to send data from the Nextion to your Arduino Uno. We did this without using the libraries. Now I'd like to show you how you can send commands from the Arduino to a Nextion screen without the libraries. You can write almost any command that we find in the instruction set to the Nextion from the Arduino.

Teacher Notes

Teachers! Did you use this instructable in your classroom?
Add a Teacher Note to share how you incorporated it into your lesson.

Step 1: Make Your Interface

For this instructable you can just download the interface. There is no code anywhere in the interface. It is much simpler than the previous instructables.

Step 2: Connect Your Components

Refer to the picture and connect your Nextion to the Arduino. You should also connect the circuit.

The idea of this circuit is just to have 2 resistors with different values connected in series. In series resistors are voltage dividers. We can measure the different voltages over the resistors and output that reading to the screen. You can also measure ground and +5V to see if they are indeed read as 0 and 1023 by the Arduino.

EDIT: In the past I connected the Nextion to a digital pin for power, but this is not recommended. If possible rather power your Nextion from the 5V pin on the Arduino or an external 5V power supply.

Step 3: Upload the Arduino Sketch

Download the sketch and upload it to your board. I tried to add enough comments to the sketch to make it understandable.

Step 4: Test

Play around with your A0 pin's jumper and read different voltages across the circuit. The value on the screen should change accordingly.

Share

Recommendations

  • Indoor Lighting Contest

    Indoor Lighting Contest
  • Make It Fly Challenge

    Make It Fly Challenge
  • Growing Beyond Earth Maker Contest

    Growing Beyond Earth Maker Contest

26 Discussions

0
None
tulabob

9 months ago

Hi, excellent instructable. You can change the properties of the progress bar thus:

sendThis = "j0.pco="; //Build the part of the string that we know
sendThis.concat(65520); //Add the variable we want to send
writeString(sendThis); //*Use a function to write the message character by character to the Nextion
where pco is the colour of the bar - the above number gives yellow

sendThis = "j0.bco="; //Build the part of the string that we know
if (num>75)
{
sendThis.concat(63488); //Add the variable we want to send
}
else
{
sendThis.concat(1024); //Add the variable we want to send
}
writeString(sendThis); //*Use a function to write the message character by character to the Nextion

and this change the background colour of the bar from green to red when the variable num exceeds 75.

You can alter any of the 'green' properties in the Nextion editor - afaik :-)

Regards
Richard

1 reply
0
None
Burn_bgtulabob

Reply 2 months ago

Thank you for your comment.
Can you please give me some ideas how to send float to num field.
I tried dtostrf, but it doesn't work neither with num field neither text field.

0
None
BJ43

2 years ago

Love the simplicity of your code. Well done.

I assume 'n0' is on the first page. Is it possible to send value on another Nextion page and if so what is the code?

8 replies
0
None
Gideon RossouwvBJ43

Reply 2 years ago

Hi,

Can you maybe describe the case in what you want to use it? This will help me to give you the best answer as I have a couple of scenarios running through my head, but I am not sure which one will suit your needs.

Regards,

Gideon

0
None
BJ43Gideon Rossouwv

Reply 2 years ago

Hi Gideon,

I think I found out how. - Make simple project with two pages.

On each page put a number box. I have Page0 with n0 and Page1 with n1. Make both number boxes GLOBAL.

Run debug. - whilst on page0 type instruction 'page1.n1,val=100' - I get error.

BUT....... make both pages GLOBAL and type the instruction again and it works. Brilliant.

You can even change other parameters like colours, pictures etc.

Bob

0
None
UserInstruBJ43

Reply 3 months ago

Just a little note about syntax:
page1.n1.val=100 // separator must be dot, not comma.

0
None
Gideon RossouwvBJ43

Reply 2 years ago

Hi Bob,

That's great! Sorry I couldn't be of more help. Running through some scenarios and ran into the problem that Nextion doesn't have global variables. Luckily I was wrong and as you've pointed out one can change an object from local to global by changing the 'vscope' attribute. Thanks for teaching me something.

Regards,

Gideon

0
None
BJ43Gideon Rossouwv

Reply 2 years ago

Hi Gideon,
I have up to 6 WEMOS D1 mini clients sending temperature readings at 5 minute intervals to esp8266 server. I want to display: Sensor no., Date, Time, and Temperature on 3.5 inch Nextion (which I am awaiting delivery).
On second Nextion page I want to add these readings to Waveform plots. I can do this with Nextion editor debug instruction 'add 1,0,25' - where 1 is the waveform id, 0 is the channel and 25 is the temperature. (This will then show temperature history for many hours). But it only works if the plot is on first page.

It would be good to know how to push value/text of text, numbers and waveforms from Arduino/esp8266 using your simplified method to other pages.
Any help will be appreciated.
Thanks,
Bob

0
None
Mr_contractorBJ43

Reply 2 years ago

Hi again,

I sent a request from a different account (nukte) asking about your wemos clients sending to an ESP8266 server.

At my work, I would like to send temperatures from several wemos minis to a wemos host, and, while you see all sorts of ideas, most use the internet to upload to a service and then back to the host.

Because my application requires privacy I may not upload to cloud (etc.) but have to use the network inside of the building. So information may flow from wemos to router to host, but not out of the building. Any help is surely appreciated.

0
None
nukteBJ43

Reply 2 years ago

Hi;

I'm working on a project like yours, and wondered if you would allow me to read your code?

Thank you

0
None
BJ43nukte

Reply 2 years ago

Hi Nukte

My temperature project is quite big. It has RTC, SD card, oled as well as connection to Nextion and WiFi to remote sensors. You're welcome to what I have but its not for real beginner. Suggest you send me your PM with what you are seeking.

Will help in any way I can

Bob

0
None
uludirik

1 year ago

Hi Mr.Gideon

Thanks for yours sharing.

I made your project but,

ı created two diffretns page in nextion editor.

First pages is is potentiometer project.

Second pages is button&led project.
when I open the second page,
my nextion sends meaningless and very fast strings
for example; "2625525525526255255255262552552552625525525526255255255

2625525525526255255255262552552552625525525526255255255

2625525525526255255255262552552552625525525526255255255"

why did this ?

Thank you very much for your help.

2 replies
0
None
uludirikuludirik

Reply 1 year ago

MY ARDUINO CODES

#include <SoftwareSerial.h> //

SoftwareSerial mySerial(10, 11); // RX, TX

int KanalBir,KanalBir2,KanalIki,KanalIki2,KanalUc,KanalUc2;

int KanalBirBolme,KanalBirMod,KanalIkiBolme,KanalIkiMod,KanalUcBolme,KanalUcMod,ProgressBar;

int HavaHizAlarmi;

String message;

void setup() {

pinMode(0, INPUT);

pinMode(1,INPUT);

pinMode(2,INPUT);

pinMode(5,OUTPUT);

pinMode(13, OUTPUT);

pinMode(12, OUTPUT);

pinMode(2,OUTPUT);

pinMode(3,OUTPUT);

Serial.begin(57600);

while (!Serial) { //

;

}

Serial.println("Serial On");

mySerial.begin(9600);

}

void loop() {

Serial.println(message);

while (!mySerial.available()){

KanalBir=analogRead(0);

KanalBir2=map(KanalBir,0,1023,0,50);

KanalBirBolme=KanalBir2/10;

KanalBirMod=KanalBir2%10;

ProgressBar=map(analogRead(0),0,1023,0,100);

//--------------------------------------------------------------

KanalIki=analogRead(1);

KanalIki2=map(KanalIki,0,1023,0,50);

//--------------------------------------------------------------

ProgressBar=map(analogRead(0),0,1023,0,100); //

//--------------------------------------------------------------

if ((analogRead(0)) < HavaHizAlarmi){

digitalWrite (5,HIGH);

delay (250);

digitalWrite (5,LOW);

delay (250);

}

if (message.length() > 14 ){

//-------------------------FAN AÇIK-----------------------------

if (message[3] == '0' && message[4] == '4' ){

delay (10);

digitalWrite(13, HIGH);

message = "";

}

// --------------------FAN KAPALI---------------------------------

if (message[3] == '1' && message[4] == '4' ){

delay (10);

digitalWrite(13, LOW);

message = "";

}

//-------------------------ISIK AÇIK------------------------------------

if ( message[3] == '0' && message[4] == '5' ){

delay(10);

digitalWrite(12, HIGH);

message = "";

}

//-------------------------ISIK KAPALI-------------------------------

if (message[3] == '1' && message[4] == '5' ){

delay (50);

digitalWrite(12, LOW);

message = "";

}

//-----------------------YUKARI HAREKET BAŞLADI----------------------

if (message[4] == '2' && message[5] == '1' ){

digitalWrite(2, HIGH);

delay(5);

message = "";

}

//----------------------YUKARI HAREKET DURDU---------------------------------

if (message[4] == '2' && message[5] == '0' ){

digitalWrite(2, LOW);

message = "";

}

//------------------------AŞAĞI HAREKET BAŞLADI-----------------------------------

if (message[4] == '3' && message[5] == '1' ){

digitalWrite(3, HIGH);

delay(5);

message = "";

}

//---------------------------AŞAĞI HAREKET DURDU-----------------------------

if (message[4] == '3' && message[5] == '0' ){

digitalWrite(3, LOW);

message = "";

}

//---------------------------HAVA HIZ SINIR ALARMI--------------------------------

if (message[4] == '7' && message[5] == '0' ){

HavaHizAlarmi = 150;

}

//------------------GELEN KODLARI FİLTRELEME----------------------------------------

message = "";

}

// --------------------sensör bilgilerini tanımlayıp gönderme---------------------------

//----------------------------------------------------------

if (message[4] != '9' && message[5] != '9' ){

String sendThis = "";

delay(50);

sendThis = "n0.val=";

sendThis.concat(KanalBirBolme);

writeString(sendThis);

message = "";

delay (10);

//----------------------------------------------------------

String sendThis2 = "";

delay(50);

sendThis2 = "n1.val=";

sendThis2.concat(KanalBirMod);

writeString(sendThis2);

message = "";

delay (10);

//----------------------------------------------------------

String sendThis3 = "";

delay(50);

sendThis3 = "n2.val=";

sendThis3.concat(KanalIki2);

writeString(sendThis3);

message = "";

delay (10);

//-----------------------------------------------------------

String sendThis4 = "";

delay(50);

sendThis4 = "j0.val=";

sendThis4.concat(ProgressBar);

writeString(sendThis4);

message = "";

delay (10);

String sendThis5 = "";

//----------------------------------------------------

delay(50);

sendThis5 = "n6.val=";

sendThis5.concat(analogRead(0)); //GEÇİCİ

writeString(sendThis5);

message = "";

delay (10);

//---------------------------------------------------

}

if ( message[0] == '2' ){ // çürük gelen kodlar için filtreleme

message = "";

}

}

while (mySerial.available()){

message.concat(mySerial.read());

}

}

//---------------------SENSÖR DATALATININ SONUNA ONDALIK EKLEME-------------------

void writeString(String stringData) {

for (int i = 0; i < stringData.length(); i++)

{

mySerial.write(stringData[i]);

}

mySerial.write(0xff);

mySerial.write(0xff);

mySerial.write(0xff);

}// end writeString function

0
None
UserInstruuludirik

Reply 3 months ago

Hi.
I'm surely late to give you my answer. I'm quite sure that you've already find-out in the Nextion Instruction Set that the string 26255255255.... (and so on), starting with 26 that means 1A hex
N° 90x1A2,34Invalid Variable name or attribute0x1A 0xFF 0xFF 0xFFReturned when invalid Variable name or invalid attribute was used

0
None
RobD146

3 months ago

This was amazing for me have spent hours on this as a beginer now i have it working many thanks

0
None
roydouce8256

7 months ago on Step 4

This is the first time I got to send data to the nextion to work. Thank you great work.

0
None
aastha3010

9 months ago

Hi, I like your work. I want to connect the display with atmega128 my transmitter code is working with UART communication but how can I connect this UART code with the TFT display to send hello from atmega128? Please help me.

0
None
uludirik

2 years ago

I like your work very much. Thank you so much.

You sent a variable here, but how is TXT sent?


for example;

When you have a 5v input, you see the "active" TXT on the screen

i try this;

String sendThis = "";

Delay (50);

SendThis = "t1.txt =";

SendThis.concat ("active");


WriteString (sendThis);

But he did not work.
2 replies
0
None
DanielM1067uludirik

Reply 9 months ago

To change text use this:
sendThis = "text1.txt="; //Build the part of the string that we know
sendThis.concat('"'); // ( ' " ')
sendThis.concat("test");
sendThis.concat('"'); // ( ' " ')
writeString(sendThis);

0
None
Gideon Rossouwvuludirik

Reply 2 years ago

Hello. I am not at home to test it now, but try everything the same except change the one line to:
SendThis.concat(""active"");

The difference being using double inverted commas.

Regards,
Gideon