ad space

DHT11 with PIC16F877A and I2C LCD Display

In this tutorial, we will explore how to interface a DHT11 sensor with a PIC16F877A microcontroller and display the temperature and humidity readings on an I2C LCD display. The DHT11 is a basic, low-cost digital temperature and humidity sensor, and the PIC16F877A is a popular 8-bit microcontroller commonly used in various electronics projects. By using an I2C LCD, we minimize the number of pins required, making the wiring simpler and reducing complexity.

Components Required:

  1. PIC16F877A Microcontroller
  2. DHT11 Temperature and Humidity Sensor
  3. I2C LCD Display (16x2 or 20x4)
  4. 4.7kΩ Resistor (for DHT11 pull-up)
  5. Jumper Wires
  6. Breadboard
  7. Power Supply (5V)
  8. MPLAB X IDE and XC8 Compiler

Circuit Diagram:

i2c lcd dht11

  1. DHT11 Sensor Connections:

    • VCC pin to 5V
    • GND pin to ground
    • Data pin to any digital pin of PIC16F877A (e.g., RB0) with a 4.7kΩ pull-up resistor.
  2. I2C LCD Connections:

    • VCC pin to 5V
    • GND pin to ground
    • SDA pin to RC4 (SDA) of PIC16F877A
    • SCL pin to RC3 (SCL) of PIC16F877A

Step 1: Configure MPLAB X Project

Open MPLAB X IDE and create a new project. Select PIC16F877A as the target device. Set up the XC8 compiler for the project.

Step 2: Install Libraries

You need two libraries:

  • DHT11 library: For reading data from the DHT11 sensor.
  • I2C and LCD library: For communicating with the I2C LCD.

You can either write your own I2C functions or use readily available libraries like LiquidCrystal_I2C.

Step 3: Write Code for DHT11 and I2C LCD

Here’s the structure of the code:

#include <xc.h>
#include "DHT11.h" // Include DHT11 header
#include "I2C_LCD.h" // Include I2C LCD header

#define _XTAL_FREQ 20000000 // 20 MHz crystal oscillator
#define DHT11_PIN RB0 // DHT11 data pin connected to RB0

void main() {
// Initialize I2C LCD
I2C_LCD_Init();
I2C_LCD_Clear();
I2C_LCD_Set_Cursor(1,1);
I2C_LCD_Write_String("DHT11 with PIC");

// Initialize DHT11
DHT11_Init(DHT11_PIN);

// Variables for storing temperature and humidity
unsigned char temperature, humidity;

while(1) {
// Read DHT11 data
if(DHT11_Read(&temperature, &humidity)) {
I2C_LCD_Set_Cursor(2,1);
I2C_LCD_Write_String("Temp: ");
I2C_LCD_Write_Number(temperature);
I2C_LCD_Write_String(" C");

I2C_LCD_Set_Cursor(2,11);
I2C_LCD_Write_String("Hum: ");
I2C_LCD_Write_Number(humidity);
I2C_LCD_Write_String(" %");
} else {
// If sensor fails to respond
I2C_LCD_Set_Cursor(2,1);
I2C_LCD_Write_String("Sensor Error");
}

__delay_ms(2000); // Delay between readings
}
}



Step 4: Explanation of the Code

  1. DHT11 Library: The DHT11_Init() function initializes the DHT11, while DHT11_Read() reads the temperature and humidity from the sensor and stores them in two variables. This sensor works on a single-wire communication protocol, which we handle through digital pin RB0.

  2. I2C LCD: The I2C_LCD_Init() function initializes the I2C LCD, and I2C_LCD_Write_String() is used to print messages. We use I2C_LCD_Write_Number() to display the numerical values (temperature and humidity).

  3. Main Loop: Inside the main loop, we constantly read the DHT11 sensor, check if the reading is successful, and update the LCD with the temperature and humidity. If the sensor reading fails, an error message is displayed.

Step 5: Compile and Upload the Code

Once the code is written, compile it using the XC8 compiler and upload the hex file to your PIC16F877A using a suitable programmer (e.g., PICkit3).

Step 6: Testing

After uploading the code, power the circuit and observe the readings displayed on the I2C LCD. The temperature should be displayed in degrees Celsius, and the humidity as a percentage.

Conclusion

This project demonstrates how to use a DHT11 sensor with a PIC16F877A microcontroller and display the readings on an I2C LCD. The combination of I2C communication with the LCD and single-wire communication with the DHT11 sensor makes this project a good exercise in integrating multiple protocols in a microcontroller environment.

Further Reading:

 

Post a Comment

Previous Post Next Post