🔥 Project: PIC16F877A Temperature Monitoring System fb90dd19 f862 4f40 9b57 83963253

🔥 Project: PIC16F877A Temperature Monitoring System


1️⃣ Introduction

This project measures the ambient temperature using an LM35 sensor and displays it on a 16×2 LCD. It’s a common application in thermostats, HVAC systems, and data logging.

Real-World Applications:

  • Home thermostats
  • Greenhouse monitoring
  • Industrial temperature control

2️⃣ Learning Objectives

By the end of this project, you’ll:

  • Understand ADC (Analog-to-Digital Converter) usage in PIC microcontrollers.
  • Learn LCD interfacing with a PIC microcontroller.
  • Develop and debug embedded C code.
  • Master reading analog sensors and displaying data.

3️⃣ Tools and Components

✅ Hardware:

  • PIC16F877A microcontroller
  • LM35 temperature sensor
  • 16×2 LCD display
  • 10kΩ potentiometer (for LCD contrast)
  • 10kΩ resistor (optional for pull-ups)
  • 0.1μF capacitor (for power supply decoupling)
  • Breadboard or PCB
  • Jumper wires
  • 5V power supply

✅ Software:

  • MPLAB X IDE
  • XC8 Compiler
  • PICkit 3 or similar programmer

4️⃣ Background and Definitions

ADC (Analog-to-Digital Converter): Converts analog voltage to digital value. PIC16F877A has a 10-bit ADC module (0–1023 range).

LM35 Sensor: Analog temperature sensor with a linear output of 10mV/°C. At 25°C, it outputs 250mV.

LCD (16×2): A display module that can show alphanumeric data. Connected in 4-bit or 8-bit mode; here, we use 4-bit mode to save pins.


5️⃣ Step-by-Step Guide


🛠️ Circuit Diagram

LM35:
- VCC (Pin 1) -> +5V
- GND (Pin 3) -> GND
- Vout (Pin 2) -> RA0/AN0 (Pin 2 on PIC16F877A)

LCD (16x2) in 4-bit mode:
- RS -> RC0
- RW -> GND
- E  -> RC1
- D4 -> RC2
- D5 -> RC3
- D6 -> RC4
- D7 -> RC5
- VSS -> GND
- VDD -> +5V
- VEE -> Potentiometer center pin (Contrast)

💻 Annotated Source Code (XC8)

#include <xc.h>

// CONFIG
#pragma config FOSC = HS        // High-speed oscillator
#pragma config WDTE = OFF       // Watchdog Timer disabled
#pragma config PWRTE = ON       // Power-up Timer enabled
#pragma config BOREN = ON       // Brown-out Reset enabled
#pragma config LVP = OFF        // Low-voltage programming disabled
#pragma config CPD = OFF        // Data EEPROM memory code protection off
#pragma config WRT = OFF        // Flash Program Memory Write Enable off
#pragma config CP = OFF         // Flash Program Memory Code Protection off

#define _XTAL_FREQ 20000000     // 20 MHz Crystal

// LCD Connections
#define RS RC0
#define EN RC1
#define D4 RC2
#define D5 RC3
#define D6 RC4
#define D7 RC5

// Function Prototypes
void LCD_Init(void);
void LCD_Command(char cmd);
void LCD_Char(char data);
void LCD_String(const char *str);
void LCD_Clear();
void ADC_Init();
unsigned int ADC_Read(unsigned char channel);
void Display_Temperature(float temperature);

void main() {
    TRISC = 0x00;       // PORTC as output for LCD
    TRISA = 0xFF;       // PORTA as input for ADC

    LCD_Init();         // Initialize LCD
    ADC_Init();         // Initialize ADC

    while (1) {
        unsigned int adc_value = ADC_Read(0);  // Read ADC value from AN0
        float voltage = adc_value * (5.0 / 1023.0);  // Convert to voltage
        float temperature = voltage * 100.0;         // Convert to °C

        LCD_Clear();
        LCD_String("Temp: ");
        Display_Temperature(temperature);
        LCD_Char(0xDF);  // Degree symbol (custom)
        LCD_Char('C');

        __delay_ms(1000);  // Update every second
    }
}

// ADC Initialization
void ADC_Init() {
    ADCON0 = 0x41;      // ADC ON, Channel 0 selected
    ADCON1 = 0x80;      // Right justified result, Vref = Vdd
}

// ADC Read Function
unsigned int ADC_Read(unsigned char channel) {
    ADCON0 &= 0xC5;                // Clear channel bits
    ADCON0 |= (channel << 3);      // Set channel
    __delay_ms(2);                 // Acquisition time
    GO_nDONE = 1;                  // Start conversion
    while (GO_nDONE);              // Wait until complete
    return ((ADRESH << 8) + ADRESL); // Return result
}

// LCD Functions
void LCD_Init() {
    LCD_Command(0x02);  // Initialize in 4-bit mode
    LCD_Command(0x28);  // 2-line, 5x7 matrix
    LCD_Command(0x0C);  // Display ON, cursor OFF
    LCD_Command(0x06);  // Increment cursor
    LCD_Command(0x01);  // Clear display
    __delay_ms(2);
}

void LCD_Command(char cmd) {
    RS = 0;
    D4 = (cmd >> 4) & 0x01;
    D5 = (cmd >> 5) & 0x01;
    D6 = (cmd >> 6) & 0x01;
    D7 = (cmd >> 7) & 0x01;
    EN = 1;
    __delay_ms(2);
    EN = 0;

    D4 = (cmd >> 0) & 0x01;
    D5 = (cmd >> 1) & 0x01;
    D6 = (cmd >> 2) & 0x01;
    D7 = (cmd >> 3) & 0x01;
    EN = 1;
    __delay_ms(2);
    EN = 0;
}

void LCD_Char(char data) {
    RS = 1;
    D4 = (data >> 4) & 0x01;
    D5 = (data >> 5) & 0x01;
    D6 = (data >> 6) & 0x01;
    D7 = (data >> 7) & 0x01;
    EN = 1;
    __delay_ms(2);
    EN = 0;

    D4 = (data >> 0) & 0x01;
    D5 = (data >> 1) & 0x01;
    D6 = (data >> 2) & 0x01;
    D7 = (data >> 3) & 0x01;
    EN = 1;
    __delay_ms(2);
    EN = 0;
}

void LCD_String(const char *str) {
    while (*str)
        LCD_Char(*str++);
}

void LCD_Clear() {
    LCD_Command(0x01);
    __delay_ms(2);
}

void Display_Temperature(float temperature) {
    int temp_int = (int)temperature;
    int tens = temp_int / 10;
    int units = temp_int % 10;
    LCD_Char(tens + '0');
    LCD_Char(units + '0');
}

6️⃣ Testing and Debugging Tips

✅ No Display on LCD: Check contrast potentiometer wiring. Also, verify that RW is grounded.

✅ Incorrect Temperature Reading: Confirm Vref configuration (we use Vdd = 5V) and double-check the LM35 wiring.

✅ ADC Reading Stuck: Ensure acquisition time is sufficient (__delay_ms(2)).

✅ Floating ADC Channel: Connect unused ADC channels to GND via a resistor to prevent noise.


7️⃣ Extensions

🚀 Add Fahrenheit conversion and display both scales.

🚀 Log data via UART to a PC.

🚀 Use interrupts for ADC instead of polling.

🚀 Integrate a fan relay or heater control output.