
Temperature Monitoring System Using PIC16F877A and LCD
Introduction
This project demonstrates how to build a temperature monitoring system using a PIC16F877A microcontroller, an LM35 temperature sensor, and an LCD display. The system reads the temperature from the LM35 sensor and displays it in Celsius on the LCD screen. This setup is useful for room temperature monitoring, industrial applications, and smart home systems.
Learning Objectives
- Learn how to use the ADC (Analog-to-Digital Converter) of the PIC16F877A.
- Interface an LM35 temperature sensor with a microcontroller.
- Display sensor readings on a 16×2 LCD using MikroC PRO for PIC.
- Implement basic troubleshooting techniques for circuit debugging.
Required Components
Hardware Components
- PIC16F877A Microcontroller
- LM35 Temperature Sensor
- 16×2 LCD Display
- 10kΩ Potentiometer (for LCD contrast)
- 4 MHz Crystal Oscillator
- Two 22pF Capacitors (for the oscillator)
- 10kΩ Resistor (pull-up resistor)
- 330Ω Resistor (for LCD backlight)
- 5V Power Supply
- Breadboard & Jumper Wires
Software Requirements
- MikroC PRO for PIC (for coding)
- Proteus 8 (for circuit simulation)
- Pickit 3 or USB PIC Programmer (for code uploading)
Background and Key Concepts
LM35 Temperature Sensor
- Outputs 10mV per degree Celsius.
- If temperature is 25°C, the output voltage is 250mV.
ADC (Analog-to-Digital Conversion) in PIC16F877A
- The PIC16F877A has a 10-bit ADC (values range from 0 to 1023).
- Formula to convert ADC value to voltage: V=(ADC_value1023)×5VV = \left(\frac{ADC\_value}{1023}\right) \times 5VV=(1023ADC_value)×5V
- Formula to convert voltage to temperature: Temp(°C)=V0.01Temp (°C) = \frac{V}{0.01}Temp(°C)=0.01V
Wiring Guide
LM35 Connections
- VCC → Connect to +5V
- GND → Connect to Ground (GND)
- OUT → Connect to RA0 (AN0) of PIC16F877A
LCD Connections
- RS (Register Select) → Connect to RD0
- RW (Read/Write) → Connect to RD1
- EN (Enable) → Connect to RD2
- D4, D5, D6, D7 (Data Pins) → Connect to RD4, RD5, RD6, RD7
- VEE (Contrast Adjustment) → Connect to 10kΩ Potentiometer
- VCC & GND → Connect to 5V & Ground
Code Implementation (MikroC PRO for PIC)
// Temperature Monitoring System using PIC16F877A
// Author: [Your Name]
// Date: [DD/MM/YYYY]
#include <LCD.h>
// Define LCD pins
sbit LCD_RS at RD0_bit;
sbit LCD_EN at RD2_bit;
sbit LCD_D4 at RD4_bit;
sbit LCD_D5 at RD5_bit;
sbit LCD_D6 at RD6_bit;
sbit LCD_D7 at RD7_bit;
sbit LCD_RS_Direction at TRISD0_bit;
sbit LCD_EN_Direction at TRISD2_bit;
sbit LCD_D4_Direction at TRISD4_bit;
sbit LCD_D5_Direction at TRISD5_bit;
sbit LCD_D6_Direction at TRISD6_bit;
sbit LCD_D7_Direction at TRISD7_bit;
unsigned int adc_value;
float temperature;
char display[16];
void main() {
ADCON1 = 0x80; // Configure AN0 as analog input
TRISA.F0 = 1; // Set RA0 as input (LM35)
Lcd_Init(); // Initialize LCD
Lcd_Cmd(_LCD_CLEAR);
Lcd_Cmd(_LCD_CURSOR_OFF);
while(1) {
adc_value = ADC_Read(0); // Read ADC from AN0
temperature = (adc_value * 5.0 / 1023.0) / 0.01; // Convert to Celsius
Lcd_Cmd(_LCD_CLEAR);
Lcd_Out(1, 1, “Temp: “);
FloatToStr(temperature, display);
Lcd_Out(1, 7, display);
Lcd_Out(1, 13, “C”);
Delay_ms(1000); // Update every second
}
}
Troubleshooting and Debugging
Problem: LCD not displaying anything
- Adjust the contrast potentiometer (10kΩ).
- Ensure VCC, GND, and data pins are correctly wired.
- Add a delay after Lcd_Init(); to allow proper LCD initialization.
Problem: Incorrect temperature values
- Verify LM35 wiring (VCC, GND, and OUT must be properly connected).
- Ensure ADC_Read() is working correctly.
- Use a multimeter to check the LM35 output voltage.
Problem: No temperature updates
- Make sure the while(1) loop is executing continuously.
- Check if ADC is enabled and properly configured.
- Try replacing the LM35 sensor if the issue persists.
Problem: Program not uploading to PIC
- Ensure proper PIC wiring (VCC, GND, MCLR, ICSP connections).
- Verify that the 4 MHz crystal oscillator is connected correctly.
- Try using a different USB port or reinstalling Pickit 3 drivers.
Extensions and Improvements
- Add a buzzer alarm if temperature exceeds a threshold.
- Interface with an EEPROM to store temperature data.
- Send temperature readings to a PC via UART serial communication.
- Implement fan control based on temperature readings.
Conclusion
This project successfully demonstrates how to interface an LM35 temperature sensor with a PIC16F877A microcontroller and display real-time temperature readings on an LCD. By following this guide, you can build a functional and reliable temperature monitoring system for various applications.