Components and supplies
Arduino enclosure (optional)
Power N-MOSFET
Resistor 10k ohm
10 K NTC Thermistor
USB-A to B Cable
Arduino prototype shield
Arduino UNO
Tools and machines
Soldering iron (generic)
Project description
Code
Heater control library header
c_cpp
Header for heater control library
1/**************************************************************** 2 * Heater control library header 3 * 4*****************************************************************/ 5 6#ifndef HeaterControl_h 7#define HeaterControl_h 8 9#include "Arduino.h" 10 11class HeaterControl 12{ 13 public: 14 HeaterControl(int FETPin); 15 int Control_PID(double dCurrentTemperature, int dt, int ThresholdTemp, double MaxTemp, double MinTemp, double SetTemp, double K_P_ctrl, double K_I_ctrl, double K_D_ctrl); //for ATMega328P 16 void TurnOff(); 17 18 private: 19 //received 20 int _FETPin; 21 22 //internal 23 float previous_error = 0; //Initialize PID variables 24 float s_integral = 0; //Initialize PID variables 25}; 26 27#endif 28
Temperature controller
c_cpp
Complete temperature controller code. Just copy and paste. Includes: - thermistor code for temperature readout - code for controlling heater input - basic PID controller - basic serial interface
1// variables and definitions 2 3 //Important parameter, set to match environment 4 const int dt = 500; // [ms] time constant in milliseconds (controller clock rate = 1/(dt/1000) [Hz]) 5 #define SetTemp 62.8 // [degC] set temperature in DegC 6 #define MinTemp 20 // [degC] minimum expected temperature (needed for rescaling inputs) 7 #define MaxTemp 65 // [degC] maximum allowed temperature, over which heater is turned off (needed for rescaling inputs) 8 int SetTime = 1800; // [s] timer in seconds, if reached, running stops [Default: 1800] 9 10 //I/O pins - don't edit unless replaced 11 #define thermistorPin A0 12 #define FETPin 3 13 //#define LEDPin //number of LED pin (optional) 14 15 //control parameters - editing not recommended 16 double K_P_ctrl = 15; //proportional gain 17 double K_I_ctrl = 0; //integral gain (set to lower values i.e. 10^-3) 18 double K_D_ctrl = 0; //derivative gain 19 20// including headers and definitions 21 #include <math.h> 22 23//Inititalization 24 //target temperature reached? 25 bool bInRange = 0; 26 27 //ticks per ms 28 int TicksPerMS = floor(1000/dt); 29 30 //Initialize PID variables: 31 float previous_error = 0; 32 float s_integral = 0; 33 34//Thermistor code 35 double Thermistor(int RawADC) { 36 double Temp; 37 Temp = log(10000.0*((1024.0/RawADC-1))); 38 Temp = 1 / (0.001129148 + (0.000234125 + (0.0000000876741 * Temp * Temp ))* Temp ); 39 Temp = Temp - 273.15; // Convert Kelvin to Celcius 40 return Temp; 41 } 42 43//PID controller code 44 void Control_PID(double iTemp){ 45 46 //Overheat protection 47 if(iTemp>MaxTemp){ 48 analogWrite(FETPin, 0); 49 Serial.println("Error:overheat. Heater turned off"); 50 return; 51 } 52 53 //In range? If in range, maybe turn on LED? 54 if((iTemp) >= SetTemp){ 55 if(bInRange==0){ 56 //digitalWrite(LEDPin, HIGH); 57 bInRange=1; 58 } 59 }else{ 60 if(bInRange==1){ 61 //digitalWrite(LEDPin, LOW); 62 bInRange=0; 63 } 64 } 65 66 67 //PID subroutine 68 float err = SetTemp - iTemp; 69 //Serial.println(err); 70 s_integral += err*dt; 71 //Serial.println(s_integral); 72 float s_derivative = (err - previous_error)/dt; 73 //Serial.println(s_derivative); 74 int U_in_ctrl = (K_P_ctrl*err + K_I_ctrl*s_integral + K_D_ctrl*s_derivative)/(MaxTemp-MinTemp)*255; 75 previous_error = err; 76 77 78 // put voltage to output and write value to serial monitor 79 Serial.print("Output PWM frequency: "); 80 81 if (U_in_ctrl<=255){ 82 if (U_in_ctrl > 0){ 83 analogWrite(FETPin, U_in_ctrl); 84 Serial.println(U_in_ctrl); 85 } 86 else 87 { 88 analogWrite(FETPin, 1); 89 Serial.println("1 - cca. 0 V"); 90 } 91 } 92 else{ 93 analogWrite(FETPin,255); 94 Serial.println("255 - cca. 5 V"); 95 } 96 } 97 98void setup() { 99 Serial.begin(9600); 100 pinMode(FETPin, OUTPUT); 101 //pinMode(LEDPin, OUTPUT); 102 103 //rescale timer according to dt 104 SetTime = SetTime * TicksPerMS; 105} 106 107void loop() { 108 //Take a temperature reading and display it 109 double Temp = double(Thermistor(analogRead(thermistorPin))); 110 Serial.print("Temperature:"); 111 Serial.println(Temp); // display temperature 112 113 //Timer serial out - displays time on serial monitor 114 Serial.print(SetTime/60*dt/1000); 115 Serial.print(" [mins] - SetTime: "); 116 Serial.print(SetTime); 117 Serial.println(""); 118 119 //Call controller algorithm 120 Control_PID(Temp); // call controller algorithm 121 122 //End line in serial monitor... 123 Serial.println(""); 124 Serial.println(""); 125 126 //Timer ticking (countdown) 127 if (SetTime>0){ 128 SetTime--; 129 // if zero reached 130 if (SetTime==0){ 131 132 while(1) { 133 //loop until disconnected 134 Serial.println("Time ran out, controller stopped. Please disconnect or reset the controller."); 135 digitalWrite(FETPin, LOW); 136 delay(dt); 137 } 138 } 139 } 140 141 //wait dt before next cycle 142 delay(dt); 143} 144
Thermometer library CPP
c_cpp
Arduino library for thermistor input. You can include this in your own project
1#include "Arduino.h" 2#include "Thermistor.h" 3 4Thermistor::Thermistor(int ThermistorPin) 5{ 6 pinMode(ThermistorPin, INPUT); 7 thermistorPin = ThermistorPin; 8} 9 10double Thermistor::Thermistor_Read(int TemperatureOffset, double T0, double R0, double B) 11{ 12 //Thermistor code with a generalized equation 13 //Steinhart-Hart equation: T[K]=1/(a+b*log(R)+c*(log(R))^3) 14 //where a=1/T_0-(1/B)*log(R_0), b=1/B and c = 0 15 16 //take a reading: 17 int RawADC = analogRead(thermistorPin); 18 double Temp; 19 20 double R; 21 R = 10000.0 * (1024.0 / RawADC - 1); 22 Temp = 1 / (1 / T0 + 1 / B * log(R / R0)); 23 Temp = Temp - 273.15; // Convert Kelvin to Celsius 24 return Temp; 25}
Heater control library CPP
c_cpp
Arduino library for heater control. You can include this in your own project.
1/**************************************************************** 2 * Heater control library 3 * 4 * Return codes: 5 * 1: in range 6 * 0: not in range 7 * -1: error 8*****************************************************************/ 9 10#include "Arduino.h" 11#include "HeaterControl.h" 12 13HeaterControl::HeaterControl(int FETPin) 14{ 15 pinMode(FETPin, OUTPUT); 16 _FETPin = FETPin; 17} 18 19int HeaterControl::Control_PID(double dCurrentTemperature, int dt, int ThresholdTemp, double MaxTemp, double MinTemp, double SetTemp, double K_P_ctrl, double K_I_ctrl, double K_D_ctrl) 20{ 21 bool bInRange =0; 22 23 //Overheat protection 24 if (dCurrentTemperature > MaxTemp) { 25 analogWrite(_FETPin, 0); //turn off heater 26 return -1; //return with overheat code 27 } 28 29 //In range? If yes, return with in range code 30 if (dCurrentTemperature >= ThresholdTemp && dCurrentTemperature < (ThresholdTemp + 2)) { bInRange = 1; } 31 32 //PID subroutine 33 float err = SetTemp - dCurrentTemperature; 34 s_integral += err * dt; 35 float s_derivative = (err - previous_error) / dt; 36 int U_in_ctrl = (K_P_ctrl * err + K_I_ctrl * s_integral + K_D_ctrl * s_derivative) / (MaxTemp - MinTemp) * 255; 37 previous_error = err; 38 39 // put voltage to output and write value to serial monitor 40 if (U_in_ctrl <= 255) 41 { 42 if (U_in_ctrl > 0) { 43 analogWrite(_FETPin, U_in_ctrl); 44 } 45 else 46 { 47 analogWrite(_FETPin, 1); 48 } 49 } 50 else 51 { 52 analogWrite(_FETPin, 255); 53 } 54 55 return bInRange; 56} 57 58 59void HeaterControl::TurnOff() 60{ 61 analogWrite(_FETPin, 0); //turn off heater 62} 63 64
Thermometer library header
c_cpp
Header for thermometer library
1/* Thermistor control library header 2*/ 3 4#ifndef Thermistor_h 5#define Thermistor_h 6 7#include "Arduino.h" 8 9class Thermistor 10{ 11 public: 12 Thermistor(int ThermistorPin); //constructor 13 double Thermistor_Read(int TemperatureOffset); 14 double Thermistor_Read(int TemperatureOffset, double T0, double R0, double B); 15 16 private: 17 int thermistorPin; 18}; 19 20#endif 21
Heater control library CPP
c_cpp
Arduino library for heater control. You can include this in your own project.
1/**************************************************************** 2 * Heater control library 3 * 4 * Return codes: 5 * 1: in range 6 * 0: not in range 7 * -1: error 8*****************************************************************/ 9 10#include "Arduino.h" 11#include "HeaterControl.h" 12 13HeaterControl::HeaterControl(int FETPin) 14{ 15 pinMode(FETPin, OUTPUT); 16 _FETPin = FETPin; 17} 18 19int HeaterControl::Control_PID(double dCurrentTemperature, int dt, int ThresholdTemp, double MaxTemp, double MinTemp, double SetTemp, double K_P_ctrl, double K_I_ctrl, double K_D_ctrl) 20{ 21 bool bInRange =0; 22 23 //Overheat protection 24 if (dCurrentTemperature > MaxTemp) { 25 analogWrite(_FETPin, 0); //turn off heater 26 return -1; //return with overheat code 27 } 28 29 //In range? If yes, return with in range code 30 if (dCurrentTemperature >= ThresholdTemp && dCurrentTemperature < (ThresholdTemp + 2)) { bInRange = 1; } 31 32 //PID subroutine 33 float err = SetTemp - dCurrentTemperature; 34 s_integral += err * dt; 35 float s_derivative = (err - previous_error) / dt; 36 int U_in_ctrl = (K_P_ctrl * err + K_I_ctrl * s_integral + K_D_ctrl * s_derivative) / (MaxTemp - MinTemp) * 255; 37 previous_error = err; 38 39 // put voltage to output and write value to serial monitor 40 if (U_in_ctrl <= 255) 41 { 42 if (U_in_ctrl > 0) { 43 analogWrite(_FETPin, U_in_ctrl); 44 } 45 else 46 { 47 analogWrite(_FETPin, 1); 48 } 49 } 50 else 51 { 52 analogWrite(_FETPin, 255); 53 } 54 55 return bInRange; 56} 57 58 59void HeaterControl::TurnOff() 60{ 61 analogWrite(_FETPin, 0); //turn off heater 62} 63 64
Temperature controller
c_cpp
Complete temperature controller code. Just copy and paste. Includes: - thermistor code for temperature readout - code for controlling heater input - basic PID controller - basic serial interface
1// variables and definitions 2 3 //Important parameter, set to match environment 4 const int dt = 500; // [ms] time constant in milliseconds (controller clock rate = 1/(dt/1000) [Hz]) 5 #define SetTemp 62.8 // [degC] set temperature in DegC 6 #define MinTemp 20 // [degC] minimum expected temperature (needed for rescaling inputs) 7 #define MaxTemp 65 // [degC] maximum allowed temperature, over which heater is turned off (needed for rescaling inputs) 8 int SetTime = 1800; // [s] timer in seconds, if reached, running stops [Default: 1800] 9 10 //I/O pins - don't edit unless replaced 11 #define thermistorPin A0 12 #define FETPin 3 13 //#define LEDPin //number of LED pin (optional) 14 15 //control parameters - editing not recommended 16 double K_P_ctrl = 15; //proportional gain 17 double K_I_ctrl = 0; //integral gain (set to lower values i.e. 10^-3) 18 double K_D_ctrl = 0; //derivative gain 19 20// including headers and definitions 21 #include <math.h> 22 23//Inititalization 24 //target temperature reached? 25 bool bInRange = 0; 26 27 //ticks per ms 28 int TicksPerMS = floor(1000/dt); 29 30 //Initialize PID variables: 31 float previous_error = 0; 32 float s_integral = 0; 33 34//Thermistor code 35 double Thermistor(int RawADC) { 36 double Temp; 37 Temp = log(10000.0*((1024.0/RawADC-1))); 38 Temp = 1 / (0.001129148 + (0.000234125 + (0.0000000876741 * Temp * Temp ))* Temp ); 39 Temp = Temp - 273.15; // Convert Kelvin to Celcius 40 return Temp; 41 } 42 43//PID controller code 44 void Control_PID(double iTemp){ 45 46 //Overheat protection 47 if(iTemp>MaxTemp){ 48 analogWrite(FETPin, 0); 49 Serial.println("Error:overheat. Heater turned off"); 50 return; 51 } 52 53 //In range? If in range, maybe turn on LED? 54 if((iTemp) >= SetTemp){ 55 if(bInRange==0){ 56 //digitalWrite(LEDPin, HIGH); 57 bInRange=1; 58 } 59 }else{ 60 if(bInRange==1){ 61 //digitalWrite(LEDPin, LOW); 62 bInRange=0; 63 } 64 } 65 66 67 //PID subroutine 68 float err = SetTemp - iTemp; 69 //Serial.println(err); 70 s_integral += err*dt; 71 //Serial.println(s_integral); 72 float s_derivative = (err - previous_error)/dt; 73 //Serial.println(s_derivative); 74 int U_in_ctrl = (K_P_ctrl*err + K_I_ctrl*s_integral + K_D_ctrl*s_derivative)/(MaxTemp-MinTemp)*255; 75 previous_error = err; 76 77 78 // put voltage to output and write value to serial monitor 79 Serial.print("Output PWM frequency: "); 80 81 if (U_in_ctrl<=255){ 82 if (U_in_ctrl > 0){ 83 analogWrite(FETPin, U_in_ctrl); 84 Serial.println(U_in_ctrl); 85 } 86 else 87 { 88 analogWrite(FETPin, 1); 89 Serial.println("1 - cca. 0 V"); 90 } 91 } 92 else{ 93 analogWrite(FETPin,255); 94 Serial.println("255 - cca. 5 V"); 95 } 96 } 97 98void setup() { 99 Serial.begin(9600); 100 pinMode(FETPin, OUTPUT); 101 //pinMode(LEDPin, OUTPUT); 102 103 //rescale timer according to dt 104 SetTime = SetTime * TicksPerMS; 105} 106 107void loop() { 108 //Take a temperature reading and display it 109 double Temp = double(Thermistor(analogRead(thermistorPin))); 110 Serial.print("Temperature:"); 111 Serial.println(Temp); // display temperature 112 113 //Timer serial out - displays time on serial monitor 114 Serial.print(SetTime/60*dt/1000); 115 Serial.print(" [mins] - SetTime: "); 116 Serial.print(SetTime); 117 Serial.println(""); 118 119 //Call controller algorithm 120 Control_PID(Temp); // call controller algorithm 121 122 //End line in serial monitor... 123 Serial.println(""); 124 Serial.println(""); 125 126 //Timer ticking (countdown) 127 if (SetTime>0){ 128 SetTime--; 129 // if zero reached 130 if (SetTime==0){ 131 132 while(1) { 133 //loop until disconnected 134 Serial.println("Time ran out, controller stopped. Please disconnect or reset the controller."); 135 digitalWrite(FETPin, LOW); 136 delay(dt); 137 } 138 } 139 } 140 141 //wait dt before next cycle 142 delay(dt); 143} 144
Thermometer library CPP
c_cpp
Arduino library for thermistor input. You can include this in your own project
1#include "Arduino.h" 2#include "Thermistor.h" 3 4Thermistor::Thermistor(int 5 ThermistorPin) 6{ 7 pinMode(ThermistorPin, INPUT); 8 thermistorPin = ThermistorPin; 9} 10 11double 12 Thermistor::Thermistor_Read(int TemperatureOffset, double T0, double R0, double 13 B) 14{ 15 //Thermistor code with a generalized equation 16 //Steinhart-Hart 17 equation: T[K]=1/(a+b*log(R)+c*(log(R))^3) 18 //where a=1/T_0-(1/B)*log(R_0), 19 b=1/B and c = 0 20 21 //take a reading: 22 int RawADC = analogRead(thermistorPin); 23 24 double Temp; 25 26 double R; 27 R = 10000.0 * (1024.0 / RawADC - 1); 28 29 Temp = 1 / (1 / T0 + 1 / B * log(R / R0)); 30 Temp = Temp - 273.15; // 31 Convert Kelvin to Celsius 32 return Temp; 33}
Thermometer library header
c_cpp
Header for thermometer library
1/* Thermistor control library header 2*/ 3 4#ifndef Thermistor_h 5#define 6 Thermistor_h 7 8#include "Arduino.h" 9 10class Thermistor 11{ 12 public: 13 14 Thermistor(int ThermistorPin); //constructor 15 double Thermistor_Read(int 16 TemperatureOffset); 17 double Thermistor_Read(int TemperatureOffset, double 18 T0, double R0, double B); 19 20 private: 21 int thermistorPin; 22}; 23 24#endif 25
Downloadable files
Circuit diagram (overview)
Circuit diagram (overview)
Circuit diagram (overview)
Circuit diagram (overview)
Comments
Only logged in users can leave comments