
Project: Temperature and Humidity Monitoring System using PIC Microcontroller
1. Introduction
This project demonstrates how to interface a DHT11 sensor with a PIC16F877A microcontroller to measure temperature and humidity. The measured values are displayed on a 16×2 LCD. This system is useful for applications such as weather monitoring, greenhouse monitoring, or home automation.
2. Learning Objectives
- Understand the working of the DHT11 sensor and how to interface it with a PIC microcontroller.
- Learn how to display sensor readings on a 16×2 LCD.
- Gain experience in MikroC programming for embedded systems.
- Develop troubleshooting skills for real-time microcontroller applications.
3. Tools and Components
Hardware:
- PIC16F877A Microcontroller
- DHT11 Temperature and Humidity Sensor
- 16×2 LCD Display
- 10K Potentiometer (for LCD contrast adjustment)
- 4 MHz Crystal Oscillator
- Two 22pF Capacitors
- 10KΩ Resistor
- 330Ω Resistor
- Breadboard and Jumper Wires
- 5V Power Supply
Software:
- MikroC PRO for PIC (for programming)
- Proteus (for simulation)
- PICkit3 (for programming the microcontroller)
4. Background and Key Concepts
The DHT11 sensor is a digital sensor that measures temperature and humidity. It communicates using a single-wire communication protocol, which involves an initial start signal followed by a response from the sensor.
The 16×2 LCD is a commonly used display module that operates in either 8-bit or 4-bit mode. In this project, we use the 4-bit mode to minimize the number of pins required for communication.
5. Circuit Design and Wiring
Connect the DHT11 sensor data pin to RB0 of the PIC16F877A. The LCD module is connected in 4-bit mode, using pins RD0, RD1, RD2, RD4, RD5, RD6, and RD7. The 10K potentiometer is used to adjust the contrast of the LCD. A 4 MHz crystal oscillator is connected between OSC1 and OSC2 of the microcontroller, along with two 22pF capacitors to stabilize the clock.
6. Code Implementation
The following MikroC code reads data from the DHT11 sensor and displays it on a 16×2 LCD:
#include <lcd.h>
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;
#define DHT11_PIN PORTB.F0
#define DHT11_PIN_DIR TRISB.F0
unsigned char humidity, temp, checksum;
void DHT11_Start() {
DHT11_PIN_DIR = 0;
DHT11_PIN = 0;
Delay_ms(18);
DHT11_PIN = 1;
Delay_us(30);
DHT11_PIN_DIR = 1;
}
unsigned char DHT11_CheckResponse() {
if (!DHT11_PIN) {
Delay_us(80);
if (DHT11_PIN) {
Delay_us(80);
return 1;
}
}
return 0;
}
unsigned char DHT11_ReadByte() {
unsigned char i, data = 0;
for (i = 0; i < 8; i++) {
while (!DHT11_PIN);
Delay_us(30);
if (DHT11_PIN) {
data = (data << 1) | 1;
} else {
data = (data << 1);
}
while (DHT11_PIN);
}
return data;
}
void main() {
Lcd_Init();
Lcd_Cmd(_LCD_CLEAR);
Lcd_Cmd(_LCD_CURSOR_OFF);
Lcd_Out(1, 1, "Temp: ");
Lcd_Out(2, 1, "Humidity: ");
while (1) {
DHT11_Start();
if (DHT11_CheckResponse()) {
humidity = DHT11_ReadByte();
temp = DHT11_ReadByte();
checksum = DHT11_ReadByte();
Lcd_Out(1, 7, Ltrim(IntToStr(temp)));
Lcd_Chr(1, 9, 'C');
Lcd_Out(2, 10, Ltrim(IntToStr(humidity)));
Lcd_Chr(2, 12, '%');
} else {
Lcd_Out(1, 7, "Error");
}
Delay_ms(2000);
}
}
7. Testing and Troubleshooting Tips
If the LCD displays garbage or blank characters, check the contrast setting of the 10K potentiometer and verify the LCD connections.
If the LCD displays “Error”, it means the DHT11 sensor is not responding. Check if the sensor is properly connected to the 5V power supply and the data pin is connected to RB0.
If the temperature or humidity readings are incorrect, verify that the timing delays in the code match the DHT11 specifications.
If the PIC does not respond, check if the 4 MHz crystal oscillator and capacitors are correctly connected.
8. Extensions and Improvements
One possible improvement is to replace the 16×2 LCD with an OLED display for better visibility. Another enhancement is to store the temperature and humidity data on an SD card or transmit it to a remote server using an ESP8266 WiFi module.
This project provides a complete guide to interfacing a DHT11 sensor with a PIC microcontroller. By following these steps, you can successfully monitor temperature and humidity in real-time.