Thursday, October 25, 2012

Temperature Sensor (LM35 + PIC16F877A)

Here's one temperature sensor (thermometer) circuit that you can easily build. It uses the popular PIC 16F877A microcontroller. The temperature sensor is LM35. The LM35 outputs an analog voltage proportional to the temperature. The output from the LM35 is 0.1V/'C. So, when temperature sensed is 61'C, the output voltage is 0.61V. This analog voltage is read by the PIC and processed to display the corresponding temperature value on the LCD.

The temperature range for this circuit is 0'C to 150'C.

The analog to digital conversion is done by the PIC ADC module. In the code, I've used the mikroC library function for ADC. You can view the library file here:

However, you should have a knowledge of how the ADC module works and how to use it. I had written a tutorial on modalities of operation of the PIC 16F877A ADC. You can find the tutorial here:

For LCD interfacing, I used the mikroC LCD library. You can view the library file here:

Here is the code for PIC16F877A:
(You can download the source file from:
//Programmer: Syed Tahmid Mahbub
//Compiler: mikroC PRO for PIC v4.60
//Target PIC: PIC16F877A
sbit LCD_RS at RB4_bit;
sbit LCD_EN at RB5_bit;
sbit LCD_D4 at RB0_bit;
sbit LCD_D5 at RB1_bit;
sbit LCD_D6 at RB2_bit;
sbit LCD_D7 at RB3_bit;

sbit LCD_RS_Direction at TRISB4_bit;
sbit LCD_EN_Direction at TRISB5_bit;
sbit LCD_D4_Direction at TRISB0_bit;
sbit LCD_D5_Direction at TRISB1_bit;
sbit LCD_D6_Direction at TRISB2_bit;
sbit LCD_D7_Direction at TRISB3_bit;

unsigned long ADRead;
unsigned int vDisp[3];
unsigned char Display[7];
void main() {

     PORTA = 0;
     TRISA = 0X01;
     PORTB = 0;
     TRISB = 0;
     LCD_Out(1, 1, "Temp:");
     //Display = "+125 'C";
     Display[4] = 39; //'
     Display[5]= 'C';
     ADCON1 = 0x0E;
     while (1){
           ADRead = (ADC_Get_Sample(0) * 500) >> 10;
           vDisp[0] = ADRead / 100;
           vDisp[1] = (ADRead / 10) % 10;
           vDisp[2] = ADRead % 10;
           Display[1] = vDisp[0] + 48;
           Display[2] = vDisp[1] + 48;
           Display[3] = vDisp[2] + 48;
           LCD_Chr(1, 8, Display[0]);
           LCD_Chr(1, 9, Display[1]);
           LCD_Chr(1, 10, Display[2]);
           LCD_Chr(1, 11, Display[3]);
           LCD_Chr(1, 12, Display[4]);
           LCD_Chr(1, 13, Display[5]);
           //LCD_Out(1, 8, ); // 'Show temperature
           delay_ms(200); //200ms delay for waiting

Reference documents:
LM35 datasheet:
PIC16F877A datasheet:
Modalities of Using the ADC module of the PIC16F877A:
mikroC LCD library:
mikroC ADC library:


  1. Hi. Would you please review my code here.?? I want to use LM35 sensor with PIC16f877a to make a system with below requirements.

    temperature >25celsius = only led1 blink
    temperature <20celsius = only led2 blink
    else, neither blink.

    I used MPLAB Hi Tech as compiler. But, sadly it isn't working. Can you please advice what should I corrected?? Thank you very much

    __CONFIG(0x3F32); // PIC16F77A Configuration Fuses

    #define ADC_value
    #define ADC_init
    #define LED1 RB2
    #define LED2 RB3

    ADC_init ();

    void main ()
    TRISA = 0b00000001; //set pin 0 at portA as input
    TRISB = 0b00000000; //set portB as output
    PORTB = 0; //clear portB

    while (1); //loop forever
    unsigned int ADC_value = 0;
    unsigned int temp = 0;

    ADON = 1;
    ADC_value = read_adc (0); //read analog inputs from sensor at channel 0
    temp = (ADC_value/2); //convert temp in Volt to Celcius

    if (temp < 20);
    LED1 = 1;
    LED2 = 0;
    else if (temp > 25);
    LED2 = 1;
    LED1 = 0;
    LED1 = 0;
    LED2 = 0;
    __delay_ms (100);

    void ADC_init (void)
    *// ADC Initialization//*
    ADCON0 =0b10000001; //set ADC FOSC/32, channel 0 for input AN0, ADC on
    ADCON1 =0b10001110; //right justified for 10-bits, analog input for pin AN0-AN3

    unsigned int read_adc (void)
    unsigned int digit = 0;
    __delay_ms (1);

    GODONE = 1;
    while (GODONE==1);
    digit = (unsigned int)ADRESH<<8;
    digit = digit + ADRESL;
    return digit;

  2. hi what LCD OR versión do you use of proteus? i can't run the program :/

    1. I just used a simple 16x1 LCD. What problem are you running into?

  3. Hi there! Can you send me the complete schematic diagram of this project?I'm using sourceboost IDE C but I need to program this using assembly can someone provide me an assembly code for this project?

    Help would be much appreciated. Thanks!

    1. The schematic is shown here. You just need to supply the microcontroller VDD and VSS lines from your input supply (5V). That's the only thing not shown here.

  4. Display[4] = 39; //'
    Display[5]= 'C';
    ADCON1 = 0x0E;
    while (1){
    ADRead = (ADC_Get_Sample(0) * 500) >> 10;

  5. i want to understand how this line work in the programme

  6. What's really mean TRIS, ADCON, INTCON... thanks

  7. can you provide asm code for the same

  8. Dear Sir,

    I am Shri M and Pursuing B.E final year. I came across your site and it's really to say that your web site is amazing which provides new trends techno project ideas. So, I am doing project on the title "Temperature control in PROTEUS using ADC with motor attached to it". Its like when temperature is high then RPM of motor should be more and vise versa. In addition to that I needed to add any feature by adding TRANSISTOR. But I am not getting sir. Please help me out this. I saw your all projects So, your one of the expert.

    Please send me the design of such project and its C-code. I am using 8051 or 8951. Please send me ADC-Motor-LCD-TRANSISTOR-Any other feature. Please send sir and your help will be like star for me. Really I needed your help.

    Thanking You,


  9. can u plz upload the souce file again/....we cant download from the given link

  10. who have full coding this project? can give it to me plzz

  11. what i must change if i connect lcd at port RD

  12. can you give me coding of this project

  13. hi dear its blog very nice and helpful thanks from inverter

  14. how can program if temperature is greater or equal to 40 it prints abnomal

  15. Mr tahmid pls help me as i am new to micro c ,what additional coding did i should do if

    Temperature is more than 38 solenoid valve on
    Temperature less than 38 solenoid off