Hello,
I am trying to do a heat control. For that I have to thermocouples, one controlling temperature of a gas and the other controlling the temperature of the cell; which have diferent set points. With each thermocouple I do a PID.
Once the PIDs are done I compare both outputs and the output with lowest value is the one that set on/off a relay.
- Using two thermocouples Type K with MAX31885 and the Adafruit_MAX31855 library
- Relay A-senco 501
This is the code I have:
#include <PID_v1.h>
#include <SPI.h>
#include "Adafruit_MAX31855.h"
#define RelayPin 13
#define MAXDO 50
#define MAXCS 8
#define MAXCLK 52
#define MAXCS2 9
// initialize the Thermocouple
Adafruit_MAX31855 thermocouple(MAXCLK, MAXCS, MAXDO);
Adafruit_MAX31855 thermocouple2(MAXCLK,MAXCS2, MAXDO);
//Define Variables we'll be connecting to
double Setpoint, Input, Output;
double Setpoint2, Input2, Output2;
//Specify the links and initial tuning parameters
PID myPID(&Input, &Output, &Setpoint,2,5,1, DIRECT);
PID myPID2(&Input2, &Output2, &Setpoint2,2,5,1, DIRECT);
int WindowSize = 5000;
unsigned long windowStartTime;
void setup()
{
#ifndef ESP8266
while (!Serial); // will pause Zero, Leonardo, etc until serial console opens
#endif
Serial.begin(9600);
// wait for MAX chip to stabilize
delay(500);
windowStartTime = millis();
//initialize the variables we're linked to
Setpoint = 100;
//tell the PID to range between 0 and the full window size
myPID.SetOutputLimits(0, WindowSize);
//turn the PID on
myPID.SetMode(AUTOMATIC);
//initialize the variables we're linked to
Setpoint2 = 80;
//tell the PID to range between 0 and the full window size
myPID2.SetOutputLimits(0, WindowSize);
//turn the PID on
myPID2.SetMode(AUTOMATIC);
Serial.begin(9600);
}
void loop()
{
Serial.println("PID 1");
double c = thermocouple.readCelsius();
if (isnan(c)) {
Serial.println("Something wrong with thermocouple!");
} else {
Serial.print("Temperature 1 = ");
Serial.println(c);
}
//Serial.print("F = ");
//Serial.println(thermocouple.readFarenheit());
Input = c;
myPID.Compute();
Serial.print("Output 1 = ");
Serial.println(Output);
Serial.println("PID 2");
double d = thermocouple2.readCelsius();
if (isnan(d)) {
Serial.println("Something wrong with thermocouple2!");
} else {
Serial.print("Temperature 2 = ");
Serial.println(d);
}
Input2 = d;
myPID2.Compute();
Serial.print("Output 2 = ");
Serial.println(Output2);
/************************************************
- turn the output pin on/off based on pid output
************************************************/
if (Output<Output2)
{
unsigned long now = millis();
if(now - windowStartTime>WindowSize)
{ //time to shift the Relay Window
windowStartTime += WindowSize;
}
if(Output > now - windowStartTime) digitalWrite(RelayPin,HIGH);
else digitalWrite(RelayPin,LOW);
}
else
{
unsigned long now = millis();
if(now - windowStartTime>WindowSize)
{ //time to shift the Relay Window
windowStartTime += WindowSize;
}
if(Output2 > now - windowStartTime) digitalWrite(RelayPin,HIGH);
else digitalWrite(RelayPin,LOW);
}
}
I will like to know why it is not working, it does not set on and off the relay
Also could I use a PWM for the relay?