NTC thermistor temperature sensing


Closed Thread
Results 1 to 37 of 37

Hybrid View

  1. #1

    Default NTC thermistor temperature sensing

    Hello everyone,

    I use with my PIC a simple NTC thermistor in series with a 2.2k thermistor (to make a voltage divider), then I fed the ADC1 port.
    The ADC reading works well, with a 8-bit setting (values from 0 to 255 in function of temperature).

    Now I want to convert this number into degrees celcius, so I will use this function :

    ADCIN 1,adcVar
    ADCVoltage=adcVar/78 (255/3.3=78)
    temp=(ADCVoltage-0.8143)/0.0319

    But I'm aware that PBP don't handle float numbers. Here's my code :

    ' Set PORTA to all input
    TRISA = %11111111

    ' Set up ADCON1
    ADCON1 = %10000010

    define OSC 20
    DEFINE ADC_BITS 8 ' Set number of bits in result
    DEFINE ADC_CLOCK 3 ' Set clock source (rc = 3)
    DEFINE ADC_SAMPLEUS 10 ' Set sampling time in microseconds
    DEFINE LCD_DREG PORTC 'LCD data port
    DEFINE LCD_RSREG PORTC 'LCD data port
    DEFINE LCD_EREG PORTC
    DEFINE LCD_EBIT 5
    DEFINE LCD_BITS 4 'LCD bus size 4 or 8
    DEFINE LCD_LINES 2 'Number lines on LCD
    DEFINE LCD_COMMANDUS 10000'Command delay time in us
    DEFINE LCD_DATAUS 1000 'Data delay time in us

    adcVar VAR WORD
    adcVoltage VAR WORD
    temp VAR WORD

    Pause 4000
    Lcdout "Thermometer"
    pause 4000

    main:

    ADCIN 1,adcVar

    ADCVoltage=adcVar/78
    temp=(ADCVoltage-0.8143)/0.0319

    Pause 100
    lcdout $fe,1
    lcdout #adcvar
    lcdout $fe,$c0
    lcdout 1,#temp

    GoTo main
    I can read correctly ADC value on the LCD display (which changes as function of temperature).

    But I like to display on the LCD the temperature with a float number (XX.YY °C).

    But I don't know how to proceed. Can you arrange the code for me? Thanks in advance.

  2. #2
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default

    Dave
    Always wear safety glasses while programming.

  3. #3
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,648


    Did you find this post helpful? Yes | No

    Default

    Hi,

    IF you want to show 1/100 ° ...

    better finely tune your calculations @ first :

    255/ 3.3 = 77.272727 ... not 78 !!!

    PBP can handle ADRes * 2550 / 33 ... sooooooo ...

    add to that , here, a linear regression is not the best for a thermistor, and we conclude you will show " something " , but nothing that could be called a temperature.

    and I don't talk about the least digit meaning ...

    just my two cents ...

    Alain
    Last edited by Acetronics2; - 14th January 2011 at 08:30.
    ************************************************** ***********************
    Why insist on using 32 Bits when you're not even able to deal with the first 8 ones ??? ehhhhhh ...
    ************************************************** ***********************
    IF there is the word "Problem" in your question ...
    certainly the answer is " RTFM " or " RTFDataSheet " !!!
    *****************************************

  4. #4


    Did you find this post helpful? Yes | No

    Default

    Well, sorry for these newbie questions...

    The precision for my thermoter is not critical, is just a temperature sensor for security purposes. But I want to keep the code for current and voltage sensing purposes (linear curves), so in these cases I need more precision...

    I've read many exemples given... I've modified my code in consequence, I can read the integer part (like 23.00 °C), but not the float part...

    Here's my code :
    Code:
    define OSC 20
    DEFINE ADC_BITS 8      ' Set number of bits in result
    DEFINE ADC_CLOCK 3     ' Set clock source (rc = 3)
    DEFINE ADC_SAMPLEUS 10 ' Set sampling time in microseconds 
    DEFINE LCD_DREG PORTC 'LCD data port
    DEFINE LCD_RSREG PORTC 'LCD data port
    DEFINE LCD_EREG PORTC
    DEFINE LCD_EBIT 5 
    DEFINE LCD_BITS 4 'LCD bus size 4 or 8 
    DEFINE LCD_LINES 2 'Number lines on LCD 
    DEFINE LCD_COMMANDUS 10000'Command delay time in us 
    DEFINE LCD_DATAUS 1000 'Data delay time in us 
    
    AD_AN1_VALUE VAR WORD
    B0 VAR WORD
    B1 VAR WORD
    B2 VAR WORD
    B3 VAR WORD
    
    TRISA = %11111111     
    
    ADCON1 = %10000010   
    
    PAUSE 4000
    
    lp:
    
    ADCIN 1, AD_AN1_VALUE
    
    B0 = AD_AN1_VALUE*129   
    B1 = B0-8143                                      
    B2 = B1/319
    PAUSE 100
                          
    LCDOUT $fe,1
    LCDOUT #AD_AN1_VALUE
    Lcdout $fe,$c0,"Temp.: ",DEC (B2 DIG 0),DEC (B2 DIG 1),".",DEC (B2 DIG 2),DEC (B2 DIG 3),"°C"
    
    goto lp
    I like to have XX.Y °C (23.2 °C for example).
    I know that is a very common problem, but I've spend many hours to try solve this, so...

    Example of my calculation :
    AD_AN1_VALUE=137
    B0=137*129=17673
    B1=17673-8143=9530
    B2=9530/319=29,874 °C

    But here I only get 29,00 °C...
    Last edited by pxidr84; - 14th January 2011 at 17:56.

  5. #5
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default

    No problem with the newbie questions, just figured a search would get you there.

    Powers of ten...
    If you are using an 18F chip it is easy with LONG type variables.

    Make the 9530, 953000, mult by 100.
    then
    953000 / 319 = 2987
    then
    do the DIG.

    What chip are you using?
    Dave
    Always wear safety glasses while programming.

  6. #6
    Join Date
    Oct 2010
    Posts
    27


    Did you find this post helpful? Yes | No

    Default

    PBP can only handle integer, it clips off the .874 dec portion.

    where analog in is 0-1024 I would do something like this, tho your scaling would vary :

    Analog_In_1:
    ADCIN 1, AIN1
    Volts_1=AIN1*50/51/2
    Start_Seconds=AIN1*50/51/2
    HI_1 = Volts_1/100
    LOW_1 = Volts_1//100
    Lcdout $fe,$c0,"Temp.: ",#HI_1,".",#LOW_1,"°C"

    Return

    So work in values*100
    Divide by 100 for the integer
    get the modulus of the Divide by 100 for the floating portion
    if you support longs even better!
    note // is modulus
    I thought B0 was reserved for Byte zero, but I may be thinking of something else.
    Last edited by Plcguy; - 14th January 2011 at 19:42.

  7. #7
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,648


    Did you find this post helpful? Yes | No

    Default

    not much better ...

    Code:
     
    .
    .
    .
     
    AD_AN1_VALUE VAR WORD
    B0 VAR WORD
    B1 VAR WORD
    B2 VAR WORD
    B3 VAR WORD
    B3 = 100
    TRISA  = %11111111     
    .
    .
    .
     
    PAUSE 4000
    lp:
    ADCIN 1, AD_AN1_VALUE
    B0 = AD_AN1_VALUE*129   
    B1 = B0 - 8143
    B1 = B1 * B3                                      
    B2 = Div32 319
    PAUSE 100
    B0 = B2 / 100   ' we get integer part of temp
    B1 = B2 // 100  ' we get the decimal part x 100
     
    LCDOUT $fe,1
    LCDOUT #AD_AN1_VALUE
    Lcdout $fe,$c0,"Temp.: ",DEC (B0 DIG 1),DEC (B0 DIG 0),"." ,DEC (B1 DIG 1),DEC (B1 DIG 0),"°C"
    goto lp
    But with whisles and Bells ...

    Alain
    ************************************************** ***********************
    Why insist on using 32 Bits when you're not even able to deal with the first 8 ones ??? ehhhhhh ...
    ************************************************** ***********************
    IF there is the word "Problem" in your question ...
    certainly the answer is " RTFM " or " RTFDataSheet " !!!
    *****************************************

  8. #8
    Join Date
    Oct 2010
    Posts
    27


    Did you find this post helpful? Yes | No

    Default

    I'm not seeing what "DEC" and "dig" offers over my ver.....
    Please enlighten me!

  9. #9


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Acetronics View Post
    not much better ...

    Code:
     
    .
    .
    .
     
    AD_AN1_VALUE VAR WORD
    B0 VAR WORD
    B1 VAR WORD
    B2 VAR WORD
    B3 VAR WORD
    B3 = 100
    TRISA  = %11111111     
    .
    .
    .
     
    PAUSE 4000
    lp:
    ADCIN 1, AD_AN1_VALUE
    B0 = AD_AN1_VALUE*129   
    B1 = B0 - 8143
    B1 = B1 * B3                                      
    B2 = Div32 319
    PAUSE 100
    B0 = B2 / 100   ' we get integer part of temp
    B1 = B2 // 100  ' we get the decimal part x 100
     
    LCDOUT $fe,1
    LCDOUT #AD_AN1_VALUE
    Lcdout $fe,$c0,"Temp.: ",DEC (B0 DIG 1),DEC (B0 DIG 0),"." ,DEC (B1 DIG 1),DEC (B1 DIG 0),"°C"
    goto lp
    But with whisles and Bells ...

    Alain
    Thank you so much, it works flawlessly now. Very helpful.

    For mackrackit, I use the 18F26K20 (3.3V, 64MHz, 28-DIP), but I will switch to 18F4431 (because mine haven't enough I/O ports, and not enough hardware PWM ports).

Members who have read this thread : 0

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts