-
Notifications
You must be signed in to change notification settings - Fork 0
/
Heating_System_PID.ino
62 lines (51 loc) · 1.25 KB
/
Heating_System_PID.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include<math.h>
#include<PID_v1.h>
//Define constants for Temperature calculation
const int transistorPin = 9;
int ThermistorPin= A0; //define thermistor
int Vo;
float R1=10000;
float logR2, R2, T;
float c1= 1.009249522e-03, c2=2.378405444e-04,c3=2.019202697e-07;
float target_value = 30.0;
//Define constants for PID control
double SetPoint;
double Input;
double Output;
double Kp=0,Ki=5,Kd=0;
//create PID instance
PID myPID(&Input,&Output,&SetPoint,Kp,Ki,Kd,DIRECT);
void setup (){
Serial.begin(9600) ;
pinMode (transistorPin, OUTPUT) ;
pinMode (ThermistorPin, INPUT) ;
//Setting parameters for PID
SetPoint=target_value-5;
myPID.SetMode(AUTOMATIC);
myPID.SetTunings(Kp,Ki,Kd);
}
void loop (){
//Temperature Detection
Vo=analogRead(ThermistorPin) ;
R2=R1* (1023.0/(float)Vo-1.0) ;
logR2=log(R2);
T=(1.0/(c1+c2*logR2+c3*logR2*logR2*logR2) ) ;
T=T-273.15;
Serial.print("Temperature=");
Serial.print(T);
Serial.print("C ");
//delay (1000) ;
Input=T;
myPID.Compute();
//Controller
if (T<target_value-0.5){
analogWrite(transistorPin,Output);
//delay(100);
Serial.println(Output);
}
else if(T>=target_value+0.5){
analogWrite(transistorPin,Output);
//delay(100);
Serial.println(Output);
}
}