Interference between SPI reading and using the PID algorithm

I want to finalize my soldering station project and I will use a MAX6675 module.
I wrote a part of the attached program, and another part I took from the internet.

I need to know if there are any interference or any compatibility issues between using the function for reading the MAX6675 module and using the PID function ?

#include <PID_v1.h>
#include <LiquidCrystal.h>
#include <SPI.h> // The SPI library
#define CS_MAX 10 // MAX6675 CS (Chip Select) Line

unsigned long updaterate = 1200; 
unsigned long lastupdate;

int temperature = 0;

//Protection
int relay_pin = 9;

//Define Variables we'll be connecting to
double Setpoint, Input, Output;

//Define the aggressive and conservative Tuning Parameters
double aggKp = 4, aggKi = 0.2, aggKd = 1;
double consKp = 1, consKi = 0.05, consKd = 0.25;

//Specify the links and initial tuning parameters
PID myPID(&Input, &Output, &Setpoint, consKp, consKi, consKd, DIRECT);

//Display
LiquidCrystal lcd(2, 3, 4, 5, 6, 7);

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

  pinMode(CS_MAX, OUTPUT); // MAX6675/6674 /CS Line must be an output for hardware SPI
  digitalWrite(CS_MAX, HIGH); // Set MAX7765 /CS High

  SPI.begin(); // Init SPI
  SPI.setBitOrder(MSBFIRST); // Set the order of the bits shifted out of and into the SPI bus
  SPI.setDataMode(SPI_MODE1); // Capture data on clock's falling edge.
  SPI.setClockDivider(SPI_CLOCK_DIV4); // Set SPI data rate to 4mhz.
  //We do not want to drive the soldering iron at 100% because it may burn, so we set it to about 85% (220/255)
  myPID.SetOutputLimits(0, 220);
  myPID.SetMode(AUTOMATIC);
  lastupdate = millis();
  Setpoint = 0;
  pinMode(relay_pin, OUTPUT);
  digitalWrite(relay_pin, HIGH);
  lcd.begin(16, 2);
  lcd.print("STATIE DE LIPIT");
  delay(500);
}

void loop() {
  int Input = GetTemperature();

  //Protection at disconnected TC
  if (Input == -1) // No Connection
  {
    while (true) {
      digitalWrite(relay_pin, LOW);
      digitalWrite(11, LOW);
      lcd.clear();
      lcd.setCursor(0, 1);
      lcd.print("NO TC CONN !!!");
      delay(1000);
    }
  }

  // Protection at iron's over temperature
  if (Input >= 430)
  {
    while (true) {
      digitalWrite(relay_pin, LOW);
      digitalWrite(11, LOW);
      lcd.clear();
      lcd.setCursor(0, 1);
      lcd.print("OVERHEATING !!!");
      delay(1000);
    }
  }

  //Display temperature
  if (millis() - lastupdate > updaterate) {
    lastupdate = millis();
    temperature = Input;
  }

  //Read setpoint and transform it into degrees celsius(minimum 150, maximum 350)
  double newSetpoint = analogRead(A1);
  newSetpoint = map(newSetpoint, 0, 1023, 150, 400);
  //Display setpoint
  if (abs(newSetpoint - Setpoint) > 3) {
    Setpoint = newSetpoint;
    temperature = newSetpoint;
    lastupdate = millis();
  }

  double gap = abs(Setpoint - Input); //distance away from setpoint

  if (gap < 10)
  { //we're close to setpoint, use conservative tuning parameters
    myPID.SetTunings(consKp, consKi, consKd);
  }
  else
  {
    //we're far from setpoint, use aggressive tuning parameters
    myPID.SetTunings(aggKp, aggKi, aggKd);
  }

  myPID.Compute();
  //Drive the output
  analogWrite(11, Output);
  //Display the temperature
  lcd.setCursor(0,0);
  lcd.print("SET= ");
  lcd.setCursor(5,0);
  lcd.print(newSetpoint);
  lcd.setCursor(11,0);
  lcd.print("C");
  lcd.setCursor(0,1);
  lcd.print("READ= ");
  lcd.setCursor(6,0);
  lcd.print(Input); 
  lcd.setCursor(11,0);
  lcd.print("C");  
}

int GetTemperature (void)
  {
  unsigned int temp_reading;

  // stop any conversion process
  delay(5);
  digitalWrite(CS_MAX,LOW); // Set MAX7765 /CS Low
  delay(5);
  
  // initiate a new conversion
  digitalWrite(CS_MAX,HIGH); // Set MAX7765 /CS High
  delay(250); // wait for conversion to finish..
  
  // read result
  digitalWrite(CS_MAX,LOW); // Set MAX7765 /CS Low
  delay(1);
  
  temp_reading = SPI.transfer(0xff) << 8;  
  temp_reading += SPI.transfer(0xff);  

  digitalWrite(CS_MAX,HIGH); // Set MAX7765 /CS High
  delay(1);
  
  // Bit D2 is normally low and goes high if the thermocouple input is open.
  if(bitRead(temp_reading,2) == 1) // No Connection
    {
      return(-1); // No Connection Error
    }
    else
    {
      return((int)(temp_reading >> 5)); //Convert to Degc
    }
  }

the_bear:
I need to know if there are any interference or any compatibility issues between using the function for reading the MAX6675 module and using the PID function ?

Have you some specific reason to think there might be?

...R

I don't have any specific reason, I am generally asking.

the_bear:
I don't have any specific reason, I am generally asking.

If it was my project I would run it and see what happens.

...R

Another approach would be to examine the range and precision of the data from the sensor, and also that of the PID code. Do they match?

Thank you for your advice.
I managed to make the program work, I modified some things.
I found that the read temperature from the thermocouple is not the real temperature of the tip of the iron, and after some tests, I found that the difference between the tip temperature and thermocouple temperature is about 100 C (I am using a Chinese copy of Gordak).
How can I write the program in order to display on the LCD the real temperature of the tip ?
If I simply display the read temp from max6675 - 100 C, then it is starting from negative temps.

the_bear:
If I simply display the read temp from max6675 - 100 C, then it is starting from negative temps.

Something like this ?

displayTemp = max6675temp - 100;
if (displayTemp < 0) {
  displayTemp = 0;
}

…R

Yes, I was already thinking about that “if” but I thought there are better solutions. I will use the “if”, I already implemented into the project and it is working.
I also have 2 types of error in my project and they do not work correctly. I mean there are no errors in the code, the errors are for displaying a message when the iron gets overheated or when the thermocouple gets disconnected (one or both wires of the TC).
Sometimes when I disconnect the thermocouple, it says “overheating” instead of “no tc connection”.
How can I solve this ?

I think that I should add something in the “if” condition to separate the “no tc connection” from the “overheating”. I am also aware of the fact that during the disconnection of the thermocouple wires, there could be some kind of the phenomenon that we name “bounce” at a switch.

heaterTemp = thermocouple.readCelsius();
  Input = heaterTemp - 100; 
   //No TC connection
  if (isnan(Input)) // No TC Connection
  {
    while (true) {
      digitalWrite(relay_pin, LOW);
      digitalWrite(11, LOW);
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("NO TC CONN !!!");
      delay(1000);
    }
  }

  //Soldering Iron overtemperature
  if (Input >= 432)
  {
    while (true) {
      digitalWrite(relay_pin, LOW);
      digitalWrite(11, LOW);
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("OVERHEATING !!!");
      delay(1000);
    }
  }

the_bear:
Sometimes when I disconnect the thermocouple, it says "overheating" instead of "no tc connection".
How can I solve this ?

I don't know. I have not used a thermocouple with an Arduino.

...R

I finally managed to make it work. It only shows one single error message on the LCD display for both type of errors, but it should be fine.

//Arduino Nano Soldering Station for Gordak soldering iron K type thermocouple.
// Uses MAX6675 module, 1602 display and protection relay.
//displays a single error message

#include <max6675.h>
#include <PID_v1.h>
#include <LiquidCrystal.h>

int thermoDO = 12;
int thermoCS = 10;
int thermoCLK = 13;

MAX6675 thermocouple(thermoCLK, thermoCS, thermoDO);

//protection
int relay_pin = 8;

//Define Variables we'll be connecting to
double Setpoint, Input, Output;
double heaterTemp;

//Define the aggressive and conservative Tuning Parameters
double aggKp = 4, aggKi = 0.2, aggKd = 1;
double consKp = 1, consKi = 0.05, consKd = 0.25;

//Specify the links and initial tuning parameters
PID myPID(&Input, &Output, &Setpoint, consKp, consKi, consKd, DIRECT);

//display
LiquidCrystal lcd(2, 3, 4, 5, 6, 7);

//display variables
int setTemp, readTemp;

byte thermometer[8] = //icon for termometer
{
  B00100,
  B01010,
  B01010,
  B01110,
  B01110,
  B11111,
  B11111,
  B01110
};

void setup()
{
  lcd.createChar(0, thermometer);
  //We do not want to drive the soldering iron at 100% because it may burn, so we set it to about 85% (220/255)
  myPID.SetOutputLimits(0, 220);
  myPID.SetMode(AUTOMATIC);
  Setpoint = 0;
  pinMode(relay_pin, OUTPUT);
  digitalWrite(relay_pin, LOW);
  lcd.clear();
  lcd.begin(16, 2);
  lcd.print("STATIE DE LIPIT");
  delay(1000);
  digitalWrite(relay_pin, HIGH);
}

void loop() {

  heaterTemp = thermocouple.readCelsius();
  Input = 0.779828 * heaterTemp - 10.3427;
  if (isnan(heaterTemp) or Input >= 432) // No TC Connection OR over-temperature
  {
    while (true) {
      digitalWrite(relay_pin, LOW);
      digitalWrite(11, LOW);
      for (int i=0; i<=1; i++){
      lcd.clear();
      lcd.setCursor(0, i);
      lcd.write((byte)0);
      lcd.setCursor(1, i);
      lcd.write((byte)0);
      lcd.setCursor(5, i);
      lcd.print("ERROR!");
      lcd.setCursor(14, i);
      lcd.write((byte)0);
      lcd.setCursor(15, i);
      lcd.write((byte)0);
      delay(1000);
      }
    }
  }

  //Read setpoint and transform it into degrees celsius(minimum 150, maximum 400)
  double newSetpoint = analogRead(A1);
  newSetpoint = map(newSetpoint, 0, 1023, 150, 400);
  //Display setpoint
  if (abs(newSetpoint - Setpoint) > 3) {
    Setpoint = newSetpoint;
    
  }

  double gap = abs(Setpoint - Input); //distance away from setpoint

  if (gap < 10)
  { //we're close to setpoint, use conservative tuning parameters
    myPID.SetTunings(consKp, consKi, consKd);
  }
  else
  {
    //we're far from setpoint, use aggressive tuning parameters
    myPID.SetTunings(aggKp, aggKi, aggKd);
  }

  myPID.Compute();
  //Drive the output
  analogWrite(11, Output);
  //Display the temperature
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("SET= ");
  lcd.setCursor(5, 0);
  setTemp = (int) Setpoint;
  lcd.print(setTemp);
  lcd.setCursor(10, 0);
  lcd.print((char)223);//degree sign
  lcd.setCursor(11, 0);
  lcd.print("C");
  lcd.setCursor(0, 1);
  lcd.print("READ= ");
  lcd.setCursor(6, 1);
  readTemp = (int) Input;
  if (readTemp < 25) { // displays 25 when temperature is lower than 25 C
    readTemp = 25;
  }
  lcd.print(readTemp);
  lcd.setCursor(11, 1);
  lcd.print((char)223);//degree sign
  lcd.setCursor(12, 1);
  lcd.print("C");
  delay(500);
}