Arduino Nano RP2040 Serial and Nextion

Hello, I am new using Nextion Screens. I have issue loading the code and my believe is that the problem lies on the serial of RP2040 TX and RX.

when I try to upload/compile the code I get these error:

`C:\Users\Dario\Documents\Arduino\libraries\ITEADLIB_Arduino_Nextion-master\NexUpload.cpp:17:10: fatal error: SoftwareSerial.h: No such file or directory
#include <SoftwareSerial.h>
^~~~~~~~~~~~~~~~~~
compilation terminated.

exit status 1

Compilation error: exit status 1`

I am using library of Nextion Master and I have modified the library as recommended:

/** 
 * Define DEBUG_SERIAL_ENABLE to enable debug serial. 
 * Comment it to disable debug serial. 
 */
//#define DEBUG_SERIAL_ENABLE

/**
 * Define dbSerial for the output of debug messages. 
 */
#define dbSerial Serial

/**
 * Define nexSerial for communicate with Nextion touch panel. 
 */
#define nexSerial Serial

that did not work.

here is my complete code:


#include <Nextion.h>


////////////////////////// DECLARE OBJECT VARIABLES: ...

  NexButton b0 = NexButton(0, 26, "b0");  // Button added
  NexButton b1 = NexButton(0, 27, "b1");  // Button added
  NexButton b2 = NexButton(0, 28, "b2");  // Button added
  NexButton b3 = NexButton(0, 29, "b3");  // Button added
  NexButton b4 = NexButton(0, 30, "b4");  // Button added
  NexButton b5 = NexButton(0, 31, "b5");  // Button added
  NexButton b6 = NexButton(0, 32, "b6");  // Button added
  NexButton b7 = NexButton(0, 33, "b7");  // Button added
  
 // Declare pages:
  int variable1 = 0;  // Create a variable to have a counter going up by one on each cycle
  int counter = 0;  // Create a variable to have a counter for the + and - buttons
  int CurrentPage = 0;  // Create a variable to store which page is currently loaded


  NexPage page0 = NexPage(0, 0, "page0");  // Page added as a touch event
  // End of declaring objects


  //char buffer[100] = {0};  // This is needed only if you are going to receive a text from the display. You can remove it otherwise.
   // Further on this sketch I do receive text so that's why I created this buffer.

////////////////////////// END OF DECLARE OBJECT VARIABLES:

////////////////////////// DECLARE INPUTS:
  
  // Analog input pins
  #define SENSOR_PIN1 A1
  #define SENSOR_PIN2 A2

////////////////////////// END OF DECLARE INPUTS:

////////////////////////// DECLARE OUTPUTS:
  
  //Declare digital outputs
    const int modeValveBlowerPin = 2;
    const int modeValveDPPin = 3;
    const int pistonOpenEnablePin = 4;
    const int pistonOpenReleasePin = 5;
    const int pistonCloseEnablePin = 6;
    const int pistonCloseReleasePin = 7;
    const int mainValveOpenPin = 8;
    const int mainValveClosePin = 9;



////////////////////////// END OF DECLARE OUTPUTS:

////////////////////////// REGISTER TOUCH EVENT LIST: ...
  NexTouch *nex_listen_list[] = 
  {
    &b0,  // Button added
    &b1,  // Button added
    &b2,  // Button added
    &b3,  // Button added
    &b4,  // Button added
    &b5,  // Button added
    &b6,  // Button added
    &b7,  // Button added
    &page0,  // Page added as a touch event
  NULL  // String terminated
  };  // End of touch event list
////////////////////////// END OF TOUCH EVENT LIST:

////////////////////////// TOUCHE VENTS:

  //-------[B0]

    void b0PushCallback(void *ptr)  // Press event for button b0
    {
      digitalWrite(modeValveDPPin, HIGH);  // Turn ON internal LED
    }  // End of press event

    void b0PopCallback(void *ptr)  // Release event for button b0
    {
      digitalWrite(modeValveDPPin, LOW);  // Turn OFF internal LED
    }  // End of release event

  //-------[B1]

    void b1PushCallback(void *ptr)  // Press event for button b1
    {
      digitalWrite(modeValveBlowerPin, HIGH);  // Turn ON internal LED
    }  // End of press event

    void b1PopCallback(void *ptr)  // Release event for button b1
    {
      digitalWrite(modeValveBlowerPin, LOW);  // Turn OFF internal LED
    }  // End of release event

  //-------[B2]

    void b2PushCallback(void *ptr)  // Press event for button b2
    {
      digitalWrite(pistonOpenEnablePin, HIGH);  // Turn ON internal LED
    }  // End of press event

    void b2PopCallback(void *ptr)  // Release event for button b2
    {
      digitalWrite(pistonOpenEnablePin, LOW);  // Turn OFF internal LED
    }  // End of release event

  //-------[B3]

    void b3PushCallback(void *ptr)  // Press event for button b3
    {
      digitalWrite(pistonOpenReleasePin, HIGH);  // Turn ON internal LED
    }  // End of press event

    void b3PopCallback(void *ptr)  // Release event for button b3
    {
      digitalWrite(pistonOpenReleasePin, LOW);  // Turn OFF internal LED
    }  // End of release event

  //-------[B4]

    void b4PushCallback(void *ptr)  // Press event for button b4
    {
      digitalWrite(pistonCloseEnablePin, HIGH);  // Turn ON internal LED
    }  // End of press event

    void b4PopCallback(void *ptr)  // Release event for button b4
    {
      digitalWrite(pistonCloseEnablePin, LOW);  // Turn OFF internal LED
    }  // End of release event

  //-------[B5]

    void b5PushCallback(void *ptr)  // Press event for button b5
    {
      digitalWrite(pistonCloseReleasePin, HIGH);  // Turn ON internal LED
    }  // End of press event

    void b5PopCallback(void *ptr)  // Release event for button b5
    {
      digitalWrite(pistonCloseReleasePin, LOW);  // Turn OFF internal LED
    }  // End of release event
  //-------[B6]

    void b6PushCallback(void *ptr)  // Press event for button b6
    {
      digitalWrite(mainValveOpenPin, HIGH);  // Turn ON internal LED
    }  // End of press event

    void b6PopCallback(void *ptr)  // Release event for button b6
    {
      digitalWrite(mainValveOpenPin, LOW);  // Turn OFF internal LED
    }  // End of release event

  //-------[B7]

    void b7PushCallback(void *ptr)  // Press event for button b7
    {
      digitalWrite(mainValveClosePin, HIGH);  // Turn ON internal LED
    }  // End of press event

    void b7PopCallback(void *ptr)  // Release event for button b7
    {
      digitalWrite(mainValveClosePin, LOW);  // Turn OFF internal LED
    }  // End of release event

  //-------[B...]
  // ... Define callbacks for more buttons ...

////////////////////////// END OF TOUCH EVENTS:

////////////////////////// ANALOF READING FUNCTIONS:

  // Number of readings to consider for average calculation
  const int numReadings = 25;

  // Time interval for taking readings (0.1 second)
  const unsigned long interval = 100;
  unsigned long previousMillis = 0;

  // Array to store the readings
  float readings1[numReadings];
  float readings2[numReadings];
  int readIndex = 0;

  // Function to map analog readings to a signal range of 0-5V
  float mapToSignal(int reading) {
    // Map the analog reading from the range 55-1023 to 0-150 (0-150 psi)
    return (152.0 * (reading - 25)) / (1023 - 25);
  }

  // Function to calculate the average value from the stored readings
  float calculateAverage(float readings[]) {
    float sum = 0;
    for (int i = 0; i < numReadings; i++) {
      sum += readings[i];
    }
    return sum / numReadings;
  }

////////////////////////// END OF ANALOF READING FUNCTIONS:


void setup() {
  Serial.begin(9600);


  // You might need to change NexConfig.h file in your ITEADLIB_Arduino_Nextion folder
  // Set the baudrate which is for debug and communicate with Nextion screen
  nexInit();

  ////////////////////////// DEFINE PINS:

    pinMode(modeValveBlowerPin, OUTPUT);
    pinMode(modeValveDPPin, OUTPUT);
    pinMode(pistonOpenEnablePin, OUTPUT);
    pinMode(pistonOpenReleasePin, OUTPUT);
    pinMode(pistonCloseEnablePin, OUTPUT);
    pinMode(pistonCloseReleasePin, OUTPUT);
    pinMode(mainValveOpenPin, OUTPUT);
    pinMode(mainValveClosePin, OUTPUT);

  ////////////////////////// END OF DEFINE PINS:

  ////////////////////////// REGISTER EVENT CALLBACK FUNCTIONS:
    
    // Register the event callback functions of each touch event:
      b0.attachPush(b0PushCallback);  // Button press 
      b0.attachPop(b0PopCallback);    // Button release
      b1.attachPush(b1PushCallback);  // Button press 
      b1.attachPop(b1PopCallback);    // Button release
      b2.attachPush(b2PushCallback);  // Button press 
      b2.attachPop(b2PopCallback);    // Button release
      b3.attachPush(b3PushCallback);  // Button press 
      b3.attachPop(b3PopCallback);    // Button release
      b4.attachPush(b4PushCallback);  // Button press 
      b4.attachPop(b4PopCallback);    // Button release
      b5.attachPush(b5PushCallback);  // Button press 
      b5.attachPop(b5PopCallback);    // Button release
      b6.attachPush(b6PushCallback);  // Button press 
      b6.attachPop(b6PopCallback);    // Button release
      b7.attachPush(b6PushCallback);  // Button press 
      b7.attachPop(b6PopCallback);    // Button release
    // End of registering the event callback functions
   
  ////////////////////////// END OF REGISTER EVENT CALLBACK FUNCTIONS:
  
  for (int i = 0; i < numReadings; i++) {
      readings1[i] = 0;
      readings2[i] = 0;
      }
  //
}

void loop() {

  delay(30);
  
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis >= interval) {

    previousMillis = currentMillis;
    int sensorValue1 = analogRead(SENSOR_PIN1);
    int sensorValue2 = analogRead(SENSOR_PIN2);
    float signal1 = mapToSignal(sensorValue1);
    float signal2 = mapToSignal(sensorValue2);
    readings1[readIndex] = signal1;
    readings2[readIndex] = signal2;
    readIndex = (readIndex + 1) % numReadings;
    float averageValue1 = calculateAverage(readings1);
    float averageValue2 = calculateAverage(readings2);
    
  ////////////////////////// Serial PRINT VALUES:
      Serial.print(averageValue1, 2);
      Serial.print(",");
      Serial.print(averageValue2, 2);
      Serial.print(",");
      Serial.print(150);
      Serial.print(",");
      Serial.print(0);
      Serial.print(",");

    if(CurrentPage == 0){  // If the display is on page 0, do the following

        Serial.print("n1.val=");  // This is sent to the nextion display to set what object name (before the dot) and what atribute (after the dot) are you going to change.
        Serial.print(averageValue1, 2);  // This is the value you want to send to that object and atribute mentioned before.
        Serial.write(0xff);  // We always have to send this three lines after each command sent to the nextion display.
        Serial.write(0xff);
        Serial.write(0xff);

        Serial2.print(",");
            
        Serial.print("n2.val=");  // This is sent to the nextion display to set what object name (before the dot) and what atribute (after the dot) are you going to change.
        Serial.print(averageValue2, 2);  // This is the value you want to send to that object and atribute mentioned before.
        Serial.write(0xff);  // We always have to send this three lines after each command sent to the nextion display.
        Serial.write(0xff);
        Serial.write(0xff);
   
      Serial.println();
      }

  ////////////////////////// END OF Serial PRINT VALUES:
  }

  delay(interval);


}

could you help me figure what i am doing wrong?

thanks!

I'm not familiar with your board. Try to use Serial1 (I think that that is teh RX/TX pins) instead of Serial (the USB). Have a look at the schematic: https://content.arduino.cc/assets/ABX00053-schematics.pdf

Note:
You probably (not sure) don't have to change the library, just the sketch itself to use Serial1.

Thank you for the suggestion, I tried what you suggested


#include <Nextion.h>
// Connect the Nextion TX (transmit) pin to Arduino Digital RT (receive)
// Connect the Nextion RX (receive) pin to Arduino Digital TX (transmit)
// Connect to 5V independent Power Input
// Connect to Ground.


////////////////////////// DECLARE OBJECT VARIABLES: ...

  NexButton b0 = NexButton(0, 26, "b0");  // Button added
  NexButton b1 = NexButton(0, 27, "b1");  // Button added
  NexButton b2 = NexButton(0, 28, "b2");  // Button added
  NexButton b3 = NexButton(0, 29, "b3");  // Button added
  NexButton b4 = NexButton(0, 30, "b4");  // Button added
  NexButton b5 = NexButton(0, 31, "b5");  // Button added
  NexButton b6 = NexButton(0, 32, "b6");  // Button added
  NexButton b7 = NexButton(0, 33, "b7");  // Button added
  
 // Declare pages:
  int variable1 = 0;  // Create a variable to have a counter going up by one on each cycle
  int counter = 0;  // Create a variable to have a counter for the + and - buttons
  int CurrentPage = 0;  // Create a variable to store which page is currently loaded


  NexPage page0 = NexPage(0, 0, "page0");  // Page added as a touch event
  // End of declaring objects


  //char buffer[100] = {0};  // This is needed only if you are going to receive a text from the display. You can remove it otherwise.
   // Further on this sketch I do receive text so that's why I created this buffer.

////////////////////////// END OF DECLARE OBJECT VARIABLES:

////////////////////////// DECLARE INPUTS:
  
  // Analog input pins
  #define SENSOR_PIN1 A1
  #define SENSOR_PIN2 A2

////////////////////////// END OF DECLARE INPUTS:

////////////////////////// DECLARE OUTPUTS:
  
  //Declare digital outputs
    const int modeValveBlowerPin = 2;
    const int modeValveDPPin = 3;
    const int pistonOpenEnablePin = 4;
    const int pistonOpenReleasePin = 5;
    const int pistonCloseEnablePin = 6;
    const int pistonCloseReleasePin = 7;
    const int mainValveOpenPin = 8;
    const int mainValveClosePin = 9;



////////////////////////// END OF DECLARE OUTPUTS:

////////////////////////// REGISTER TOUCH EVENT LIST: ...
  NexTouch *nex_listen_list[] = 
  {
    &b0,  // Button added
    &b1,  // Button added
    &b2,  // Button added
    &b3,  // Button added
    &b4,  // Button added
    &b5,  // Button added
    &b6,  // Button added
    &b7,  // Button added
    &page0,  // Page added as a touch event
  NULL  // String terminated
  };  // End of touch event list
////////////////////////// END OF TOUCH EVENT LIST:

////////////////////////// TOUCHE VENTS:

  //-------[B0]

    void b0PushCallback(void *ptr)  // Press event for button b0
    {
      digitalWrite(modeValveDPPin, HIGH);  // Turn ON internal LED
    }  // End of press event

    void b0PopCallback(void *ptr)  // Release event for button b0
    {
      digitalWrite(modeValveDPPin, LOW);  // Turn OFF internal LED
    }  // End of release event

  //-------[B1]

    void b1PushCallback(void *ptr)  // Press event for button b1
    {
      digitalWrite(modeValveBlowerPin, HIGH);  // Turn ON internal LED
    }  // End of press event

    void b1PopCallback(void *ptr)  // Release event for button b1
    {
      digitalWrite(modeValveBlowerPin, LOW);  // Turn OFF internal LED
    }  // End of release event

  //-------[B2]

    void b2PushCallback(void *ptr)  // Press event for button b2
    {
      digitalWrite(pistonOpenEnablePin, HIGH);  // Turn ON internal LED
    }  // End of press event

    void b2PopCallback(void *ptr)  // Release event for button b2
    {
      digitalWrite(pistonOpenEnablePin, LOW);  // Turn OFF internal LED
    }  // End of release event

  //-------[B3]

    void b3PushCallback(void *ptr)  // Press event for button b3
    {
      digitalWrite(pistonOpenReleasePin, HIGH);  // Turn ON internal LED
    }  // End of press event

    void b3PopCallback(void *ptr)  // Release event for button b3
    {
      digitalWrite(pistonOpenReleasePin, LOW);  // Turn OFF internal LED
    }  // End of release event

  //-------[B4]

    void b4PushCallback(void *ptr)  // Press event for button b4
    {
      digitalWrite(pistonCloseEnablePin, HIGH);  // Turn ON internal LED
    }  // End of press event

    void b4PopCallback(void *ptr)  // Release event for button b4
    {
      digitalWrite(pistonCloseEnablePin, LOW);  // Turn OFF internal LED
    }  // End of release event

  //-------[B5]

    void b5PushCallback(void *ptr)  // Press event for button b5
    {
      digitalWrite(pistonCloseReleasePin, HIGH);  // Turn ON internal LED
    }  // End of press event

    void b5PopCallback(void *ptr)  // Release event for button b5
    {
      digitalWrite(pistonCloseReleasePin, LOW);  // Turn OFF internal LED
    }  // End of release event
    
  //-------[B6]

    void b6PushCallback(void *ptr)  // Press event for button b6
    {
      digitalWrite(mainValveOpenPin, HIGH);  // Turn ON internal LED
    }  // End of press event

    void b6PopCallback(void *ptr)  // Release event for button b6
    {
      digitalWrite(mainValveOpenPin, LOW);  // Turn OFF internal LED
    }  // End of release event

  //-------[B7]

    void b7PushCallback(void *ptr)  // Press event for button b7
    {
      digitalWrite(mainValveClosePin, HIGH);  // Turn ON internal LED
    }  // End of press event

    void b7PopCallback(void *ptr)  // Release event for button b7
    {
      digitalWrite(mainValveClosePin, LOW);  // Turn OFF internal LED
    }  // End of release event

  //-------[B...]
  // ... Define callbacks for more buttons ...

////////////////////////// END OF TOUCH EVENTS:

////////////////////////// ANALOF READING FUNCTIONS:

  // Number of readings to consider for average calculation
  const int numReadings = 25;

  // Time interval for taking readings (0.1 second)
  const unsigned long interval = 100;
  unsigned long previousMillis = 0;

  // Array to store the readings
  float readings1[numReadings];
  float readings2[numReadings];
  int readIndex = 0;

  // Function to map analog readings to a signal range of 0-5V
  float mapToSignal(int reading) {
    // Map the analog reading from the range 55-1023 to 0-150 (0-150 psi)
    return (152.0 * (reading - 25)) / (1023 - 25);
  }

  // Function to calculate the average value from the stored readings
  float calculateAverage(float readings[]) {
    float sum = 0;
    for (int i = 0; i < numReadings; i++) {
      sum += readings[i];
    }
    return sum / numReadings;
  }

////////////////////////// END OF ANALOF READING FUNCTIONS:


void setup() {

  while (!Serial);
  Serial.begin(9600); //Arduino Serial 
  Serial1.begin(9600); //Nextion Serial 
  delay(250);

  // You might need to change NexConfig.h file in your ITEADLIB_Arduino_Nextion folder
  // Set the baudrate which is for debug and communicate with Nextion screen
  nexInit();

  ////////////////////////// DEFINE PINS:

    pinMode(modeValveBlowerPin, OUTPUT);
    pinMode(modeValveDPPin, OUTPUT);
    pinMode(pistonOpenEnablePin, OUTPUT);
    pinMode(pistonOpenReleasePin, OUTPUT);
    pinMode(pistonCloseEnablePin, OUTPUT);
    pinMode(pistonCloseReleasePin, OUTPUT);
    pinMode(mainValveOpenPin, OUTPUT);
    pinMode(mainValveClosePin, OUTPUT);

  ////////////////////////// END OF DEFINE PINS:

  ////////////////////////// REGISTER EVENT CALLBACK FUNCTIONS:
    
    // Register the event callback functions of each touch event:
      b0.attachPush(b0PushCallback);  // Button press 
      b0.attachPop(b0PopCallback);    // Button release
      b1.attachPush(b1PushCallback);  // Button press 
      b1.attachPop(b1PopCallback);    // Button release
      b2.attachPush(b2PushCallback);  // Button press 
      b2.attachPop(b2PopCallback);    // Button release
      b3.attachPush(b3PushCallback);  // Button press 
      b3.attachPop(b3PopCallback);    // Button release
      b4.attachPush(b4PushCallback);  // Button press 
      b4.attachPop(b4PopCallback);    // Button release
      b5.attachPush(b5PushCallback);  // Button press 
      b5.attachPop(b5PopCallback);    // Button release
      b6.attachPush(b6PushCallback);  // Button press 
      b6.attachPop(b6PopCallback);    // Button release
      b7.attachPush(b6PushCallback);  // Button press 
      b7.attachPop(b6PopCallback);    // Button release
    // End of registering the event callback functions
   
  ////////////////////////// END OF REGISTER EVENT CALLBACK FUNCTIONS:
  
  for (int i = 0; i < numReadings; i++) {
      readings1[i] = 0;
      readings2[i] = 0;
      }
  //
}

void loop() {

  delay(30);
  
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis >= interval) {

    previousMillis = currentMillis;
    int sensorValue1 = analogRead(SENSOR_PIN1);
    int sensorValue2 = analogRead(SENSOR_PIN2);
    float signal1 = mapToSignal(sensorValue1);
    float signal2 = mapToSignal(sensorValue2);
    readings1[readIndex] = signal1;
    readings2[readIndex] = signal2;
    readIndex = (readIndex + 1) % numReadings;
    float averageValue1 = calculateAverage(readings1);
    float averageValue2 = calculateAverage(readings2);
    
  ////////////////////////// Serial PRINT VALUES:
      Serial.print(averageValue1, 2);
      Serial.print(",");
      Serial.print(averageValue2, 2);
      Serial.print(",");
      Serial.print(150);
      Serial.print(",");
      Serial.print(0);
      Serial.print(",");

    if(CurrentPage == 0){  // If the display is on page 0, do the following

        Serial1.print("n1.val=");  // This is sent to the nextion display to set what object name (before the dot) and what atribute (after the dot) are you going to change.
        Serial1.print(averageValue1, 2);  // This is the value you want to send to that object and atribute mentioned before.
        Serial1.write(0xff);  // We always have to send this three lines after each command sent to the nextion display.
        Serial1.write(0xff);
        Serial1.write(0xff);

        Serial1.print(",");
            
        Serial1.print("n2.val=");  // This is sent to the nextion display to set what object name (before the dot) and what atribute (after the dot) are you going to change.
        Serial1.print(averageValue2, 2);  // This is the value you want to send to that object and atribute mentioned before.
        Serial1.write(0xff);  // We always have to send this three lines after each command sent to the nextion display.
        Serial1.write(0xff);
        Serial1.write(0xff);
   
      Serial.println();
      }

  ////////////////////////// END OF Serial PRINT VALUES:
  }

  delay(interval);


}

I updated the library :

#ifndef __NEXCONFIG_H__
#define __NEXCONFIG_H__

/**
 * @addtogroup Configuration 
 * @{ 
 */

/** 
 * Define DEBUG_SERIAL_ENABLE to enable debug serial. 
 * Comment it to disable debug serial. 
 */
#define DEBUG_SERIAL_ENABLE

/**
 * Define dbSerial for the output of debug messages. 
 */
#define dbSerial Serial

/**
 * Define nexSerial for communicate with Nextion touch panel. 
 */
#define nexSerial Serial1

but it does not compile and I get the following error:

C:\Users\Dario\Documents\Arduino\libraries\ITEADLIB_Arduino_Nextion-master\NexUpload.cpp:17:10: fatal error: SoftwareSerial.h: No such file or directory
 #include <SoftwareSerial.h>
          ^~~~~~~~~~~~~~~~~~
compilation terminated.

exit status 1

Compilation error: exit status 1

Has anyone ever been able to make a nextion screen work with an Arduino nano RP2040? could anyone help me figure it out how to make them talk?

If you look in the libraries subdirectory in the rp2040 packages tree, you will not find a SoftwareSerial library. At least, I didn't find one in mine. That's likely because the rp2040 has 2 hardware serial ports (in addition to the USB Serial object).

From a quick glance on Github, the ITEADLIB_Arduino_Nextion library hasn't been updated in 7 years. The odds that it will work on a board that was only released 2 years ago are not in your favour. If you really want to use it, you may have to update it yourself. Or find a fork where someone has already done the work for you. Good luck with that.

I don't understand why you are trying to use software serial on a board that has hardware serial ports.

I have a tutorial for using Nextion displays here:

My methods do not use the official Nextion library. I have not tried my methods on the board you have but I expect them to work without difficulty as they do not rely on any special features of a particular processor, other than a hardware serial port.