Connecting three sensors to a Nextion Display?

Hello everyone,

I am currently working on a project where I need to connect three thermocouples to an Arduino Mega then transfer the temperature data to the Nextion display. I need to display the temperature for all three thermocouples at all times.

Currently I can display one thermocouple’s temperature. But cannot figure out how I would display three simultaneously.

Here is the code to display one thermocouple reading.

If you guys could help me out it would be greatly appreciated.

Troubleshooting_sketch.ino (1.38 KB)

Looking at the Adafruit_MAX31855 library you have the option of using the harware SPI… so why not use it with your thermocouples!

here’s how I would code it based on what I could see from online examples(compiles but no idea if it will work since I have neither hardware!)

#include <SoftwareSerial.h>
#include <SPI.h>
#include "Adafruit_MAX31855.h"

#define Nextion_TX 30
#define Nextion_RX 28

//Initialise SoftwareSerial for Nextion display Interface
SoftwareSerial Nextion(Nextion_RX, Nextion_TX);

// initialize the Thermocouples (using hardware SPI port. All thermocouples share MOSI and SCK pin)
//MAX31855 DO -> Arduino MISO
//MAX31855 CLK -> Arduino SCK

#define MAXCS  27
#define MAXCS2 34
#define MAXCS3 38

Adafruit_MAX31855 thermocouple(MAXCS);
Adafruit_MAX31855 thermocouple2(MAXCS2);
Adafruit_MAX31855 thermocouple3(MAXCS3);

void setup() {
  Serial.begin(115200); //open the hardware serial port
  while (!Serial) { // wait for serial port to connect. Needed for native USB port only
    ;
  }

  Serial.println("Serial On"); //Print this messages when the serial port is connected
  Nextion.begin(9600); // set the data rate for the SoftwareSerial port
}

void loop() {
  float temperatures[3];
  String sendThis = "temperature.txt=\""; //Declare and initialise the string we will send. This is the code used to store the temperature and send to the display

  temperatures[0] = thermocouple.readCelsius();
  temperatures[1] = thermocouple2.readCelsius();
  temperatures[2] = thermocouple3.readCelsius();

  for (uint8_t i = 0; i < 3; ++i) {
    if (isnan(temperatures[i])) {
      Serial.print("Something wrong with thermocouple! "); // Error check checks to see if a wire has disconnected from the terminal block
      Serial.println(i + 1, DEC);
      sendThis.concat("---\n");
    } else {
      sendThis.concat(String(temperatures[i], 1 ) + "\n");
    }
  }
  sendThis.concat("\"");
  writeString(sendThis); //Use a function to write the message character by character to the Nextion because
  delay(300); //Probably unneccessary, but I give the screen some time to respond
}

void writeString(String stringData) { // Used to serially push out a String with Serial.write()

  for (int i = 0; i < stringData.length(); i++)
  {
    Nextion.write(stringData[i]);   // Push each char 1 by 1 on each loop pass
  }

  Nextion.write(0xff); //We need to write the 3 ending bits to the Nextion as well
  Nextion.write(0xff); //it will tell the Nextion that this is the end of what we want to send.
  Nextion.write(0xff);

}// end writeString function

hope that helps…

I am having trouble understanding what the difference is between hardware SPI and Software? Why would I use say hardware SPI? Ive never used SPI before this project.

Babycobh1:
I am having trouble understanding what the difference is between hardware SPI and Software? Why would I use say hardware SPI? Ive never used SPI before this project.

Speed.

“//MAX31855 DO → Arduino MISO”

So I currently have three outputs for my three amplifiers. Where do my three outputs go on the Arduino MEGA?

Usually, SPI devices share clock and data lines, and use a separate device enable line to select devices. SPI on the Mega is on pins 50-53. the select lines can be any GPIO.

Babycobh1:
"//MAX31855 DO -> Arduino MISO"

So I currently have three outputs for my three amplifiers. Where do my three outputs go on the Arduino MEGA?

did you check the code I shared or was it not clear in it?

//All thermocouples share MOSI and SCK pin

ie the DO and CLK of the 3 thermocouples all connect to MOSI and SCK respectively

the CS connect to different pins on your board (in the code as well!)

https://www.arduino.cc/en/reference/SPI

Sorry about that. I will rewire everything.

Code provided did not work for the display.