cancel
Showing results for 
Search instead for 
Did you mean: 

Code Examples

MotooTanaka
Esteemed Contributor

This time, actually a while ago, I came across a chance to make a sample project of thermistor.

IMG_2503.JPG

Although the circuit seemed to be easy, the calculation was not as easy as I was expecting.tempSensor.JPG

According to the web page of the thermistor, we can calculate the resistance of  a thermistor with following equation.

equ1.JPG

Rt : Resistance at temperature t (kelvin)

R0: Resistance at T0

B: Thermistor related constant. For 103AT-11 it is 3435 +/- 1%

Conversion from Tk (Kelvin) to Tc (Celsius) is Tc = Tk - 273.15

Dividing both sides by R0, we get

equ2.JPG

Replacing e to log

equ3.JPG

Dividing both sides by B

equ4.JPG

From the Datasheet the resistance value of this thermistor is 10.0K at 25 degree Celsius,

so if we replace R0 to R25 and T0 to T25 (Klevin for 25c) the equation  is now

equ5.JPG

Moving 1/T25 from the right side to the left side

equ6.JPG

Reducing the left side

equ7.JPG

Then reforming the equation according to T

equ8.JPG

Finally we get

equ9.JPG

In the figure below

Rt = R1 x Vt / (VDDA -Vt)

circuit1.JPG

If we normalize the ADC value (ADin)  of input voltage 0V ~ VDDA V to 0.0 ~ 1.0

Rt = R1 x ADin / (1 - ADin)

Now we can write a code

Rt = R1 * ADin / (1 - ADin) ;

K = 1.0 / (log(Rt / R25) / B + 1.0/T25) ;

T = K - 273.15 ;

To make the long story short, here is the whole enchilada!

(aka, project attached...)

moto

8 Replies
Vasanth
Moderator
Moderator

Hello Tanaka san,

Thanks for your contribution to the community.

Best Regards,
VSRS

0 Likes
MotooTanaka
Esteemed Contributor

本サンプルは CQ出版 インターフェース 9月号に

-50 ~ +105℃で使える高精度サーミスタ

として掲載されました。

I contributed this sample project to a Japanese

technical mazagine "Interface" Sep. edition.

Interface2018年9月号 目次|Interface

moto

0 Likes
odissey1
Honored Contributor II

Motoo,

You can speed-up temperature calculation using Fast Thermistor Calculator component, which takes about 15 times less CPU clocks than traditional calculations.

Fast Thermistor Calculator

/odissey1

0 Likes
MotooTanaka
Esteemed Contributor

Dear /odissey1-san,


Thank you very much for your information!

I studied your code a little, and noticed that there are two differences.

(1) You (and Cypress Component) use approximation between 0 ~ 50 C degree,

    beside the range of 103AT-11 is -50 ~ +105 C degree and I used the maker's formula.

    (In general 0 ~ 50 should be enough though)

(2) The biggest contribution to the speed is usage of "fastlog()" which does not use "log()"

About (1),

It requires 3 sampled values, such as @5C, @25C, @45C,

and the error out side of 0~50 could be large (Sorry I was too lazy to calculate them, yet).

About (2), if "fastlog()" can speed up the calculation, may be replacing "log()" in my

formula also provide speed boost similar to your estimation and it also discard the

requirement for "-lm" option, which is very nice.

BTW, the Thermistor Calculator's URL has been changed and it is now

https://www.thinksrs.com/downloads/programs/therm%20calc/ntccalibrator/ntccalculator.html

Attached is my project using "fastlog()".

Around 25~26 C degree, the value seems to be OK.

Best Regards,

25-Jul-2018

Motoo Tanaka

0 Likes
odissey1
Honored Contributor II

Motoo,

Thank you for updated link and updated project.

/odissey1

0 Likes
Doorknob
Contributor

Thank you all so much for your support and contributions ... I will try and apply your examples and report back.

Thanks

Scott

0 Likes
Doorknob
Contributor

Motoot thank you for your example code ... i tried to use it in my application using CY8C4024LQI-S402 but i am stuck on how i use the API to get just the ADC count only.  There is some simple API for getting readings in mVolts.  I believe I am supposed to use the ADC_StartConvert(uint8 chld), but I am not sure, how to make sure the ADC has completed it measurement using the ADC_IsBusy(void) API.  Can i get some help on how to do this properly using this single slope 10-bit ADC for this PSoC 4000S.

float measure(void)

{

    int16_t adc_counts ;

    float value ;

    ADC_StartConvert(ADC_CH) ;

   ADC_IsBusy() ;

    ///??? adc_counts = ?????

    value = (float)adc_counts / (float)ADC_MAX ;

    return( value ) ;

Below it the available API, I am confused as to how to actually get the count and not the voltage.  I was successful in getting the ReadResult_mVolts to function and give me the correct voltage, but i need Count.

• cystatus ADC_StartConvert(uint8 chId)

Initializes the hardware and initiates an analog-to-digital conversion on the selected input channel.

• uint8 ADC_IsBusy(void)

The function returns the status of the ADC's operation.

• uint16 ADC_ReadResult_mVolts(uint8 chId)

This is a blocking API. It initiates a conversion, waits for completion and returns the result.

• uint16 ADC_GetResult_mVolts(uint8 chId)

This API does not perform an ADC conversion and returns the last valid result for the specified channel.

Thanks again for the help!!

Scott

0 Likes
MotooTanaka
Esteemed Contributor

Dear Scott-san,

I did not know that CY8C4024LQI-S402 does not have Sequencing SAR ADC!

I tried to to use 10-bit ADC, which is probably same one with yours on my CY8CKIT-149.

IMG_3662.JPG

I hope this will work with your system, if you change the device and pins. (Fingers crossed)

Note: As my board is using 5V as VDD/VDDA, if your system uses 3.3V,

please change VDDAMV to 3300.0 or something like that.

(I suggest you to measure the voltage and use real value * 1000.0 to make it mV)

schematic

101-schematic.JPG

main.c

=============================

#include "project.h"

#include "stdio.h"

#include "math.h"

/* value for 103AT-11 */

#define R1          10.0

#define R25C        10.0

#define B           3435

#define ABS_ZERO    273.15

#define T25C        298.15

#define VDDAMV      5000.0 /* use 3300.0 for 3.3V system */

volatile uint16 ADC_CSD_result;

char str[32] ;

void print(char *str)

{

    UART_UartPutString(str) ;

}

float mv2temp(int16_t mv)

{

    float result ;

    result = (float)mv / 1000.0 ; /* just for test */

    return( result ) ;

}

float get_temp(int16_t mv)

{

    float adc_in ;

    float Rt ;

    float Tk, Tc ;

    adc_in = (float)mv / VDDAMV ;

    Rt = R1 * adc_in / (1.0 - adc_in) ;

    Tk = 1.0 / (log(Rt / R25C) / B + 1.0/T25C) ;

    Tc = Tk - ABS_ZERO ;

    return(Tc) ;

}

void splash(void)

{

    sprintf(str, "test 103AT-11 (%s %s)\n", __DATE__, __TIME__) ;

    print(str) ;

}

int main(void)

{

    uint16_t mv ;

    float temp = 0.0 ;

   

    CyGlobalIntEnable; /* Enable global interrupts. */

    ADC_CSD_Start();

   

    UART_Start() ;

   

    splash() ;

   

    for(;;)

    {

        while(ADC_CSD_Calibrate());

        mv = ADC_CSD_ReadResult_mVolts(0);

        temp = get_temp(mv) ;

        sprintf(str, "%d.%03d\n", (int)temp, (int)(temp * 1000) % 1000) ;

        print(str) ;

        CyDelay(1000);

    }

}

=============================

Tera Term log

100-TeraTerm-log.JPG

moto

0 Likes