Hi, I'm trying to understand the PID library and I'm a noob so its a little rough. I'm planning on using PID to control a heating element with SSR using MAX31855 as input.
I'm starting from scratch basically but with the library so I wanted to test out the relay output example included in the library. Here's my code:
Setpoint is 100 which is fine, as input is around 16 right now in my house! Heater is busted.... I'm not worried about tuning or coding steps or anything else like that right now. just trying to get it to work and control the SSR.
Whats happening is when it starts the ssr actually pulses a bit but the higher the output the less it pulses until it stops? I'd think SSR would be on 100% when it starts and if I heat up the tc it should start pulsing to stop? Something is very wrong here lol and some help is greatly appreciated!
/********************************************************
* PID RelayOutput Example
* Same as basic example, except that this time, the output
* is going to a digital pin which (we presume) is controlling
* a relay. the pid is designed to Output an analog value,
* but the relay can only be On/Off.
*
* to connect them together we use "time proportioning
* control" it's essentially a really slow version of PWM.
* first we decide on a window size (5000mS say.) we then
* set the pid to adjust its output between 0 and that window
* size. lastly, we add some logic that translates the PID
* output into "Relay On Time" with the remainder of the
* window being "Relay Off Time"
********************************************************/
#include <Adafruit_MAX31855.h>
#include <PID_v1.h>
#define RelayPin 6
//Define Variables we'll be connecting to
double Setpoint, Input, Output;
//Specify the links and initial tuning parameters
PID myPID(&Input, &Output, &Setpoint,2,5,1, DIRECT);
int WindowSize = 5000;
unsigned long windowStartTime;
//31855 stuff
int thermoCLK = 53;
int thermoCS = 51;
int thermoDO = 49;
int backLight = 13; // pin 13 will control the backlight
Adafruit_MAX31855 thermocouple(thermoCLK, thermoCS, thermoDO);
void setup()
{
Serial.begin(115200);
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);
}
void loop()
{
pinMode(RelayPin, OUTPUT);
Input = thermocouple.readCelsius();
myPID.Compute();
Serial.print("INPUT:");
Serial.println(Input);
Serial.print("OUTPUT:");
Serial.print(Output);
/************************************************
* 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(RelayPin,HIGH);
else digitalWrite(RelayPin,LOW);
}
Here's output:
OUTPUT:5000.00INPUT:16.25
OUTPUT:46.88INPUT:15.75
OUTPUT:46.88INPUT:15.50
OUTPUT:260.63INPUT:15.50
OUTPUT:260.63INPUT:15.00
OUTPUT:301.63INPUT:15.75
OUTPUT:301.63INPUT:15.75
OUTPUT:329.75INPUT:15.75
OUTPUT:329.75INPUT:16.00
OUTPUT:376.25INPUT:15.25
OUTPUT:376.25INPUT:15.25
OUTPUT:430.13INPUT:15.75
OUTPUT:430.13INPUT:15.00
OUTPUT:468.13INPUT:15.50
OUTPUT:468.13INPUT:16.50
OUTPUT:489.38INPUT:16.00
OUTPUT:489.38INPUT:16.00
OUTPUT:552.38INPUT:16.00
OUTPUT:552.38INPUT:15.00
OUTPUT:601.88INPUT:15.00
OUTPUT:601.88INPUT:15.75
OUTPUT:625.00INPUT:16.50
OUTPUT:625.00INPUT:15.25
OUTPUT:680.88INPUT:15.25
OUTPUT:680.88INPUT:16.00
OUTPUT:708.88INPUT:15.50
OUTPUT:708.88INPUT:16.50
OUTPUT:752.13INPUT:15.25
OUTPUT:752.13INPUT:15.25
OUTPUT:814.50INPUT:15.75
OUTPUT:814.50INPUT:15.75
OUTPUT:838.13INPUT:15.25
OUTPUT:838.13INPUT:15.75
OUTPUT:885.25INPUT:15.50
OUTPUT:885.25INPUT:15.50
OUTPUT:930.50INPUT:16.25
OUTPUT:930.50INPUT:15.75
OUTPUT:967.13INPUT:16.25
OUTPUT:967.13INPUT:15.50
OUTPUT:1014.88INPUT:15.50
OUTPUT:1014.88INPUT:15.50
OUTPUT:1054.63INPUT:16.50
OUTPUT:1054.63INPUT:15.75
OUTPUT:1093.75INPUT:15.50
OUTPUT:1093.75INPUT:16.00
OUTPUT:1135.25INPUT:16.25
OUTPUT:1135.25INPUT:16.25
OUTPUT:1176.63INPUT:16.50
OUTPUT:1176.63INPUT:16.00
OUTPUT:1224.13INPUT:15.50
OUTPUT:1224.13INPUT:16.75
OUTPUT:1254.25INPUT:15.75
OUTPUT:1254.25INPUT:15.75
OUTPUT:1315.88INPUT:16.00
OUTPUT:1315.88INPUT:16.75
OUTPUT:1335.50INPUT:15.75
OUTPUT:1335.50INPUT:16.00
OUTPUT:1396.50INPUT:16.75
OUTPUT:1396.50INPUT:16.25
OUTPUT:1427.88INPUT:16.25
OUTPUT:1427.88INPUT:16.50
OUTPUT:1469.13INPUT:16.00
OUTPUT:1469.13INPUT:16.25
OUTPUT:1516.50INPUT:16.00
OUTPUT:1516.50INPUT:16.50
OUTPUT:1552.75INPUT:16.50
OUTPUT:1552.75INPUT:16.25
OUTPUT:1600.13INPUT:16.75
OUTPUT:1600.13INPUT:16.00
OUTPUT:1642.63INPUT:15.75
OUTPUT:1642.63INPUT:15.75
OUTPUT:1685.25INPUT:16.50
OUTPUT:1685.25INPUT:16.00
OUTPUT:1721.75INPUT:16.25
OUTPUT:1721.75INPUT:16.50
OUTPUT:1760.00INPUT:16.25
OUTPUT:1760.00INPUT:16.75
OUTPUT:1803.63INPUT:16.75
OUTPUT:1803.63INPUT:16.75
OUTPUT:1847.75INPUT:16.25
OUTPUT:1847.75INPUT:16.00
OUTPUT:1898.75INPUT:16.50
OUTPUT:1898.75INPUT:16.00
OUTPUT:1933.25INPUT:15.75
OUTPUT:1933.25INPUT:16.25
OUTPUT:1972.13INPUT:16.00
OUTPUT:1972.13INPUT:16.00
OUTPUT:2019.63INPUT:16.00
OUTPUT:2019.63INPUT:16.00
OUTPUT:2059.12INPUT:16.00
OUTPUT:2059.12INPUT:16.50
OUTPUT:2094.87INPUT:16.25
OUTPUT:2094.87INPUT:16.00
OUTPUT:2147.87INPUT:16.25
OUTPUT:2147.87INPUT:16.75
OUTPUT:2175.50INPUT:16.25
OUTPUT:2175.50INPUT:16.00
OUTPUT:2234.00INPUT:15.25
OUTPUT:2234.00INPUT:16.25
OUTPUT:2265.37INPUT:17.00
OUTPUT:2265.37INPUT:16.25
OUTPUT:2309.75INPUT:16.00
OUTPUT:2309.75INPUT:16.00
OUTPUT:2354.75INPUT:16.25
OUTPUT:2354.75INPUT:16.75
OUTPUT:2384.87INPUT:16.50
OUTPUT:2384.87INPUT:16.25
OUTPUT:2440.25INPUT:16.50
OUTPUT:2440.25INPUT:16.25
OUTPUT:2477.12INPUT:17.00
OUTPUT:2477.12INPUT:17.00
OUTPUT:2509.62INPUT:16.00
OUTPUT:2509.62INPUT:17.00
OUTPUT:2558.62INPUT:17.25
OUTPUT:2558.62INPUT:17.25
OUTPUT:2597.00INPUT:16.00
OUTPUT:2597.00INPUT:16.50
OUTPUT:2650.25INPUT:16.75
OUTPUT:2650.25INPUT:16.50
OUTPUT:2684.50INPUT:16.50
OUTPUT:2684.50INPUT:16.25
OUTPUT:2729.37INPUT:16.00
OUTPUT:2729.37INPUT:15.50
OUTPUT:2778.12INPUT:15.50
OUTPUT:2778.12INPUT:16.50
OUTPUT:2800.37INPUT:16.25
OUTPUT:2800.37INPUT:16.25
OUTPUT:2855.25INPUT:16.00
OUTPUT:2855.25INPUT:16.00
OUTPUT:2897.75INPUT:16.25
OUTPUT:2897.75INPUT:16.25
OUTPUT:2934.12INPUT:16.50
OUTPUT:2934.12INPUT:17.00
OUTPUT:2969.12INPUT:16.75
OUTPUT:2969.12INPUT:15.75
OUTPUT:3033.75INPUT:16.25
OUTPUT:3033.75INPUT:16.75
OUTPUT:3050.87INPUT:17.00
OUTPUT:3050.87INPUT:16.75
OUTPUT:3102.50INPUT:16.75
OUTPUT:3102.50INPUT:17.00
OUTPUT:3141.00INPUT:16.75
OUTPUT:3141.00INPUT:16.25
OUTPUT:3194.37INPUT:16.25
OUTPUT:3194.37INPUT:16.50
OUTPUT:3225.62INPUT:16.50
OUTPUT:3225.62INPUT:17.00
OUTPUT:3263.62INPUT:16.25
OUTPUT:3263.62INPUT:16.00
OUTPUT:3322.62INPUT:16.75
OUTPUT:3322.62INPUT:16.25
OUTPUT:3351.50
I'll spare you the rest but output simply keeps raising until it hits WindowSize(5000). If I heat up the thermocouple the output does decrease but doesn't seem to do anything different with the RelayPin? Very odd. I have been searching around and trying to implement different bits of code from other sketches to no avail.
THANKS!