Saturday, June 18, 2011

Temperature sensor with PIC18 and MCP9700

 This can be a complete project on its own - a simple DIY digital thermometer with LCD display and only a handful of parts - the PIC18F45K20, LM35 and a small number of resistors and capacitors running off a regulated 5v supply.

Display type - LCD (can be 16x1, 16x2 or anything larger)
Controller: PIC18F45K20
Programming Language: BASIC
Compiler: mikroBASIC PRO for PIC v3.20

'Programmer: Syed Tahmid Mahbub
'Compiler: mikroBASIC PRO for PIC v3.20
'Target PIC: 18F45K20
'Configuration: Check the project in the download link for the configuration bits

program MC9700_Temp

dim LCD_RS as sbit at RC4_bit
    LCD_EN as sbit at RC5_bit
    LCD_D4 as sbit at RC0_bit
    LCD_D5 as sbit at RC1_bit
    LCD_D6 as sbit at RC2_bit
    LCD_D7 as sbit at RC3_bit

    LCD_RS_Direction as sbit at TRISC4_bit
    LCD_EN_Direction as sbit at TRISC5_bit
    LCD_D4_Direction as sbit at TRISC0_bit
    LCD_D5_Direction as sbit at TRISC1_bit
    LCD_D6_Direction as sbit at TRISC2_bit
    LCD_D7_Direction as sbit at TRISC3_bit

dim ADRead as word
dim Temp as longword
dim vDisp as word[3]
dim Display as string[7]

     TRISA = $FF
     TRISC = 0
     PORTC = 0
     LCD_Out(1, 1, "Temp:")
     Display = "+125 'C"
     while true
           ADRead = ADC_Read(0)
           if (ADRead > 102) then 'If temperature is positive
              Temp = (100 * ( (10 * ADRead) - 1024 ) ) >> 11 'Get temperature
              Display[0] = "+"
           else 'If temperature is negative
              Temp = ( (1024 - (10 * ADRead) ) * 100 ) >> 11 'Get temperature
              Display[0] = "-"
           end if
           vDisp[0] = Temp div 100
           vDisp[1] = (Temp div 10) mod 10
           vDisp[2] = Temp mod 10
           Display[1] = vDisp[0] + 48
           Display[2] = vDisp[1] + 48
           Display[3] = vDisp[2] + 48
           LCD_Out(1, 8, Display) 'Show temperature


Hex file + Schematic + Code freely available for download:

The code can be freely used and there is no copyright restrictions or anything as such. Only credit the author (me) where necessary.


  1. hi dear its very nice and very helpful post for those who are new and thinking to start a blog site.Your posts are very helpful and creative. I have atlast opened a blog site being inspired from your articles clik here and see about inverter

  2. hello sir please provide assembly code for lm35 with pic.