Introduction: Writing From Arduino Uno to Nextion

Picture of 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.

Step 1: Make Your Interface

Picture of 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

Picture of 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

Picture of Test

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

Comments

uludirik (author)2017-11-23

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.

uludirik (author)uludirik2017-11-23

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

BJ43 (author)2017-06-17

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?

Gideon Rossouwv (author)BJ432017-06-19

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

BJ43 (author)Gideon Rossouwv2017-06-19

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

Mr_contractor (author)BJ432017-08-01

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.

nukte (author)BJ432017-07-30

Hi;

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

Thank you

BJ43 (author)nukte2017-07-31

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

BJ43 (author)Gideon Rossouwv2017-06-20

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

Gideon Rossouwv (author)BJ432017-06-20

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

uludirik (author)2017-03-17

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.
Gideon Rossouwv (author)uludirik2017-03-17

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

NguyenthanhC (author)2017-03-04

Thanks for the Instructable.It's verygood and so helpfull for any newone to play with Nextion.I spend days to find out this simply but good way to mke Arduino and Nextion work together.

Thank you so much.

Sincerely

BillS37 (author)2017-01-05

Thanks for the Instructable. I'm having fun playing with my new Nextion.
I can't wait to try out your Arduino code as it seems very straight
forward......Except for the part where you're driving a device that
consumes 90ma from a port (7) that only supplies 40ma max. Maybe a
transistor should be used to supply the required current. I hope nobody
fries their Nextion.

Gideon Rossouwv (author)BillS372017-01-06

Hi,

I hope the code works well. You are absolutely right about the power thing. I will change that soon.

Regards,
Gideon

sdtag (author)2016-09-14

I got this to work, thank you.

Instead of writing numbers in a box can you tell me if I can change a button's background color based on if a certain digital input is hi or low in the Arduino?

Or an analog input, can it change colors that way also?

Thanks again

Gideon Rossouwv (author)sdtag2016-09-15

Hi,

When in the Nextion editor, look at your button's properties. Properties in bold and green are the only properties you can set from the Arduino. The bco (colour) property of the button is green but not bold, so I suspect that you cannot change its colour.

Nextion has a dual state button however. It has a property "val" that indicates the button state and is editable via the Arduino. In short it is possible, but you'll need to use a dual state button of which you'll change the "val" property with the Arduino to get different colours.

Regards,

Gideon

sdtag made it! (author)2016-09-14

I added some buttons at the bottom of the screen so I can dim or brighten the screen. I have a 3d printer and made a wall plate for it so I can mount it in my wall. It will just barely fit into a single gang box.

Gideon Rossouwv (author)sdtag2016-09-15

Hi,

That looks cool. I am glad you could expand it for your own purpose.

Regards,
Gideon

About This Instructable

7,679views

11favorites

License:

More by Gideon Rossouwv:Writing From Arduino Uno to Nextion Getting Started With Arduino and Nextion (pt 3)Peltier Fermentation Chamber for Beer
Add instructable to: