C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old October 25th, 2012, 06:09 PM
fredf fredf is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 4 fredf User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 11 m 40 sec
Reputation Power: 0
Prg calculate T from Rntc

Please read the comments:
/* Find the NTC temperature when Rntc is measured*/

// R in ohms T value at 0,5,10,15 in บC
//R0 = 27335; R5 =22146 ; R10 = 17941 ; R15 =14719
/*********************ffm Oct 2012********************************/

/* You well may ask
....having equation Rntc = 19.67*T*T-1136.15*T+27335.25....
Why not use the inverse equation T = f(Rntc) ?
This f(Rntc) turns out to be a monster with cubic terms. No No.

My question:- T value last iteration and T value in foundT: often differ by 0.1 degree
Any good guru got an answer? Please.
*/
#include <stdio.h>

int main()
{
float Rntc,T=-4;
int n, c = 1,ADC;
printf("Introduce number of loop iterationssay 200) ");
scanf("%d", &n); // It is assumed that n >= 1
printf("\n");
printf("Introduce an ADC o/p value for Rntc\n ");
printf("( Valid values range 14500 to 28000 ) ");
scanf("%d", &ADC);

print: // label
Rntc = 19.67*T*T-1136.15*T+27335.25 ;
printf("R is %.0f\n",Rntc);
printf("T is %.1f\n",T);
printf(" c is %d\n",c);
//Rntc==R
T=T+0.1;

if (Rntc < ADC)
goto foundT;
c++;
if (c <= n)
goto print;


foundT: printf("-----------------------\n");
printf("ADC o/p is Rntc -> %.0f ohms\n",Rntc);
printf("Giving T as %.1f degrees C\n",T);
printf(" c is %d\n",c);
getch();

return 0;
}

Reply With Quote
  #2  
Old October 25th, 2012, 09:41 PM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,460 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 4 Days 6 h 56 m 42 sec
Reputation Power: 403
You have a thermistor and you want to interpolate temperature given the resistance which you measure. Plotting this data I see that it looks fairly linear but slightly concave upward. Call this file a
Code:
#Ohms   dC
27335	 0 
22146	 5 
17941	10 
14719	15 

Code:
gnuplot> plot 'a'u 2:1w l,19.67*x*x-1136.15*x+27335.25

This quadratic looks really good.


So why don't you just find the roots of the quadratic equation?

Rntc = 19.67*T*T-1136.15*T+27335.25

Example, you measure 20000 ohms. What is the temperature?

20000 = 19.67*T*T-1136.15*T+27335.25

Subtract 20000 from each side

0 = 19.67*T*T-1136.15*T+7335.25

Use quadratic equation to find the temperatures at which this is true.

T = (-b +/- Sqrt[b^2 - 4 a c])/(2 a)

a is 19.67
b is -1136.15
c is 7335.25

Or we simply feed
Solve[0 == 19.67*x*x-1136.15*x+7335.25,x]
into Wolfram alpha.

giving 7.4058 or 50.355 degrees.
__________________
[code]Code tags[/code] are essential for python code!

Reply With Quote
  #3  
Old October 25th, 2012, 09:59 PM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,460 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 4 Days 6 h 56 m 42 sec
Reputation Power: 403
Furthermore, "inverting a quadratic" won't give a cubic equation. What you (or whomever) did was to fit a cubic equation through the 4 points, which I didn't verify but know full well is almost certain to be absolutely stupid between the data points. That's why cubic splines do something else.

If, instead, we find a best fit quadratic equation that expresses temperature as a function of resistance, we come up with
Code:
T = 45.5979 - 0.00256762 * R + 3.29445e-8 * R * R

When I evaluate this at 20000 Ohms I get 7.42 degrees C which varies from the result of my previous post by 2 hundredths of a degree.

Here's the work in executable Iverson notation

Code:
   R=: 27335 22146 17941 14719  NB. resistances
   T=: 0 5 10 15                NB. corresponding temperature

   T%.R^/i.3                    NB. best fit quadratic coefficients
45.5979 _0.00256762 3.29445e_8
   


   f=:(T%.R^/i.3)&p.            NB. verb f evaluates the polynomial

   f 20000                      NB. f 20000    Ohms gives 7.42331 dC
7.42331

Reply With Quote
  #4  
Old October 26th, 2012, 02:51 AM
fredf fredf is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 4 fredf User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 11 m 40 sec
Reputation Power: 0
Prg calculates T from Rntc

Many thanks amigo b49P23TIvg for your time spent in giving such an in depth reply.

Maths of NTC thermistors
As you know much better than I do the maths that come with relating
thermistor resistance to the temperature are a bit horrible.
The simplest relation in wiki , and many equivalent simplifications
could be...
Steinhart Hart simplified
1/T = 1/T0 +1/B ln (R/R0)
This is where my poor grasp of C + my poor grasp of all but the most basic maths gave me cold feet.

But life goes on .
The best R vs T fit I could find with the freeware prg graph, was exponential , and not good.

Yet, you can tame the curve by chopping it into small bits, say every 5 degrees.

Let me digress before returning to the bit wise approach.

The project could be to measure temperature in the range 0 to 100 degree C, with a resolution of 0.1 บC

Here we need something that depends on some invariable law of he universe, a pyrometer
based on the defunct Mr Plank’s Law: ( sic transit gloria)

Sadly Vishay only offers as its best NTC line and pricey 1%,
and even worse tables of T vs R only every 5degrees.
The data come with their 10k product NTCLE413-428 10K 1 % B3435 K.

Bit wise approach to following a curve.
Now we come to the crunch.
To interpolate with a certain amount of accuracy between 5 degree steps
a linear division might not even be accurate to one degree.

My approach to improve resolving the data points curve into something edible
was based on the pleasant surprise discovery .
That for small 15 degree parts of the curve, it was possible to fit a quadratic
equation.
Not the same equation, but I found that four ***equations 0 to 60 degrees
adjusted very well to the data supplied by Vishay.

Objective one was covered,
I could now relate R vs T with reasonable accuracy.
Accuracy? Well for me, 0.25 degree steps was / is just fine.

***
Well using a PIC I have a 10 bit ADC, which will let me resolve 1024 steps.
Sounds great, unfortunately here we don't want to go astray and confuse
Resolution with Precision..
Resolution being here 15degrees / 1024 = steps of 14.648e-3
Precision? who knows!

Getting back to the quadratic equations
As b49P23TIvg has pointed out , the formula invented by another
long forgotten gentleman in India has TWO solutions, two roots.

In the calculation for Rntc = 20k
we get 7.4058 or 50.355 degrees. ( I take your word )

I have a problem with this , not the precision, but rather how do I know
which is correct,? both fall in the 0 to 100 degree C range.

That is why I looked for a method where as T increases
the equation R = 19.67*T^2-1136.15*T+27335.25
stops its testing when it just passes 20000

Logically it will never go through the 50.355 value
as the equation is only valid, and the module dimensioned
for 0 to 15 degrees C.

In fact we can with a view to confirming, not bettering
the result, step T from 0 to 15 halting at T1,
and then step T from 15 to 0 halting at T2
We get a mean value of (T1 +T2) / 2

Another way of looking at the elimination of the 50.355 result
is that we are covering an input R range of 27335 to 14719.
So here the 50.355 value is not possible.

On the other hand, using the quadratic formula will save
a lot of processing time, so maybe some one can see a test
condition to find the right root :-)

My original question was why do I get two results
differing by 0.1.
I see this on the list when the module is run.

I'm not worried about precision ( 0.25 degree is fine )just
curious as to why the C routine shows these two results?

As I said in the beginning, C is not my strong point,
I normally program embedded micro controllers in Assembly

Bye and thanks
Fred
Madrid

Reply With Quote
  #5  
Old October 26th, 2012, 09:51 AM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,460 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 4 Days 6 h 56 m 42 sec
Reputation Power: 403
Use an interpolation routine based on your calibration points.

gnu scientific library interpolation
http://www.gnu.org/software/gsl/manual/html_node/Interpolation.html


Also, I didn't bother reading your program because there are library functions to find zeros. These are also in the gsl.

Last edited by b49P23TIvg : October 26th, 2012 at 09:55 AM.

Reply With Quote
  #6  
Old October 26th, 2012, 12:47 PM
fredf fredf is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 4 fredf User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 11 m 40 sec
Reputation Power: 0
บบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบ
Please, my question is very simple.
When I run the subroutine in the thread opener, and with this snippet we are approaching the final value for T

In the console I see
R is 17078
T is 11 .2
c is 153

R is 17008
T is 11 .3
c is 154

R is 16939
T is 11 .4
c is 155

ADC o/p is Rntc -> 16939 ohms
Giving T as 11.5 degrees C
c is 155


The R is the same, the C is the same
but T is both 11.4 and 11.5.

Q1 Can I ask as a newbie, what is happening in this
particular loop ?
Q2 which of the two values for T is correct?
Bye and thanks

Reply With Quote
  #7  
Old October 26th, 2012, 01:23 PM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,460 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 4 Days 6 h 56 m 42 sec
Reputation Power: 403
See comments.
Code:
 print:
  Rntc = 19.67*T*T-1136.15*T+27335.25 ; /* Here you compute Rntc */
  printf("R is %.0f\n",Rntc);
  printf("T is %.1f\n",T);
  printf(" c is %d\n",c);
  //Rntc==R
  T=T+0.1;			/* Here you change the value used to compute Rntc (violating the correspondence between T and Rntc) */

  if (Rntc < ADC)		/* Here you decide if Rntc is close enough */
    goto foundT;

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Prg calculate T from Rntc

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap