PID library not working for me, what am I missing?

Hi there, I am building a temperature controller and am such at something that I thought I had taken care of before, but I never really fully tested it. So the relay turns on and my heater coil doesn't stop heating until I cut power.

So I am backtracking now and just trying to use the PID library example, PID_RelayOutput. It all seems simple enough but with this example I can't get the relay to turn on.

I'm using the Adafruit_Max31855 library for the thermocouple and the only change I made to the example was adding
Input = thermocouple.readFarenheit();

I not great at coding and I'm sure it's simple and something I'm just missing cause or seems like it should be pretty easy.

I've tried other libraries/examples and it's not doing it for me.

Any help would be greatly appreciated!

Thanks

Please post your current code.

How to use this forum

Sorry meant to include my code in first post.

This basically uses a rotary encoder and the ClickEncoder library to change the value of the Setpoint and display the Setpoint and Actual temperature on the lcd

//Libraries
#include <ClickEncoder.h>
#include <TimerOne.h>
#include <SPI.h>
#include "Adafruit_MAX31855.h"
#include <LiquidCrystal.h>
#include <PID_v1.h>


#define LCD_CHARS 16
#define LCD_LINES 2

#define RelayPin 6


LiquidCrystal lcd(7, 8, 9, 10, 11, 12);

int thermoCLK = 3;
int thermoCS = 4;
int thermoDO = 5;

Adafruit_MAX31855 thermocouple(thermoCLK, thermoCS, thermoDO);


//Define Variables we'll be connecting to
double Setpoint, SetpointF, Input, Output;
double Kp=50, Ki=0.50, Kd=1;

//Specify the links and initial tuning parameters
PID PID1(&Input, &Output, &Setpoint,Kp,Ki,Kd, AUTOMATIC);

byte degree[8] = {
0b01110,
0b01010,
0b01110,
0b00000,
0b00000,
0b00000,
0b00000,
};

//Click Encoder
ClickEncoder *encoder;
int16_t last, value;

void timerIsr() {
encoder->service();
}

void setup() {
Serial.begin(9600);
lcd.print("L O A D I N G ");
delay(3000);

lcd.setCursor(1, 0);
lcd.print(" Select Temp: ");
encoder = new ClickEncoder(A0, A1, A2);
lcd.createChar(1, degree);


lcd.begin(LCD_CHARS, LCD_LINES);
lcd.clear();
lcd.print(" Select Temp: ");


Timer1.initialize(1000);
Timer1.attachInterrupt(timerIsr);
last = -1;
Setpoint = 40;//initial setpoint
//turn the PID on
  PID1.SetMode(AUTOMATIC);
  
}

void loop() {
double c = thermocouple.readFarenheit();
lcd.setCursor(8, 1);
if (isnan(c))
{
lcd.print("T/C Problem");
}
else
{
lcd.print(c);
lcd.write(1);
lcd.print("F");
}

value += encoder->getValue();

if (value != last) {
last = value;
Serial.print("Encoder Value: ");
Serial.println(value);

lcd.setCursor(3, 1);
lcd.write(1);
lcd.print("F");
lcd.setCursor(0, 1);
lcd.print(value);

Setpoint = (value - 32) / 1.8;// convert F to C 
Input = (c - 32) / 1.8; // convert F to C
delay(5);
PID1.Compute();
analogWrite(RelayPin,Output); //PWM 
}
}

So, no indenting, huh?

Can you describe what actually happens here? And what you expect to happen?

And copy and paste what the serial prints show you?

My computer doesn't have internet so i have to copy files from there to my phone and then open a text editor on my phone and copy/paste the code to this forum. I'm sure it lost the indentation somewhere along there lol.

What happens is that when powered on, my relay switches, the heater coil turns on and it overshoots indefinitely past my setpoint. I'm assuming it will continue climbing until it breaks, but I don't want to find out. The LCD does accurately display the temperature.

What I'd like to happen is have it reach the setpoint and stay around there somewhere like its supposed to, pending tuning.

The serial monitor just shows "Encoder Value:" and then whatever the value is.

The serial monitor just shows "Encoder Value:" and then whatever the value is.

Can't you print the setpoint, the input and the output? Then we can see what the PID is doing.

Hi,
Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png or pdf?

Thanks Tom....... :slight_smile:

I attached the serial print. I'll grab the fritzing diagram I made when I get home.

Encoder Value: 0
PID Input: 25.00   PID Set point: -17.78   PID Output: 255.00
Encoder Value: 1
PID Input: 25.00   PID Set point: -17.22   PID Output: 84.87
Encoder Value: 2
PID Input: 25.00   PID Set point: -16.67   PID Output: 83.97
Encoder Value: 3
PID Input: 25.00   PID Set point: -16.11   PID Output: 83.97
Encoder Value: 4
PID Input: 25.00   PID Set point: -15.56   PID Output: 81.95
Encoder Value: 5
PID Input: 25.00   PID Set point: -15.00   PID Output: 81.95
Encoder Value: 6
PID Input: 25.00   PID Set point: -14.44   PID Output: 79.92
Encoder Value: 7
PID Input: 25.00   PID Set point: -13.89   PID Output: 79.92
Encoder Value: 8
PID Input: 25.00   PID Set point: -13.33   PID Output: 77.89
Encoder Value: 9
PID Input: 25.00   PID Set point: -12.78   PID Output: 77.89
Encoder Value: 10
PID Input: 25.00   PID Set point: -12.22   PID Output: 75.86
Encoder Value: 11
PID Input: 25.00   PID Set point: -11.67   PID Output: 75.86
Encoder Value: 12
PID Input: 25.00   PID Set point: -11.11   PID Output: 73.81
Encoder Value: 13
PID Input: 25.00   PID Set point: -10.56   PID Output: 72.88
Encoder Value: 14
PID Input: 25.00   PID Set point: -10.00   PID Output: 72.88
Encoder Value: 15
PID Input: 25.00   PID Set point: -9.44   PID Output: 70.83
Encoder Value: 16
PID Input: 25.00   PID Set point: -8.89   PID Output: 69.89
Encoder Value: 17
PID Input: 25.00   PID Set point: -8.33   PID Output: 69.89
Encoder Value: 18
PID Input: 25.00   PID Set point: -7.78   PID Output: 67.83
Encoder Value: 19
PID Input: 25.25   PID Set point: -7.22   PID Output: 69.88
Encoder Value: 20
PID Input: 25.00   PID Set point: -6.67   PID Output: 69.88
Encoder Value: 21
PID Input: 25.00   PID Set point: -6.11   PID Output: 62.32
Encoder Value: 22
PID Input: 25.00   PID Set point: -5.56   PID Output: 62.32
Encoder Value: 23
PID Input: 25.00   PID Set point: -5.00   PID Output: 62.74
Encoder Value: 24
PID Input: 25.00   PID Set point: -4.44   PID Output: 62.74
Encoder Value: 25
PID Input: 25.00   PID Set point: -3.89   PID Output: 60.67
Encoder Value: 26
PID Input: 25.00   PID Set point: -3.33   PID Output: 60.67
Encoder Value: 27
PID Input: 25.00   PID Set point: -2.78   PID Output: 58.58
Encoder Value: 28
PID Input: 25.00   PID Set point: -2.22   PID Output: 57.61
Encoder Value: 29
PID Input: 25.00   PID Set point: -1.67   PID Output: 57.61
Encoder Value: 30
PID Input: 25.00   PID Set point: -1.11   PID Output: 55.52
Encoder Value: 31
PID Input: 25.00   PID Set point: -0.56   PID Output: 55.52
Encoder Value: 32
PID Input: 25.00   PID Set point: 0.00   PID Output: 53.42
Encoder Value: 31
PID Input: 25.25   PID Set point: -0.56   PID Output: 53.42
Encoder Value: 32
PID Input: 25.00   PID Set point: 0.00   PID Output: 53.54
Encoder Value: 33
PID Input: 25.25   PID Set point: 0.56   PID Output: 53.54
Encoder Value: 34
PID Input: 25.00   PID Set point: 1.11   PID Output: 51.44
Encoder Value: 35
PID Input: 25.00   PID Set point: 1.67   PID Output: 51.44
Encoder Value: 36
PID Input: 25.00   PID Set point: 2.22   PID Output: 49.33
Encoder Value: 37
PID Input: 25.00   PID Set point: 2.78   PID Output: 49.33
Encoder Value: 38
PID Input: 25.00   PID Set point: 3.33   PID Output: 47.22
Encoder Value: 39
PID Input: 25.00   PID Set point: 3.89   PID Output: 47.22
Encoder Value: 40
PID Input: 25.00   PID Set point: 4.44   PID Output: 45.10
Encoder Value: 41
PID Input: 25.00   PID Set point: 5.00   PID Output: 44.09
Encoder Value: 42
PID Input: 25.00   PID Set point: 5.56   PID Output: 44.09
Encoder Value: 43
PID Input: 25.25   PID Set point: 6.11   PID Output: 44.96
Encoder Value: 44
PID Input: 25.00   PID Set point: 6.67   PID Output: 38.44
Encoder Value: 45
PID Input: 25.25   PID Set point: 7.22   PID Output: 38.44
Encoder Value: 46
PID Input: 25.00   PID Set point: 7.78   PID Output: 38.81
Encoder Value: 47
PID Input: 25.00   PID Set point: 8.33   PID Output: 37.78
Encoder Value: 48
PID Input: 25.00   PID Set point: 8.89   PID Output: 37.78
Encoder Value: 49
PID Input: 25.00   PID Set point: 9.44   PID Output: 35.63
Encoder Value: 50
PID Input: 25.00   PID Set point: 10.00   PID Output: 35.63
Encoder Value: 51
PID Input: 25.25   PID Set point: 10.56   PID Output: 36.48
Encoder Value: 52
PID Input: 25.00   PID Set point: 11.11   PID Output: 36.48
Encoder Value: 53
PID Input: 25.25   PID Set point: 11.67   PID Output: 31.83
Encoder Value: 54
PID Input: 25.00   PID Set point: 12.22   PID Output: 27.78
Encoder Value: 55
PID Input: 25.00   PID Set point: 12.78   PID Output: 27.78
Encoder Value: 56
PID Input: 25.00   PID Set point: 13.33   PID Output: 28.12
Encoder Value: 57
PID Input: 25.00   PID Set point: 13.89   PID Output: 28.12
Encoder Value: 58
PID Input: 25.00   PID Set point: 14.44   PID Output: 25.95
Encoder Value: 59
PID Input: 25.00   PID Set point: 15.00   PID Output: 25.95
Encoder Value: 60
PID Input: 25.00   PID Set point: 15.56   PID Output: 23.77
Encoder Value: 61
PID Input: 25.00   PID Set point: 16.11   PID Output: 23.77
Encoder Value: 62
PID Input: 25.00   PID Set point: 16.67   PID Output: 21.59
Encoder Value: 63
PID Input: 25.25   PID Set point: 17.22   PID Output: 21.59
Encoder Value: 64
PID Input: 25.00   PID Set point: 17.78   PID Output: 19.41
Encoder Value: 65
PID Input: 25.00   PID Set point: 18.33   PID Output: 19.41
Encoder Value: 66
PID Input: 25.25   PID Set point: 18.89   PID Output: 20.22
Encoder Value: 67
PID Input: 25.00   PID Set point: 19.44   PID Output: 13.63
Encoder Value: 68
PID Input: 25.00   PID Set point: 20.00   PID Output: 13.63
Encoder Value: 69
PID Input: 25.00   PID Set point: 20.56   PID Output: 13.93
Encoder Value: 70
PID Input: 25.25   PID Set point: 21.11   PID Output: 15.84
Encoder Value: 71
PID Input: 25.25   PID Set point: 21.67   PID Output: 15.84
Encoder Value: 72
PID Input: 25.00   PID Set point: 22.22   PID Output: 8.14
Encoder Value: 73
PID Input: 25.00   PID Set point: 22.78   PID Output: 9.54
Encoder Value: 74
PID Input: 25.00   PID Set point: 23.33   PID Output: 9.54
Encoder Value: 75
PID Input: 25.25   PID Set point: 23.89   PID Output: 10.32
Encoder Value: 76
PID Input: 25.00   PID Set point: 24.44   PID Output: 10.32
Encoder Value: 77
PID Input: 25.00   PID Set point: 25.00   PID Output: 2.60
Encoder Value: 78
PID Input: 25.25   PID Set point: 25.56   PID Output: 6.98
Encoder Value: 79
PID Input: 25.25   PID Set point: 26.11   PID Output: 6.98
Encoder Value: 80
PID Input: 25.25   PID Set point: 26.67   PID Output: 2.26
Encoder Value: 81
PID Input: 25.25   PID Set point: 27.22   PID Output: 1.13
Encoder Value: 82
PID Input: 25.00   PID Set point: 27.78   PID Output: 1.13
Encoder Value: 83
PID Input: 25.25   PID Set point: 28.33   PID Output: 0.00
Encoder Value: 84
PID Input: 25.00   PID Set point: 28.89   PID Output: 0.00
Encoder Value: 85
PID Input: 25.25   PID Set point: 29.44   PID Output: 0.00
Encoder Value: 86
PID Input: 25.00   PID Set point: 30.00   PID Output: 0.00
Encoder Value: 87
PID Input: 25.00   PID Set point: 30.56   PID Output: 0.00
Encoder Value: 88
PID Input: 25.00   PID Set point: 31.11   PID Output: 0.00
Encoder Value: 89
PID Input: 25.00   PID Set point: 31.67   PID Output: 0.00
Encoder Value: 90
PID Input: 25.00   PID Set point: 32.22   PID Output: 0.00
Encoder Value: 91
PID Input: 25.00   PID Set point: 32.78   PID Output: 0.00
Encoder Value: 92
PID Input: 25.00   PID Set point: 33.33   PID Output: 0.00
Encoder Value: 93
PID Input: 25.00   PID Set point: 33.89   PID Output: 0.00
Encoder Value: 94
PID Input: 25.00   PID Set point: 34.44   PID Output: 0.00
Encoder Value: 95
PID Input: 25.00   PID Set point: 35.00   PID Output: 0.00
Encoder Value: 96
PID Input: 25.00   PID Set point: 35.56   PID Output: 0.00
Encoder Value: 97
PID Input: 25.25   PID Set point: 36.11   PID Output: 0.00
Encoder Value: 98
PID Input: 25.25   PID Set point: 36.67   PID Output: 0.00
Encoder Value: 99
PID Input: 25.00   PID Set point: 37.22   PID Output: 0.00
Encoder Value: 100
PID Input: 25.00   PID Set point: 37.78   PID Output: 0.00
Encoder Value: 101
PID Input: 25.00   PID Set point: 38.33   PID Output: 0.00
Encoder Value: 102
PID Input: 25.25   PID Set point: 38.89   PID Output: 0.00
Encoder Value: 103
PID Input: 25.25   PID Set point: 39.44   PID Output: 0.00
Encoder Value: 104
PID Input: 25.00   PID Set point: 40.00   PID Output: 0.00
Encoder Value: 105
PID Input: 25.00   PID Set point: 40.56   PID Output: 0.00
Encoder Value: 106
PID Input: 25.00   PID Set point: 41.11   PID Output: 0.00

pidtempcontrollerSerial.txt (8.48 KB)

    analogWrite(RelayPin,Output); //PWM

What sort of relay are you using that you PWM it?

PID Input: 25.00   PID Set point: 32.22   PID Output: 0.00

The PID is working OK. It's trying to turn the heater off. By the sound of it, it isn't succeeding.