#define TEMP 1
#define CAN 0
#define N_CTN 4 // number of points in TNC calibration table
#define N_TC 12 // number of points in thermocouple calibration table
// Table calibration for THERMISTOR
float calib_CTN [N_CTN][2] = {
{340,15},
{395,19},
{405,20},
{470,30} // put here calibration points : first the value read on arduino ADC, second the value read on your reference thermoemter.
};
// Table calibration for THERMO COUPLE
float calib_TC [N_TC][2] = {
{0,0}, // first point : at 0°C the TC give no voltage
{25,10},
{135,40},
{305,86},
{444, 125},
{580,160},
{616,170},
{655,180},
{709,195},
{734,201},
{841,231},
{888,261}
};
void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
pinMode(2,OUTPUT);
pinMode(4,OUTPUT) ;
pinMode(3,INPUT) ; digitalWrite(3, HIGH) ;
}
// the loop routine runs over and over again forever:
void loop() {
// read the input on analog pin 0:
int Can_comp = analogRead(A0);
int Can_TC = analogRead(A1);
float T_ctn = converti(Can_comp , calib_CTN, N_CTN);
float T_Thcp = converti(Can_TC, calib_TC, N_TC) ;
// print out the value you read:
Serial.print("CTN ADC out ="); Serial.print(Can_comp);
Serial.print("\tT ref ="); Serial.print(T_ctn);
Serial.print("\tTcoup ADC out ="); Serial.print(Can_TC);
Serial.print("\tTemp TC ="); Serial.print(T_Thcp);
Serial.print("\t"); Serial.println(T_Thcp + T_ctn,2); // Temperature from Thermo couple is
// added to cold junction temperature for compensation
delay(100); // delay for stability
}
float Int_lin(float x, // input : abcisse where the fonction is to be interpolated
float X1, float Y1 , // Coordinates of lower interpolation point
float X2, float Y2 // Coordinates of upper interpolation point
){
// linear interpolation : x beetween X1 and X2
// return interpolated value of y = f(x)
float a , b ;
a = (Y1 - Y2) / (X1 - X2) ;
b = Y1 - a*X1 ;
return a*x + b ;
}
float converti(float X , // value read on ADC to convert in °C
float calib[][2], // Calibration table to use --> not the same for TCN en thermo couple
int N
){
int i ;
if (X < calib [0][CAN]){ // Entry outside calibration table (lower) --> extrapolation
// with the two first points of table
return Int_lin(X,
calib [0][CAN], calib [0][TEMP] ,
calib [1][CAN], calib [1][TEMP]) ;
}
if (X > calib [N-1][CAN]){ // Entry outside calibration table (higher) --> extrapolation
// with the two last points of table
return Int_lin(X,
calib [N-1][CAN], calib [N-1][TEMP] ,
calib [N][CAN], calib [N][TEMP]) ;
}
for (i=0 ; i < N ; i++){
if (X < calib [i][CAN]){
return Int_lin(X,
calib [i-1][CAN], calib [i-1][TEMP] ,
calib [i][CAN], calib [i][TEMP]) ;
}
}
}