1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Rep Power

    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 iterations:(say 200) ");
    scanf("%d", &n); // It is assumed that n >= 1
    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);

    if (Rntc < ADC)
    goto foundT;
    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);

    return 0;
  2. #2
  3. Contributing User

    Join Date
    Aug 2011
    Rep Power
    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
    #Ohms   dC
    27335	 0 
    22146	 5 
    17941	10 
    14719	15
    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 and Makefiles!
  4. #3
  5. Contributing User

    Join Date
    Aug 2011
    Rep Power
    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
    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

       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
    [code]Code tags[/code] are essential for python code and Makefiles!
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Rep Power

    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
    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
  8. #5
  9. Contributing User

    Join Date
    Aug 2011
    Rep Power
    Use an interpolation routine based on your calibration points.

    gnu scientific library interpolation

    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 10:55 AM.
    [code]Code tags[/code] are essential for python code and Makefiles!
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Rep Power
    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
  12. #7
  13. Contributing User

    Join Date
    Aug 2011
    Rep Power
    See comments.
      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);
      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;
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo