I can't manage to send data (a number) from Arduino to Nextion

I have been some days trying to send any kind of data (numbers, text, etc) to my Nextion screen but I can't make it work and I don't know how. I beg for help.
I'm writting the Serial.write(0xFF) three times too but it never works for me, it only sends the data to the monitor serie instead of sending it to the Nextion. I am using the #include <Nextion.h> library.

#include <Nextion.h>
#define Blue 11

int MaxCounter = 30;
int SpeedCounter = 50;
int MinCounter = 20;


NexButton bMax0      =   NexButton(0, 6, "bMax0");
NexButton bMax1    =   NexButton(0, 7, "bMax1");
NexButton bSpeed0    =   NexButton(0, 9, "bSpeed0");
NexButton bSpeed1    =   NexButton(0, 8, "bSpeed1");
NexButton bMin0    =   NexButton(0, 11, "bMin0");
NexButton bMin1    =   NexButton(0, 10, "bMin1");

NexTouch *nex_listen_list[] =
{

  &bMax0,
  &bMax1,
  &bSpeed0,
  &bSpeed1,
  &bMin0,
  &bMin1,
  NULL
};

void Max0(void *ptr)
{
  if (MaxCounter != 5) {
    MaxCounter = MaxCounter - 1;
    Serial.print("nMax.val=");
    Serial.print(MaxCounter);
    Serial.print(0xff);
    Serial.print(0xff);
    Serial.print(0xff);
  }
}

void Max1(void *ptr)
{
  if (MaxCounter != 40) {
    MaxCounter = MaxCounter + 1;
    Serial.print("nMax.val=");
    Serial.print(MaxCounter);
    Serial.write(0xff);
    Serial.write(0xff);
    Serial.write(0xff);
  }

}

void Speed0(void *ptr)
{
  if (SpeedCounter != 0) {
    SpeedCounter = SpeedCounter - 1;
    Serial.print("nSpeed.val=");
    Serial.print(SpeedCounter);
    Serial.write(0xff);
    Serial.write(0xff);
    Serial.write(0xff);
  }
}

void Speed1(void *ptr)
{
  if (SpeedCounter != 100) {
    SpeedCounter = SpeedCounter + 1;
    Serial.print("nSpeed.val=");
    Serial.print(SpeedCounter);
    Serial.write(0xff);
    Serial.write(0xff);
    Serial.write(0xff);
  }
}

void Min0(void *ptr)
{
  if (MinCounter != 0) {
    MinCounter = MinCounter - 1;
    Serial.print("nMin.val=");
    Serial.print(MinCounter);
    Serial.write(0xff);
    Serial.write(0xff);
    Serial.write(0xff);
  }
}

void Min1(void *ptr)
{

  if (MinCounter != 35) {
    MinCounter = MinCounter + 1;
    Serial.print("nMin.val=");
    Serial.print(MinCounter);
    Serial.write(0xff);
    Serial.write(0xff);
    Serial.write(0xff);
  }
}


void setup() {
  nexInit();
  Serial.begin(9600);  // Start serial comunication at baud=9600

  pinMode(Blue, OUTPUT);


  bMax0.attachPush(Max0);
  bMax1.attachPush(Max1);
  bSpeed0.attachPush(Speed0);
  bSpeed1.attachPush(Speed1);
  bMin0.attachPush(Min0);
  bMin1.attachPush(Min1);
}

void loop() {
  nexLoop(nex_listen_list);
}

Can you also provide the model and it's info in the post, a schematic of your connections, and pictures confirming the connections.

I have another tutorial about using Nextion HMI at the top of the display category of the forum, I suggest you will find it useful.

Your code is way too complicated for trying to send a number. Start again with some simple code that just sends a slowly incrementing number and nothing else. Get that right before adding anything else.

I've yet to read a comment here where someone says how good the official Nextion libraries are.

1 Like


These are the connections. The model of Arduino is Mega 2560. I will try to write an easier and send it here, but I already did that too and I was just getting the same result.
I will also take a look at that other way witgout using the Nextion libraries.

Still need a schematic and to see how it's actually hooked up while you are trying to use it. I'm not seeing power going to the Arduino board and if you don't have the 0v commons tied together, that could stop communications too.

1 Like

That's what Serial.write() does. It sends stuff to the serial monitor.

Your display is connected to RX2 and TX2. You should be using Serial2.write() to send stuff to the display.

But even that might not work as you expect. I'm not an expert in Nextion displays. Follow @PerryBebbington 's advice. Follow @apf1979 's advice too.

There is no power going to the Arduino because I gave up on it today so I disconnected it. But even conecting the 0v commons together it still doesn't work. I don't know how to make a schematic here, but these connections are simple, the 5v and GND of the Nextion are connected to the microUSB, the TX of the nextion os connected to the RX of Arduino, and the RX of Nextion to the TX of Arduino.
As PaulRB said, I also tried using Serial2.write() and didn't work either. I even tried Serial1 and Serial3 desesperately but nothing worked.

Okay. When you're ready to try again, please connect it with the commons tied together, the code writing to Serial2 and a copy to Serial so you can see it in the serial monitor, and share the exact state and code you're testing while asking for help.

Please also share which model screen you're using. Preferably with a link to the product and a data sheet.

Hello!
I wrote it again today using the Serial2 and it works now, probably I was missing something yesterday since I was burned out. Anyways thanks everyone because I know I'm not very good at programming and your messages have helped me to solve this problem.
Have a good day!

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.