PID temperature controller w/ MAX31856. Need help please :(

I am new to Programming please help!

The project is using Arduino MEGA, Adafruit MAX31856 universal thermocouple amplifier W/ K-type thermocouple, 16X2 LCD and SSR. I am using the pid algorithm for my temperature controller but it seems the Pid is not working. A got an output read of 0.0 all the time and my SSR is always high. The project is using two push button switch to change the value of the Setpoint. I want to set the Kp,Ki and Kd values manually if the PID works. I already double check the wiring.

I dont know what is the problem with my coding.
Is there someone that has the same Project that i am creating now?
Please help!

here is the code

#include <SPI.h>
#include <PID_v1.h>
#include <Adafruit_MAX31856.h>
#include <LiquidCrystal.h>
#define RELAY 45

const int rs = 2, en = 3, d4 = 7, d5 = 6, d6 = 5, d7 = 4;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
Adafruit_MAX31856 max = Adafruit_MAX31856(10, 11, 12, 13);

const int LED_RED=53; //Red LED
const int LED_GREEN=51; //Green LED
 
//Key connections with arduino
const int up_key=44;
const int down_key=46;

//Define Variables we'll be connecting to
double Setpoint, Input, Output;
double kp=45,ki=0,kd=0;
int SetPoint = 200;
//Specify the links and initial tuning parameters
PID myPID(&Input, &Output, &Setpoint,kp,ki,kd, DIRECT);

int WindowSize = 5000;
unsigned long windowStartTime;



void setup() {

  // put your setup code here, to run once:
  pinMode(LED_RED,OUTPUT);
  pinMode(LED_GREEN,OUTPUT);  
  pinMode(RELAY,OUTPUT);  
  pinMode(up_key,INPUT);
  pinMode(down_key,INPUT);
  
  //Pull up for setpoint keys
  digitalWrite(up_key,HIGH);
  digitalWrite(down_key,HIGH);
  
   // set up the LCD's number of columns and rows: 
  lcd.begin(16, 2);
  // Print a message to the LCD.
  lcd.print("PID temp control");  
  lcd.setCursor(0,1); //Move coursor to second Line
  lcd.print("Temp. Controller");
  digitalWrite(LED_GREEN,HIGH);  //Green LED Off
  digitalWrite(LED_RED,LOW);     //Red LED On
  digitalWrite(RELAY,LOW);       //Turn off Relay
  Serial.begin(115200);

  max.begin();

  max.setThermocoupleType(MAX31856_TCTYPE_K);

  Serial.print("Thermocouple type: ");
  switch ( max.getThermocoupleType() ) {
    case MAX31856_TCTYPE_B: Serial.println("B Type"); break;
    case MAX31856_TCTYPE_E: Serial.println("E Type"); break;
    case MAX31856_TCTYPE_J: Serial.println("J Type"); break;
    case MAX31856_TCTYPE_K: Serial.println("K Type"); break;
    case MAX31856_TCTYPE_N: Serial.println("N Type"); break;
    case MAX31856_TCTYPE_R: Serial.println("R Type"); break;
    case MAX31856_TCTYPE_S: Serial.println("S Type"); break;
    case MAX31856_TCTYPE_T: Serial.println("T Type"); break;
    case MAX31856_VMODE_G8: Serial.println("Voltage x8 Gain mode"); break;
    case MAX31856_VMODE_G32: Serial.println("Voltage x8 Gain mode"); break;
    default: Serial.println("Unknown"); break;
  }
  
  windowStartTime = millis();
  
  //initialize the variables we're linked to

 
  //tell the PID to range between 0 and the full window size
  myPID.SetOutputLimits(0, WindowSize);

  //turn the PID on
  myPID.SetMode(AUTOMATIC);
}

void loop() 
{
  Serial.print("Cold Junction Temp: "); Serial.println(max.readCJTemperature());
  Serial.print("Thermocouple Temp: "); Serial.println(max.readThermocoupleTemperature());
  Serial.print("Setpoint: "); Serial.println(SetPoint);
  Serial.print("Output: "); Serial.println(Output);
  // put your main code here, to run repeatedly:
  lcd.setCursor(0,0);
  lcd.print("Temperature:");    
  lcd.print(max.readThermocoupleTemperature());
  
//Get user input for setpoints  
  if(digitalRead(down_key)==LOW)
  {
    if(SetPoint>0)
    {
      SetPoint--;    
    }
  }
  if(digitalRead(up_key)==LOW)
  {
    if(SetPoint<600)
    {
      SetPoint++;
    }
  }

if( Input > SetPoint)
{
    digitalWrite(RELAY,LOW);     
   digitalWrite(LED_RED,LOW);
   digitalWrite(LED_GREEN,HIGH);  //Turn on Green LED
}
else
{
  digitalWrite(RELAY,HIGH); 
  digitalWrite(LED_GREEN,LOW);
  digitalWrite(LED_RED,HIGH);  //Turn on RED LED  
}
//Display Set point on LCD
  lcd.setCursor(0,1);
  lcd.print("Set Point:");
  lcd.print(SetPoint);
  lcd.print("C   ");

  
   Input = max.readThermocoupleTemperature()- 25;
  myPID.Compute();

  /************************************************
   * turn the output pin on/off based on pid output
   ************************************************/
  if(millis() - windowStartTime>WindowSize)
  { //time to shift the Relay Window
    windowStartTime += WindowSize;
  }
  if(Output < millis() - windowStartTime) digitalWrite(RELAY,HIGH);
  else digitalWrite(RELAY,LOW);
}

Here’s the data sheet and libraries

Did you try to just read the temperatures without the PID in-between?

Post a wiring diagram of your setup.

Please explain what this part of your code is expected to do:

  if(millis() - windowStartTime>WindowSize)
  { //time to shift the Relay Window
    windowStartTime += WindowSize;
  }
  if(Output < millis() - windowStartTime) digitalWrite(RELAY,HIGH);
  else digitalWrite(RELAY,LOW);

Is the relay output connected to a heater? Does it make sense in that setup to have the on-time of that device as the output of the PID? So does the periodical on-time of that “heater” a direct relationship with the temperature output? It might be necessary to describe your setup in a bit more detail.

Hi! thank you for your responce.

Yes, the relay is connected to a heater Heater( Fillament of the soldering iron 30 W) and also it display the temp of the heater. the sensor works properly and it display in the LcD precisely.

and that part of the code it is from the example code from the PID library(PID_relay).

Yes, the relay is connected to a heater Heater( Fillament of the soldering iron 30 W) and also it display the temp of the heater. the sensor works properly and it display in the LcD precisely.

That means the sensor is connected to more than one circuit? I ask again for a wiring diagram of the whole setup as I get the impression that the hardware does not what you expect it to do.

Hi, Did you run and try the PID_Relay example before modifying it? Before the dislplay and buttons were added.

If not, then I suggest you load the example and get it going first. Use a potentimeter on the A0 analog input.

Then when you get that working, add the thermocouple and get that working. Use Serial Monitor to see your variables.

Then and only then when you have that working, add the display.

Tom.. :)