Start a new topic

Getting Number Value From Nextion

Hello Guys

I want to read a number from a nextion component n0 and use it as pwm call to control the motor speed. Some people say i can not trigger arduino to read the number without a button push/pull event..

I found a previous topic about this and i copied the code to my own code, but it does not compile saying numonNextion does not name a type. To be honest I dont know where its coming from, I just copied it.

I am attaching hmi and ino files. 

Help is greatly appreciated/

(378 Bytes)
(1.95 MB)

First this is ill formed NexNumber declaration

   NexNumber numonNextion(2,7,"n0");


When declaring numonNextion you need to include the NexNumber constructor .  Until numonNextion is defined properly your compiler doesn't know to what it is referring to

   NexNumber numonNextion = NexNumber(2,7,"n0");


Next I see a get n0.val in b2 and not b3.  I am assuming you want to use b2 and b3 to haul the number down.  I wouldn't push the number without first notifying the MCU to be ready for it.  So ditch get n0.val from the Touch Release event in b2.  Both b2 and b3 have their send component IDs checked, so there should be nothing else needed Nextion side. 

Arduino side, b2 and b3 need to be declared, added to the listen list, attachPop added and callbacks defined.

Your sketch should look something like: 


#include <Nextion.h>
// Edited to fix &up and &dn typos

int const motorPin = 9;           // motor pin
int motorSpeed = 0;
int32_t mynumber;

// declare Nextion side components
//    Component localname = Component(pageno, .id, ".objname");
NexNumber numonNextion = NexNumber(2,7,"n0");
NexButton   up = NexButton(2,4,"b2");
NexButton   dn = NexButton(2,5,"b3");

NexTouch nex_listen_list[] = {   // listen for these
    &up, &dn, NULL

void doonup(void *ptr) {    // function for up
   numonNextion.getValue(&mynumber);   // now get n0.val
   motorSpeed = mynumber;
   analogWrite(motorPin, motorSpeed);
   // delay(100) ... delay 100 not needed here 
   // doonup only called when button released

void doondn(void *ptr) {    // function for down
   numonNextion.getValue(&mynumber);  // now get n0.val
   motorSpeed = mynumber;
   analogWrite(motorPin, motorSpeed);
   // delay(100) ... delay 100 not needed here 
   // doondn only called when button released

void setup() {
  //  Serial.begin(9600);  Serial already setup with begin in nexInit()
  pinMode(motorPin, OUTPUT);
  up.attachPop(doonup, &up);   // attach release action for b2
  dn.attachPop(doondn, &dn);   // attach release action for b3

void loop() {


And for the record, in programming there is always a way to do something. So not to confuse the situation (best path forward listed above), but if a timer is used MCU side to fetch numonNextion.getValue(&number); every timer cycle, one could circumvent needing the buttons being pressed, but the method I recommended above is still the way to go - frees your MCU to do other things as needed.

Hello Patrick,

Thank you very much for your input.

That get n0.val in nextion was just a copy paste error :)

I thought it would be much simpler for me as a beginner to get the MCU to read the number in n0.val and use it to control the motor speed. I will give this one a shot. 

For some reason, I am having a problem with compiling with the code above, getting the error message 'nexnumber does not name a type'

I have downloaded few libraries and tried them, still getting the same error.



Sorry if there is typos.  nexnumber also need be NexNumber.  Case sensitivity

Hi Patrick,

It was a library issue for UNO, I replaced some modified files and it compiled.

But I am wondering, how could i get the n0 value to Arduino without button trigger?



you can send a "get" request ...



Let's try with  "that source code with some litle modifying " .I got good compiling but not yet test it.After it works by the way touch press or touch/released event> I can make it work whithout these conditions.


Ahh, my apologies, typos.

As Gerry stated, the get request is how to do without.

If you examine NexNumber.cpp .getValue() function

 - you will note it parses out to get n0.valÿÿÿ sent over to sendCommand() in NexHardware.cpp

 - sendCommand() in NexHardware.cpp adds the three byte 0xFF 0xFF 0xFF termination.

So it is doing exactly as Gerry stated.

The .getValue() also includes retrieving the 8 byte Nextion 0x71 Return Data and parsing the number.

So without a button touch event triggering the number update

int32_t mynumber;

NexNumber numonNextion = NexNumber(2,7,"n0");


But there should really be a reason / condition /  event as to when to request the number

and it is inside this condition or event the retrieval should occur

Otherwise, one haphazardly puts such unbridled retrieve in side loop() to occur 1000 per sec

Hello Patrick,

Would it work efficiently to receive the value of number component using getValue with a delay of 100ms? 

Hello Nguyen,

I have a code at hand (the courtesy of Ignacio Fontana) which looks a bit different than yours in some aspects. I's now working, i just get some errors if I press the buttons fast. I attached the file. Could you please check the difference?

(1.84 KB)

Delay is usually always a design flaw. If the MCU is stuck on line delay(100); it won't catch incoming bytes at end of loop() or process those bytes in nexLoop(nex_listen_list); until delay(100); is completed.   1/10th of a second is enough time for 96 bytes (9600bps) to come in (or 1152 bytes at 115200bps); 

One should always truly rethink when and if such a delay() is necessary as delay is an "all stop until"

Vo= analogRead(ThermistorPin);

  Tc =25+(Vo-495)/11; // tu 25--40 doC



   uint32_t number;



I saw :the Arduino send numbers to Nextion or get numbers from Nextion and then return it to Nextion very simple and easyly But I can not used the number get from Nextion to calculate with the the variable by the arduino side for example:


Please help me Mr Patrick Martin.

Thanhk you very much.

Nextion is not floating point but integer in uint32_t format.

I can explain the Iteadlib - as I have done,

but users must learn any Arduino coding at Arduino


Let's try with ths source code ( I used this way the void repeat every 400ms) You can adjust it.


Hello Nguyen & Patrick

Actually, I made it work with the code you first posted(Patrick's example code) and also the code in the attachment. Although they both work, the code structure seems different. Such as, in one code, there is nexLoop(nex_listen_list), and in the code attached, there is also analogWrite(motorPin, motorSpeed);

There are also other differences. Can you tell me what the difference can cause between those codes?

(1.84 KB)
Login or Signup to post a comment