Go Down

### Topic: Using polynomial equation to determine thermocouple temperature (Read 1 time)previous topic - next topic

#### rodeg ##### May 07, 2015, 11:30 pm
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 + c1*E + d2*E^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();

Code: [Select]
`  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:

Code: [Select]
`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!

#### el_supremo #1
##### May 07, 2015, 11:45 pm
Is the input supposed to be in volts? If so you should use:
Code: [Select]
`float mv_sns = .012; // input voltage`

Pete
Don't send me technical questions via Private Message.

#### rodeg #2
##### May 07, 2015, 11:48 pm
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.

#### tmd3 #3
##### May 07, 2015, 11:56 pm
I have no expertise in this matter.  But, every time I've seen this notation,
Quote
Temp= c0 + c1*E + d2*E^2 + ... + cn*E^n,
it has meant this
Quote
c0 + c1*E + d2*(E^2) + ... + cn*(E^n)
rather than this
Quote
c0 + c1*E + (d2*E)^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?

#### el_supremo #4
##### May 07, 2015, 11:59 pm
Found it, but tmd3 just beat me to it :-)

I rewrote your code to use an array for the coefficients and input using mV which gives the expected result:
Code: [Select]
`void setup(){  Serial.begin(9600);  // put your setup code here, to run once:float mv_sns = 12; // input voltage  // polynomial coefficients:float poly = {  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
Don't send me technical questions via Private Message.

#### rodeg #5
##### May 08, 2015, 12:06 amLast Edit: May 08, 2015, 12:06 am by rodeg
Yep , I confirm it works. Thank you all for your time ! #### michinyon #6
##### May 08, 2015, 02:12 am
Don't use pow( ),  it is useless.

Go Up