Thursday, October 25, 2012

Temperature Sensor (MCP9700 + PIC16F877A)

Here's a temperature sensor (thermometer) circuit that you can easily build. It uses the popular PIC 16F877A microcontroller. The temperature sensor is MCP9700. The MCP9700 outputs an analog voltage corresponding to the temperature. The PIC reads the analog voltage, processes it and displays temperature on the LCD. The temperature range of this circuit is -40'C to +125'C.

The methods of processing the output of the MCP9700 can be found in the datasheet: ww1.microchip.com/downloads/en/devicedoc/21942a.pdf 

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 the PIC16F877A:

(You can download the source file from: https://rapidshare.com/files/4167495939/MC9700_Interface.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;



void main() {
     unsigned long int Temp;
     unsigned int ADRead;
     unsigned int vDisp[3];
     unsigned char Display[7];
     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);
           if (ADRead > 102){// '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] = '-';
           }
           vDisp[0] = Temp / 100;
           vDisp[1] = (Temp / 10) % 10;
           vDisp[2] = Temp % 10;
           Display[1] = vDisp[0] + 48;
           Display[2] = vDisp[1] + 48;
           Display[3] = vDisp[2] + 48;
           LCD_Out(1, 8, Display); // 'Show temperature
     }
}
 --------------------------------------------------------------------------------------------------------------

Reference documents:

MCP9700 datasheet: ww1.microchip.com/downloads/en/devicedoc/21942a.pdf
PIC16F877A datasheet: ww1.microchip.com/downloads/en/devicedoc/39582b.pdf
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
Modalities of Using the ADC module of the PIC 16F877A: http://tahmidmc.blogspot.com/2012/03/modalities-of-using-adc-module-of.html

4 comments:

  1. This is very good, can you email me the proteus simulation file?

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

    ReplyDelete
  3. All thanks to Mr Anderson for helping with my profits and making my fifth withdrawal possible. I'm here to share an amazing life changing opportunity with you. its called Bitcoin / Forex trading options. it is a highly lucrative business which can earn you as much as $2,570 in a week from an initial investment of just $200. I am living proof of this great business opportunity. If anyone is interested in trading on bitcoin or any cryptocurrency and want a successful trade without losing notify Mr Anderson now.Whatsapp: (+447883246472)
    Email: tdameritrade077@gmail.com

    ReplyDelete
  4. Çdo problem ka një zgjidhje kur takoni personin e duhur! Mos kini frikë se ekziston një magjistar i shkëlqyer dhe zgjidhës shpirtëror i problemeve që është në gjendje t'i japë fund problemit tuaj. Dua te vleresoj Dr Fady qe me riktheu martesen time me magjine e saj, Pas 1 viti ndarje me burrin tim, me ndihmen e magjistares se mrekullueshme Dr Fady burri im erdhi ne shtepi dhe tani jemi te lumtur bashke per mire, edhe nje here faleminderit per Dr Fady gjithashtu shëroi të gjitha llojet e sëmundjeve si p.sh.

    1 DEKLARATË DASHURI
    2 WIN S EX KTHIMI
    3 FRUTA GOMS
    4 NJOFTIM PËR PROMOCION
    5 DEKLARATA E MBROJTJES.
    6 MAGJIA E BIZNESIT.
    7 DEKLARATË E MIRË PUNËS.
    8 HIV SIDA.
    9 NDALO MISKARRIMIN.
    10 MIROOD NJOFTIM SHPALLJE.
    11 SHKAK I TELEKINEZËS.
    DREJTSHKRIMI I LOTARIVE DHE ÇËSHTJA GJYQËSORE Kontakt Kontaktoni atë për ndihmën tuaj nëpërmjet: drfady720@gmail.com
    WHATSAPP:+2349039422406

    ReplyDelete