Unofficial Nextion/TJC User Forum

Problem displaying force sensor data on Nextion Screen

Hi all,

I am working on a project where I am using four sensors connected to an Arduino Mega. The first three sensors are thermocouple amplifiers and they connect to digital inputs on the Mega and are controlled via SPI. The amplifier is the Max31855. I have code written to output the amplifiers values to the Nextion display continuously and this works exactly how I want it to work.

The problem that I am having is getting the display to display the output from the force sensitive resistor. It just uses a simple voltage divider with a 10K resistor and one analog input.

I have written some code for it and Im thinking that it should output onto the screen like the thermocouples but it does not the value just stays at zero.

I am using a number variable for the force sensor and assigning fsrForce to “force.val=”

If I look on the serial monitor I can see that the force.val= is changing like it should I have pictures of it. So I know that the Arduino understands this and is trying to get it onto the display but for some reason it cannot.

I created a new program where I just look at the force sensor reading and nothing else and have pictures taken of the following. What is displayed on the nextion screen, the number variable created on the nextion editor called force and what the serial monitor shows when I upload it. The three ERRORs that you see are because I have the thermocouples disconnected.

Any help would be greatly appreciated I am on a strict schedule sadly however.

Thanks in advance.

https://drive.google.com/file/d/1oXsK8523JnJuTY3UgAR1tJUqdgmVooeF/view?usp=sharing

https://drive.google.com/file/d/1Twt3im0N7a6gRShFJkGjNrdXl_W6vwqQ/view?usp=sharing

int fsrPin = 5;     // the FSR and 10K pulldown are connected to a5
int fsrReading;     // the analog reading from the FSR resistor divider
int fsrVoltage;     // the analog reading converted to voltage
unsigned long fsrResistance;  // The voltage converted to resistance, can be very big so make "long"
unsigned long fsrConductance; 
int fsrForce;       // Finally, the resistance converted to force
 
void setup(void) {
  Serial.begin(9600);   
}
 
void loop(void) {
  
  fsrReading = analogRead(fsrPin);  
  Serial.print("Analog reading = ");
  Serial.println(fsrReading);
 
  // analog voltage reading ranges from about 0 to 1023 which maps to 0V to 5V (= 5000mV)
  fsrVoltage = map(fsrReading, 0, 1023, 0, 5000);
  Serial.print("Voltage reading in mV = ");
  Serial.println(fsrVoltage);  
    
    
    fsrResistance = 5000 - fsrVoltage;     // fsrVoltage is in millivolts so 5V = 5000mV
    fsrResistance *= 10000;                // 10K resistor
    fsrResistance /= fsrVoltage;
    Serial.print("FSR resistance in ohms = ");
    Serial.println(fsrResistance);
 
    fsrConductance = 1000000;           //  micromhos
    fsrConductance /= fsrResistance;
    Serial.print("Conductance in microMhos: ");
    Serial.println(fsrConductance);
 
    // Use the two FSR guide graphs to approximate the force
    if (fsrConductance <= 1000) {
      fsrForce = fsrConductance / 80;          
      Serial.print("force.val=");
      Serial.print(fsrForce);         
      endNextionCommand();         
      }
     else {
      fsrForce = fsrConductance - 1000;
      fsrForce /= 30;          
      Serial.print("force.val=");
      Serial.print(fsrForce);         
      endNextionCommand();       
    }
}

void endNextionCommand() // Subroutine used to send the data to the Nextion screen
{
Serial.write(0xff);
Serial.write(0xff);
Serial.write(0xff);
}

Here is the link that the website wouldn’t let me put

https://drive.google.com/file/d/1OwanrCpS7oAD5oYECXgglfnapeaF_i6U/view?usp=sharing

Your code makes no distinction between the Serial.print for the Serial monitor and the same Serial.print for the Nextion, but Serial is an 1:1 protocol, so, you have to hook it onto Serial2 or Serial3 on the Mega…

How would you re write it then? Im not sure what you mean by serial 2 or serial 3 exactly. The way that I have written it is the way that I have seen data sent before. Maybe Im missing something can you give me an example of what you mean?

Thank you

The basic principle is that a CPU can only communicate with one endpoint over a serial connection. The default serial port on the Mega is on pins 0 and 1 and in parallel on the USB adapter. So, if you have the Mega connected to the Arduino IDE via USB to print out data to the serial monitor, you can not at the same time connect the Nextion HMI to pins 0 and 1 and expect that both will at the same time listen to the data which you send with Serial.print or Serial.write and understand automatically which of the Data is for the Nextion and which is for the Serial monitor.
That’s why most CPUs have more than one serial port. The Mega has for example Serial1 on pins 18 and 19 to which you’d connect the Nextion.
In your code, you’ll have to initialize both Serial ports:
void setup()
{
Serial.begin(57600); // for the serial monitor of the IDE
Serial1.begin(9600); // for the Nextion
Serial.print(“This goes to the serial monitor”);
Serial1.print(“force.val=1234”); //this goes to the Nextion
Serial1.write(0xff,0xff,0xff);
}

“ On Uno, Nano, Mini, and Mega, pins 0 and 1 are used for communication with the computer. Connecting anything to these pins can interfere with that communication, including causing failed uploads to the board.”

From the Arduino Serial reference page:
https://www.arduino.cc/reference/en/language/functions/communication/serial/

I understand what you mean. The issue that Im having is that it doesn’t stream the data to the nextion ever. So if I have the computer disconnected from the arduino it still will not send any data to the nextion. I feel like your solution would alow me to use the serial monitor and stream data to the nextion display together at once. Even when the serial monitor is not used and the arduino has say a 9 volt battery as a power source it still wont display a force on the nextion. I am using pin 1 on the MEGA for TX to the nextion’s RX pin and it works for my temperature sensors but not my force sensor. It is very strange and I think that the Nextion knows what pin to use because it displays three temperatures simultaneously without an issue.

For example this code works perfectly with the display at all times

double temperature = thermocouple.readCelsius();
   if (isnan(temperature)) {
    Serial.println("Something wrong with thermocouple!"); // Error check checks to see if a wire has disconnected from the terminal block

} else {
String command = “temperature.txt=”"+String(temperature,1 )+"""; // This is the code used to store the temperature and send to the display
Serial.print(command);
endNextionCommand();
}

double temperature2 = thermocouple2.readCelsius();
if (isnan(temperature2)) {
Serial.println(“Something wrong with thermocouple!”); // Error check checks to see if a wire has disconnected from the terminal block
} else {
String command = “temperature2.txt=”"+String(temperature2,1 )+"""; // This is the code used to store the temperature and send to the display
Serial.print(command);
endNextionCommand();
}

double temperature3 = thermocouple3.readCelsius();
if (isnan(temperature3)) {
Serial.println(“Something wrong with thermocouple!”); // Error check checks to see if a wire has disconnected from the terminal block
} else {
String command = “temperature3.txt=”"+String(temperature3,1 )+"""; // This is the code used to store the temperature and send to the display
Serial.print(command);
endNextionCommand();
}

void endNextionCommand() // Subroutine used to send the data to the Nextion screen
{
Serial.write(0xff);
Serial.write(0xff);
Serial.write(0xff);
}

The nextion displays each temperature perfectly.


This forum is in no way affiliated with NEXTION®, ITEAD STUDIO®, TJC®, or anyone else really. All product names, logos, and brands are property of their respective owners. All company, product, and service names used in this website are for identification purposes only. Use of these names, logos, and brands does not imply endorsement from the respective rights holder(s).