Nextion Display Arduino Mega Communication Problem

I have a 7 inch Nextion Touch Display and an Arduino Mega. I already connected the two via the serial connection. I also uploaded a Nextion file to the display with a button. So I wrote a very simple program on the Arduino IDE to turn on an LED when pressing a button. Unfortunately it doesnt work. I used the ITEADLIB Nextion library and my code looks like this.

#include <Nextion.h>

NexButton b0 = NexButton(0, 1, "b0");

NexTouch *nex_listen_list[] =

void b0PushCallback(void *ptr)
  digitalWrite(7, LOW);


void setup(void) {
  digitalWrite(7, HIGH);

  b0.attachPush(b0PushCallback, &b0);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);

void loop(void) {

I hope someone can help me out.

Why do you change the serial line speed in setup?
Don’t you need to make sure the transmit buffer is empty before doing it?
Edit: sorry, missed the “.end()”.

Also, this

Installation & Troubleshooting -
For problems with Arduino itself, NOT your project

As AWOL hinted, the problem is changing the serial data speed on the fly. You are sending the instruction to the Nexion to change the speed then immediately changing it on the Maga. I doubt the 'b' of baud will have left the buffer before you change it on the Mega. This is covered in my tutorial Using Nextion displays with Arduino.

Best thing is to change it in the Nextion configuration.

You are also using serial port 0 for the Nextion, well, at least your code is. Serial port 0 is used for the USB serial connection for to your PC, trying to use it for the Nextion is also asking for trouble. Use one of the other serial ports.