Embedded Systems and Power Electronics

Total Pageviews

About Me

My photo
I am currently a PhD student at UC Berkeley, following a 6-year journey working at Apple after my undergrad years at Cornell University. I grew up in Dhaka, Bangladesh where my interest in electronics was cultivated, resulting in the creation of this blog.

BTemplates.com

Powered by Blogger.

Oct 24, 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: http://www.mikroe.com/download/eng/documents/compilers/mikroc/pro/pic/help/adc_library.htm

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:
http://tahmidmc.blogspot.com/2012/03/modalities-of-using-adc-module-of.html

For LCD interfacing, I used the mikroC LCD library. You can view the library file here: http://www.mikroe.com/download/eng/documents/compilers/mikroc/pro/pic/help/lcd_library.htm


Here is the code for PIC16F877A:
(You can download the source file from: https://rapidshare.com/files/3044512089/LM35PIC16F877A.c)
--------------------------------------------------------------------------------------------------------------
//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_Init();
     LCD_Cmd(_LCD_CURSOR_OFF);
     LCD_Cmd(_LCD_CLEAR);
     LCD_Out(1, 1, "Temp:");
     //Display = "+125 'C";
     Display[4] = 39; //'
     Display[5]= 'C';
     ADCON1 = 0x0E;
     ADC_Init();
     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: www.ti.com/lit/ds/symlink/lm35.pdf
PIC16F877A datasheet: ww1.microchip.com/downloads/en/devicedoc/39582b.pdf
Modalities of Using the ADC module of the PIC16F877A: http://tahmidmc.blogspot.com/2012/03/modalities-of-using-adc-module-of.html
mikroC LCD library: http://www.mikroe.com/download/eng/documents/compilers/mikroc/pro/pic/help/lcd_library.htm
mikroC ADC library: http://www.mikroe.com/download/eng/documents/compilers/mikroc/pro/pic/help/adc_library.htm

18 comments:

  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

    #include
    #include
    __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;
    else
    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;
    }

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

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

      Delete
  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!

    ReplyDelete
    Replies
    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.

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

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

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

    ReplyDelete
  7. can you provide asm code for the same

    ReplyDelete
  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,

    Shri
    shrshr03@rediffmail.com

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

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

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

    ReplyDelete
  12. can you give me coding of this project

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

    ReplyDelete
  14. 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

    ReplyDelete
  15. this code does not work. show the error of missing libraries for ADC and LCD

    ReplyDelete