C - log value - thermistor resistance to temperature conversion - Page 1
Forum section: electronics / general

author: arul
author_rank: Regular Contributor
timestamp: February 05, 2019, 05:49:20 am
content: Hi,I wrote a thermistor resistance value to temperature conversion program by the combination of PIC18F57K42 & MPLAB x IDE. Inside the conversion, I am using log calculation. I got a value using c compiler, but that value is not same as the calculator value. Here i attached one simple c code.Code:#include <stdio.h>#include <math.h>void main(){float Rth=29498.553;float Rr=50000;float a;a = log (Rth/Rr);printf("a = %f", a);}////////////////////a = - 0.527682 ////after compiling i got that value. But the calculator value is -0.22916929139I am also changing data type float to double, but i got only the same value. Please let me know, what is making wrong...I am using the following formula for conversion: Tkelvin = (beta * room_temp) / (beta + (room_temp * log (Rthermistor / Rroom_temp_res))) . Is it correct or else please suggest other than that one.Advance in thanks...!

author: IanB
author_rank: Super Contributor
timestamp: February 05, 2019, 05:54:49 am
content: When you do log(x) on a calculator you are getting log10(x). However, when you do log(x) in your C program you are getting loge(x), which would be ln(x) on a calculator. You need to decide which kind of logarithm you want.

author: arul
author_rank: Regular Contributor
timestamp: February 05, 2019, 06:36:55 am
content: Thankyou so much #IanB

author: mark03
author_rank: Frequent Contributor
timestamp: February 05, 2019, 03:56:50 pm
content: Also, if you only need single-precision floating point as shown (the "float" type, not "double"), use the library calls with an "f" suffix, like logf. That will save a conversion from float to double and back again, and the library call is presumably faster too.

author: georges80
author_rank: Frequent Contributor
timestamp: February 05, 2019, 04:14:03 pm
content: If you only need say 1C resolution and your thermistor is likely only 1% tolerance (or worse), a simple lookup table to convert from ADC reading to degree C is fast and compact (compared to dragging in a floating point library and log functions) especially with an 8 bit uC.cheers,george.

author: MarkF
author_rank: Super Contributor
timestamp: February 05, 2019, 04:41:34 pm
content: float logf (float f);andfloat log10f (float f);single precision log functions are NOT defined in the XC8 compilier.You must use the double precision versions:double log (double f); // natural logdouble log10 (double f); // base 10 log

author: mark03
author_rank: Frequent Contributor
timestamp: February 05, 2019, 04:44:43 pm
content: Quote from: MarkF on February 05, 2019, 04:41:34 pmWhat??Yet another reason to switch to ARM, or any other architecture really, that doesn't tie you to a single vendor's tools.

author: Nusa
author_rank: Super Contributor
timestamp: February 05, 2019, 05:55:47 pm
content: I second the recommendation to use a lookup table, with whatever precision (number of data points) you require for your application. Interpolation between data points gets you a good enough result.

author: langwadt
author_rank: Super Contributor
timestamp: February 05, 2019, 06:24:39 pm
content: Quote from: mark03 on February 05, 2019, 03:56:50 pmon AVR it doesn't matter they are the same, 32bit single precision