Temperature sensor using PIC16F877A microcontroller

Components:

  • PIC 16F887: This MCU is powerful and very easy-to-program. It is CMOS FLASH-based 8-bit microcontroller. It has 40- or 44-pin package. The PIC16F887 features 256 bytes of EEPROM data memory, self programming. Operating Voltage range is between 2v to 5.5v.It also contains 2 Comparators, 14 channels of 10-bit Analog-to-Digital (A/D) converter, 1 capture/compare/PWM and 1 Enhanced capture/compare/PWM functions. It is very easy to use with LCD and sensors.
  • Pickit 3 is required to program the PIC using MPLAB IPE.
  • LCD 16×2: LCD is used to show alpha numeric characters. It has total 16 pins. A 10K ohm potentiometer is connected with pin three to set the contrast of the LCD. It also contains a backlight LED. In this project LCD we use is 16×2 which means we can display 16 characters in two lines. In this project LCD is used to show whether the motion is detected or not.
  • LM 35: The LM35 is a inexpensive but precise temperature sensor. The range to LM35 to measure temperature lies between -50 to 150 Celsius. It  operates from 4 V to 30 V and consume less than 60-μA Current. It is small in size as compare to other temperature sensors. It is very useful for portable projects.

Others: Breadboard, jumper wires and 10k ohm potentiometer

There are many temperature sensors available in market. But LM35 temperature sensor is used in this project. It is cheapest in price and one can easily find it in market. There are many other advantages of LM35 like:

  • It is more efficient than thermistor
  • It is made up of integrated circuit hence no chance of damaging to internal circuitry.
  • It draws current only in micro Amperes.

Only 5 volt power supply is required for LM35 and there is no need of extra circuitry to operate it. PIC16F877A microcontroller is used to read temperature value. 16X2 LCD is used to display temperature value on LCD. Code of this project written in Mikro C compiler.

Implementation:

LM35 temperature sensor converts temperature into its proportional analog voltage value. LM35 is three terminal device.Pin number one and three are for 5-volt voltage supply. Pin two is analog voltage output with respect to temperature value.Relation between measured temperature and analog output voltage is:

                                                              1oC = 10m volt                  

Hence for every 1 degree increase in temperature there will be a increment of 10m volt in output voltage of LM35 sensor. PIC16F877A microcontroller is used to measure analog voltage value.  PIC16F877A  microcontroller built in ADC (analog to digital converter) is used to measure analog voltage.  PIC16F877A PORTA have seven built in ADC channels. So one can interface up to seven sensors with this microcontroller very easily.I will post a project on green house system.In green house system project I have used four ADC channels to measure temperature , light, humidity and moisture. Coming back to our digital thermometer, ADC has been used to read analog voltage. After reading ADC value, using voltage and temperature relationship voltage is converted back into temperature. A conversion factor is used to convert voltage back into temperature. All these conversion has been done through programming. LCD is connected to PORTB of  PIC16F877A microcontroller. I will discuss it in programming part. LCD is used to display temperature value.

Circuit diagram:

Proteus is used for simulation. Following is a circuit diagram of Digital temperature sensor:

temperature sensor
temperature sensor circuit diagram

As shown in above figure both temperature sensor and LCD is displaying same values of temperature. To ensure protection of LM35 and microcontroller you can connect 80k ohm resistor parallel to output of temperature sensor.

NOTE: Before making this project , I recommend you should learn about LCD interfacing with microcontroller especially hardware part. because I have seen many students struggling with LCD display.

CODE:

In programming part conversion factor is used to convert voltage back into temperature.The conversion factor is:

1 volt = 100 degree

temp   = ( Output voltage * 100 oC/V )

Code is written in MIKRO C compiler.

//************************* LCD module connections****************************************
//********************** written by [email protected]**********************************
sbit LCD_D7 at RB2_bit;
sbit LCD_D6 at RB3_bit;
sbit LCD_D5 at RB4_bit;
sbit LCD_D4 at RB5_bit;
sbit LCD_EN at RB6_bit;
sbit LCD_RS at RB7_bit;

sbit LCD_D7_Direction at TRISB2_bit;
sbit LCD_D6_Direction at TRISB3_bit;
sbit LCD_D5_Direction at TRISB4_bit;
sbit LCD_D4_Direction at TRISB5_bit;
sbit LCD_EN_Direction at TRISB6_bit;
sbit LCD_RS_Direction at TRISB7_bit;
//************************temperature variables****************
int temp;
char temper[7];
//**************************************************************

void READ_temp(void)
{
temp = ADC_Read(0);
temp = temp * 5/1023;                                                                                                                                                                         temp = temp * 100;

}
void data_converstion(void)
{

inttostr(temp,temper);
}
void display1(void)
{
lcd_out(1,1,"TEMPERATURE=");
lcd_out(1,13, Ltrim(temper));
Lcd_Chr_Cp(0xdf);
Lcd_Chr_Cp('C');
Lcd_Chr_Cp(' ');

}

void main()
{
ADC_Init();
Lcd_Init(); // Initialize LCD
Lcd_Cmd(_LCD_CLEAR); // Clear display
lcd_cmd(_LCD_CURSOR_OFF);
lcd_out(1,4,"DIGITAL TEMPERATURE");
lcd_out(2,6,"SENSOR");
delay_ms(1000);
Lcd_Cmd(_LCD_CLEAR); // Clear display

while(1)
{

READ_temp();
data_converstion();
display1();
}
}

If you want complete simulation and hex file of this project.Comment on this post.Also comment your issue I will love to reply to your comments.To download circuit diagram and hex file of digital temperature sensor comment on following link

Temperature sensor using pic microcontroller

Keep visiting my blog for more projects.

137 thoughts on “Temperature sensor using PIC16F877A microcontroller”

  1. hi malik,
    thank you for this good job, i don’t understand the function Ltrim and how to display a negative temperature
    thanks a lot

  2. I am doing my project on load cell, in that i want to display load cell ouput voltage into load value,for that i am using PIC16f877a..Anyone pls help me

  3. Nice project, Have downloaded zip file.Please which compiler did you use. Everything complete except header files missing in codes. Can you please send me the complete work with header file. Email: [email protected]
    Thanks

  4. its a good thing to find your blog today!!

    I want to do two projects;
    first; i wants to measure and display room temperature and humidity at real-time , or every 2seconds, I need one pin to be high and low at 1hr intervals,
    Also i want one pin to be high for only 5 seconds after every 2hrs,

    these are for alarms, one pin high per each condition ,when temperature is at 34c, 36c, 38c 39c and 39.5c
    in case of humidity, one pin high per each condition , when humidity is at 50%, 60% and 70%

    I want it to control my homemade incubator , I hope you can add to a certain pins which will control the heaters for generating temperature and humidity default temp. is 37.8c and humidity is 65%, so any deviation from these defaults the pins assigned will act accordingly from there it will stabilizer the condition

    is it possible to add several buttons for setting the completed unit? if possible it will be awesome to include. Please kindly suggest the circuit diagram and the codes.

    Let me end here with only the first .

    Sorry for my bad English, I am from Africa, Tanzania. I am waiting for you to inbox me via [email protected] , thanks in advance

  5. Hey there,
    I wish to add a keypad to this same project, such that, message on the lcd ask me press (*),
    Then ” Enter Code”. If the right code is entered, then the temperature is displayed.
    if Wrong code is entered, Code error message is displayed.
    Can you please help with a complete source code?

  6. Hello, Bilal. I’ve tried to download the .zip file from the link you’ve given here, but it tells me the link is invalid. So may i ask you to send it to my email address? its [email protected]
    Thank you, Sir!

  7. Dear Sir,

    I am Shrikant 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.

    Thanking You,

    Shrikant M
    [email protected]

      • Dear Sir,

        My logic is that
        — –> From 0-19 Celsius, Fan OFF and Heater ON
        — –> 20-25 Normal Temp. Fan OFF and Heater OFF
        — –>26-30 Fan ON at 1 level
        — –> More than 30, Fan level 2.

        With above logic I wanted to add one more feature. I am not getting sir. So, I am seeking help from you because you the one who can help me out. Project dates are very near sir. I wanted in Embedded C program. I am using Keil software. Please check my previous circuit. Kindly, help me out.
        Thanking You,
        Shrikant M
        [email protected]

  8. i have a couple of questions:
    what is the range of temperature which can be sensed?
    is this similar to the thermometer?

  9. it is a good project .i need temperature sensor project with buzzer which beep at above 40 c using pic18f452 in assembly languange .

  10. Hi! I downloaded all the files and simulated it. But there is no display in the lcd, just the white background indicating that it is running. But no texts display. Pls help

  11. hey men. i have a troubble in the communicate tempreature. when you read the tempreatue on the LCD . it send the values to your computer an you can read on the computer. Help me! thanks!

  12. Hello!
    I would like to know the full components package for “automatic speed control of ac fan using triac,temperature sensor lm35 and,and pic microcontroller”.

  13. sir can i have the code for air pollution detection system using pic16f877a where sulpher dioxide carbon monoxide sensors are interfaced to controller and readings of sensors are compared with threshold values

  14. Assalamualaikum,
    Sir ,this project is very good.I want to try it as a project in university , please send me programming file of “Temperature sensor using PIC16F877A microcontroller” in micro c compiler .Please send me urgent and my email is [email protected].
    Thanks

  15. this code is working in the proteus software but not in circuit. can u please help us to fix this problem.
    we use MPLAB XC8 compiler and PIC16F877A.
    my Email address- [email protected]
    i have to submit my project with in two weeks.

    #define _XTAL_FREQ 4000000
    #include
    #include
    #include
    #include
    #pragma config FOSC = HS // Oscillator Selection bits (HS oscillator)
    #pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled)
    #pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
    #pragma config BOREN = OFF // Brown-out Reset Enable bit (BOR enabled)
    #pragma config LVP = OFF // Low-Voltage (Single-Supply) In-Circuit Serial Programming Enable bit //(RB3/PGM pin has PGM function; low-voltage programming enabled)
    #pragma config CPD = OFF

    void main() // main program begins
    {
    double value;
    TRISD=0b00000000;
    TRISA = 0b11111111; // initialize Port A as an input port
    TRISC = 0b00000000; // initialize Port C as an Output port
    PORTC = 0b00000011;// exhaust fan switches on

    PORTD= 0b00000000;
    while(1)
    {

    ADCON0 = 0b11000101;

    ADCON1 = 0b10000000;

    while(GO_DONE);

    value = (ADRESH <75.0)
    {
    PORTC = 0b00001100;
    __delay_ms(5000);
    }

    }

    }

  16. hi bilal
    help me codes in microcontroller based power factor correction .
    i`m using pic16f877a microcontroller. it is my final year project

  17. Sir, I’m a final year student working on a project that requires to take temperature as input and identify whether the temperature is withing admissible range. please help me out with this.
    also, if you can send the hex file and simulation of the project, it would be very helpful.
    [email protected]

  18. please mail me the source code for the project temperature automatic fan speed control system using PIC16F877A. I can send u the circuit diagram for this. please sir, its very urgent.

  19. Sir, please can u kindly send me the simulation and the programming file compiled in Mikro C of this project. I am working on a similar one and I need u to help me out please. I have few works to submit it, so kindly email me the details- [email protected]

  20. Dear,
    I have copied each and every bit of your project but lcd display shows 0 degree centigrade. i have checked the code and connections twice but could not find anything wrong. what is the issue.

  21. hi dear Bilal Malik
    I can´t find the display working to show the setup data to many kind of display Models using with PIC16F877A.
    the programming method I am using are Flowcode 6 & Flowcode 7 with EB-006-00-8. programming module. every thing shows ok from my Laptop but this small mistake in setting the programming configuration is not correct .now I say it is for a long time I am searching for a solution for this problem.
    the outputs configuration has been a problem with me in many of my privet projects.
    looking forward to hear from you if there is any solution

  22. I want to know why you have added the external crystal, capacitors, and the resistors. Please tell the function of these external components.

    • The crystal gives the oscillator frequency the micro controller will be using as a clock to perform the operations. The capacitors are to absorb noise from the power, and they are chosen according to the crystal speed. The resistor is to hold the MCLR pin on high, so it runs, and it’s a high resistor so the current that flows there is super low, so the micro controller doesn’t get damaged.

  23. Please send me an email of this program because it doesn’t seem to run on the one I built; I’m using v6.6.1 of mikroPC and proteus 8.

  24. sir,
    How the source code will change, if I use HIGH-TECH ANSI compiler. Please send source code for that compiler.

  25. Whаt’ѕ Goin ⅾown i am new to this, I stumbled upon tһis I’ve found It positively ᥙseful
    and it has helped mе out loads. I hope tօ give a
    contribution & assist Ԁifferent customers ⅼike itѕ aided me.

    Good job.

  26. sir can you help me with me thesis about battery management system.
    that can monitor voltage, current, state of charge of individual battery

  27. hi, can i have the HEX file. I have try your code… the LCD is not showing the exact temperature from the sensor. Please advise.

  28. I’m using pic16F877A n LM35 sensor with CCS compiler to coding for this project. Someone who need the hex file or full code n simulation file on Proteus, just leave your Email, I’ll send it to you.

  29. I like the project . So I made this circuit on proteus using your code but not run on proteus. I need to working circuit and code please send me the full proteus and hex file.

    here is my email
    [email protected]

  30. Hi.. could u please send me the HEX file and wiring Diagram of this project. Im also working on the same project using PIC16F877A, HITECH Compiler. Would be very much helpfull if you could send me the file.
    [email protected]

  31. hi can i have the hex file am also working on the same project and it REALLY would be helpful for my project if you just send me the file please

Leave a Comment

shares