News:

;) This forum is the property of Proton software developers

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

top204

Crickey.... It was some years ago that I wrote that code.

Where has the time gone?

Fanie

Don't worry about getting old.
We are still going to do stupid things.
Just slower.

okmn

Quote from: tnencon on Jan 05, 2023, 08:33 AM''*********************************************************
'x = The ratio of the value of NTC at the measured temperature to the NTC at 25 degrees
'Result= ln(x)
'ln (x)= 2 [ (x-1)/(x+1)  + (1/3)( (x-1)/(x+1) )^3 + (1/5) ( (x-1)/(x+1) )^5 + (1/7) ( (x-1)/(x+1) )^7 + ... ]    (x > 0) 
''*********************************************************
Proc ln_x(x As Float),Float
    If x = 0 Then
      Result = 0
      ExitProc
    EndIf
    sum = 1
    Value = 1
    y = (x-1)/(x+1)
    For i = 3 To 1000 Step 2
        Value = Value *(y*y)
        sum = sum + Value/i
    Next i
    Result = 2*y*sum
   
EndProc


Quote from: top204 on Jan 05, 2023, 11:49 AMI tried the new Ln procedure from tnencon and it works well. See below:

'---------------------------------------------------------------------------------   
' Floating Point Ln procedure
' Input     : pValue hold the value to calculate the Ln from
' Output    : Returns the Ln value
' Notes     : ln (pValue)= 2 [(pValue-1)/(pValue+1) + (1/3)( (pValue-1)/(pValue+1))^3 + (1/5) ((pValue-1)/(pValue+1))^5 + (1/7) ((pValue-1)/(pValue+1))^7 + ...] (pValue > 0)
'
Proc Ln_x(pValue As Float), Float
    Dim wLoop As Word
    Dim fTemp As Float
    Dim fSum As Float
    Dim fValue As Float
   
    If pValue.Byte0 = 0 Then
       Result = 0
       ExitProc
    EndIf
   
    fSum = 1
    fValue = 1
    fTemp = (pValue - 1) / (pValue + 1)
    For wLoop = 3 To 1000 Step 2
        fValue = fValue * (fTemp * fTemp)
        fSum = fSum + (fValue / wLoop)
    Next
    Result = (2 * fTemp) * fSum 
EndProc

However, because of the large loop, it takes a lot more time to perform the calculations and produces the same values as my original Ln procedure. For example, the new Ln_x procedure takes 155ms to operate on a 16MHz 18F device, while my original Ln procedure takes 3.56ms on the same device operating at the same speed. :-)

But many thanks for posting it.


https://protoncompiler.com/index.php/topic,1448.msg11320.html#quickreply_anchor