Using polynomial equation to determine thermocouple temperature

Hi,

I am a mechanical engineering student (with basic programming knowledge) building a code to convert emf produced by a thermocouple to temperature. For this I am using the polynomial equations and constants provided by NIST:

Temp= c0 + c1E + d2E^2 + ... + cn*E^n,

The issue is that I am receiving incorrect values of temperatures. At 12mv of input the code is supposed to return 1662 deg c. However the code is returning very large numbers. To verify the polynomial equation I tried it out in excel and it returned correct temperatures. Hence I am assuming that the polynomial equation is being written correct.

I know that float has some inaccuracy, but I do not expect it to throw off the temperature by thousands (4185264128.0000)
..

Here is a glimpse of the code, I also tried it in different ways to eliminate pow();

float mv_sns = 12.0; // input voltage
  
 // polynomial coefficients: 
  float c0 = 213.150712;
  float c1 = 285.10504*mv_sns;
  float c2 = -52.742887*mv_sns;
  float c3 = 9.9160804*mv_sns; 
  float c4 = -1.2965303*mv_sns;
  float c5 = 0.11195870*mv_sns;
  float c6 = -0.0060625199*mv_sns;
  float c7 = 0.00018661696*mv_sns;
  float c8 = -0.0000024878585*mv_sns;

 float temp = (c0 + c1 + (c2*c2)+ (c3*c3*c3)+(c4*c4*c4*c4)+(c5*c5*c5*c5*c5)+(c6*c6*c6*c6*c6*c6)+(c7*c7*c7*c7*c7*c7*c7)+(c8*c8*c8*c8*c8*c8*c8*c8));
  //float temp2 = ( c0 + (c1*mv_sns) + pow(c2*mv_sns,2) + pow(c3*mv_sns,3) + pow(c4*mv_sns,4) + pow(c5*mv_sns,5) + pow(c6*mv_sns,6) + pow(c7*mv_sns,7) + pow(c8*mv_sns,8)); 
    
   
   Serial.println(temp,4);
   delay(600);

I also tried this way which returned same results:

float temp2 = ( c0 + (c1*mv_sns) + pow(c2*mv_sns,2) + pow(c3*mv_sns,3) + pow(c4*mv_sns,4) + pow(c5*mv_sns,5) + pow(c6*mv_sns,6) + pow(c7*mv_sns,7) + pow(c8*mv_sns,8));

Your help would be highly appreciated!

Is the input supposed to be in volts? If so you should use:

float mv_sns = .012; // input voltage

Pete

Hi Pete,

Thanks for your reply. The input is supposed to be fed in mv. Even so, When I tried it as you suggested the temp does not make sense.

I have no expertise in this matter. But, every time I've seen this notation,

Temp= c0 + c1E + d2E^2 + ... + cn*E^n,

it has meant this

c0 + c1E + d2(E^2) + ... + cn*(E^n)

rather than this

c0 + c1E + (d2E)^2 + ... + (cn*E)^n

When I try the corrected version in a spreadsheet, with voltage in millivolts, it sums to 1662.9, which is pretty close to 1662.

Maybe that's what you wanted to do?

Found it, but tmd3 just beat me to it :slight_smile:

I rewrote your code to use an array for the coefficients and input using mV which gives the expected result:

void setup()
{
  Serial.begin(9600);
  // put your setup code here, to run once:
float mv_sns = 12; // input voltage
 
 // polynomial coefficients:
float poly[9] = {
  213.150712,
  285.10504,
  -52.742887,
  9.9160804,
  -1.2965303,
  0.11195870,
  -0.0060625199,
  0.00018661696,
  -0.0000024878585,
};
// float temp = (c0 + c1 + (c2*c2)+ (c3*c3*c3)+(c4*c4*c4*c4)+(c5*c5*c5*c5*c5)+(c6*c6*c6*c6*c6*c6)+(c7*c7*c7*c7*c7*c7*c7)+(c8*c8*c8*c8*c8*c8*c8*c8));
  //float temp2 = ( c0 + (c1*mv_sns) + pow(c2*mv_sns,2) + pow(c3*mv_sns,3) + pow(c4*mv_sns,4) + pow(c5*mv_sns,5) + pow(c6*mv_sns,6) + pow(c7*mv_sns,7) + pow(c8*mv_sns,8));

  float temp = 0;
  float mult = 1;
  for(int i = 0;i < 9;i++) {
    temp += mult*poly[i];
    mult *= mv_sns;
  }

   
   Serial.println(temp,6);

}

void loop() {
  // put your main code here, to run repeatedly:

}

Pete

Yep :smiley: , I confirm it works. Thank you all for your time ! :slight_smile:

Don't use pow( ), it is useless.