Hello, I am controlling a cartridge heater (12VDC-40W) to maintain a desired temperature with the help of this N-Channel MOSFET, 100K-Ohm Thermistor and obviously Arduino UNO. I used the PID_Relay Output example given with Arduino. And I used the example from Adafruit to measure the temperature using Thermistor. Everything is fine. No need of Autotune. Just example code suffices for me. But the only problem is, I set the temperature at 100C. But the code keeps the heater controlled at around 65C. That means, the code doesn't hold the temperature at desireds level but still it works in the sense that it maintains a specific temperature (more or less). For my case, this accuracy works well. So, would you please help anyone to identify the problem? How to set setpoint? NB. I may need to maintain temp up to around 250 (the thermistor has limit up to 300C). I used this assembled code:
#include <PID_v1.h>
#define RelayPin 7
// which analog pin to connect
#define THERMISTORPIN A0
// resistance at 25 degrees C
#define THERMISTORNOMINAL 100000
// temp. for nominal resistance (almost always 25 C)
#define TEMPERATURENOMINAL 25
// how many samples to take and average, more takes longer
// but is more 'smooth'
#define NUMSAMPLES 5
// The beta coefficient of the thermistor (usually 3000-4000)
#define BCOEFFICIENT 4267
// the value of the 'other' resistor
#define SERIESRESISTOR 100000
int samples[NUMSAMPLES];
//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;
void setup()
{
Serial.begin(9600);
analogReference(EXTERNAL);
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()
{
uint8_t i;
float average;
// take N samples in a row, with a slight delay
for (i=0; i< NUMSAMPLES; i++) {
samples[i] = analogRead(THERMISTORPIN);
delay(10);
}
// average all the samples out
average = 0;
for (i=0; i< NUMSAMPLES; i++) {
average += samples[i];
}
average /= NUMSAMPLES;
//Serial.print("Average analog reading ");
//Serial.println(average);
// convert the value to resistance
average = 1023 / average - 1;
average = SERIESRESISTOR / average;
//Serial.print("Thermistor resistance ");
//Serial.println(average);
float steinhart;
steinhart = average / THERMISTORNOMINAL; // (R/Ro)
steinhart = log(steinhart); // ln(R/Ro)
steinhart /= BCOEFFICIENT; // 1/B * ln(R/Ro)
steinhart += 1.0 / (TEMPERATURENOMINAL + 273.15); // + (1/To)
steinhart = 1.0 / steinhart; // Invert
steinhart -= 273.15; // convert to C
Serial.print("Temperature ");
Serial.print(steinhart);
Serial.println(" *C");
delay(1000);
Input = steinhart;
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(RelayPin,HIGH);
else digitalWrite(RelayPin,LOW);
}
I have attached a screen-shot of serial monitor and the wiring diagram. Thank you for your time!



