News:

PROTON pic BASIC Compilers for PIC, PIC24, dsPIC33

Main Menu

NTC Lookup table

Started by TimB, Dec 03, 2025, 11:37 AM

Previous topic - Next topic

TimB


Hi All

In the past Les posted a NTC Steinhart-Hart calculation. It was on the old forum so cannot find it now

I want to use it to generate a NTC lookup table 10bit ADC to Temp

Also how was the NTC wired 5v-NTC-Pic-10k-0v or 5v-10k-pic-ntc-0v

I have tried se code below but the results I get back are not right

    DIm cR1 as 9350.0           '9980 //voltage divider resistor value
    Dim cBeta as 3950.0         '3950 Beta value
    Dim cTo as 298.15           'Temperature in Kelvin for 25 degree Celsius
    Dim cRo as 10000.0          'Resistance of Thermistor at 25 degree Celsius

    Dim cadcMax as 1023              'ADC resolution 10-bit (0-1023)
    Dim cVs as 5.0                        'supply voltage
    DIm cVstep as cVs / cadcMax


    Proc getNTCTemp(), float
        DIm fVout as float = 0
        DIm fRt as float = 0
        DIm fT as float = 0
        Dim fTc as float = 0
        DIm wTemp as word
        dim ftemp  as float



        wTemp = Adin 0                          ' get ADC voltage on thermistor pin

        fVout =  wTemp * cVstep                 ' Work out the ADC to Voltage

        fRt = cR1 * fVout / (cVs - fVout)

        fT = 1/(1/cTo + log(fRt/cRo)/cBeta)     'Temperature in Kelvin
        fTc = fT - 273.15                       'Celsius

        result = fTc

    EndProc



david

#2
Murata has some good simulation tools for NTCs-
https://ds.murata.com/simsurfing/ntcthermistor.html?lcid=en-us&md5=7d4199c07c04f9e030eed504e1119f62
They also publish full temperature-resistance tables for Min, Typ and Max devices which will ultimately set your accuracy unless you elect to calibrate each device..  The simulation tool calculates the optimum fixed resistance in the divider for a given temperature span and often you can get by with a very close linear approximation. 
Select the circuit you want (typically NTC high side), the device you're using and the temperature range required.  It will calculate the R1 value for optimum results.

Maxi

Tim, you can find Albertos code here
its working well i think

https://protoncompiler.com/index.php/topic,1602.0.html

TimB




I have working code now

Send the ADC value and it will return the temperature


    DIm cR1 as 10000                            'voltage divider resistor value
    Dim cBeta as 3950.0                         '3950 Beta value
    Dim cTo as 298.15                           'Temperature in Kelvin for 25 degree Celsius
    Dim cRo as 10000.0                          'Resistance of Thermistor at 25 degree Celsius

    Dim cadcMax as 1023                         'ADC resolution 10-bit (0-1023)
    Dim cVs as 5.0                              'supply voltage
    DIm cVstep as cVs / cadcMax                 'Voltage per ADC step


    ' Wiring is 5v--\/\/\/\---Pic----NTC---- Grnd
    '                 10k            10K


    Proc getNTCTemp(pADC as word), float
        DIm fVout as float = 0
        DIm fRt as float = 0
        DIm fT as float = 0
        Dim fTc as float = 0
        dim ftemp  as float

        fVout =  pADC * cVstep                  ' Work out the ADC to Voltage

        fRt = cR1 * fVout / (cVs - fVout)       ' Then the resistance

        ftemp = log(fRt/cRo)               ' Do the maths
        ftemp = ftemp / cBeta
        ftemp = ftemp + 0.00335401643
        ftemp = 1/ftemp

        fTc = ftemp - 273.15


        result = fTc

    EndProc