PID Temperature control issues

Hi,

I was trying to perform temperature control using a PID control feedback mechanism. I was trying to use the following code to ensure that the temperature of my system reaches the setpoint, and then stays on it; However, what I see is that after my temperature reaches the set point, the pwm output from the code becomes zero, which means that then the heater gets full power and it keeps on heating.
Please note that you can ignore the conversion factors that I have applied for the temperature. Please do let me know if there is something that I'm missing or need to add.

This is my code:

#include <PID_v1.h>
//Initialization
//const int tempPin = A0 //Temperature Sensor input
// Maybe add potentionmeter to tune to the needed temperature
const int heater = 13; //Heater Output
//n------------------------------------------------------------

//Tuning Parameters
float Kp = 1;
float Ki = 0;
float Kd = 0;
int sensorPin = 0;

//For storing Values
double Setpoint, Input, Output;
PID myPID(&Input, &Output, &Setpoint, Kp, Ki, Kd, DIRECT);
// NOTES: PV is input, u(t) is output and SP is setpoint
const int sampleRate = 1; //Speed of PID loop

const long serialPing = 500;
unsigned long now = 0;
unsigned long lastMessage =0;

//------------------------------------------------------------
//The following code is used to get the Temperature Reading
float tempC;
int reading;
int tempPin = A0;
//int pot = A1;

void setup()
{
//Setpoint = map(analogRead(pot), 0, 1024, 0, 255);
Setpoint = 60;
int reading1 = analogRead(sensorPin);
float voltage1 = reading1 * 5.0;
voltage1 /= 1024.0;
Input = (voltage1 - 0.5) * 100;
Serial.begin(9600);
myPID.SetMode(AUTOMATIC);
}

void loop()
{
//getting the voltage reading from the temperature sensor
int reading = analogRead(sensorPin);
// converting that reading to voltage, for 3.3v arduino use 3.3
float voltage = reading * 5.0;
voltage /= 1024.0;

// print out the voltage
//Serial.print(voltage); Serial.println(" volts");

// now print out the temperature
float temperatureC = (voltage - 0.5) * 100 ; //converting from 10 mv per degree wit 500 mV offset
//to degrees ((voltage - 500mV) times 100)
Input = temperatureC;
Serial.print(temperatureC); Serial.print("\t");

delay(500);
//Setpoint = map(analogRead(pot), 0, 1024,0,255);
Setpoint = 60;
myPID.Compute();
analogWrite(heater,Output);
delay(100);
Serial.print(Output);
Serial.print("\t");
Serial.print(Kp);
Serial.print("\t");
Serial.print(Ki);
Serial.print("\t");
Serial.print(Kd);
Serial.println();

}
//End Temperature Reading Code
//------------------------------------------------------------

PIDcontrol_Code_final.ino (2.03 KB)

yudhveer2:
the pwm output from the code becomes zero, which means that then the heater gets full power and it keeps on heating.

Seems unusual. If that's right then you would need to use the PID in REVERSE.

If the output is negative logic, change

analogWrite(heater,Output);

to

analogWrite(heater,255-Output);