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:
- PIC16F877A Microcontroller
- DHT11 Temperature and Humidity Sensor
- I2C LCD Display (16x2 or 20x4)
- 4.7kΩ Resistor (for DHT11 pull-up)
- Jumper Wires
- Breadboard
- Power Supply (5V)
- MPLAB X IDE and XC8 Compiler
Circuit Diagram:
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.
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:
Step 4: Explanation of the Code
DHT11 Library: The
DHT11_Init()
function initializes the DHT11, whileDHT11_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.I2C LCD: The
I2C_LCD_Init()
function initializes the I2C LCD, andI2C_LCD_Write_String()
is used to print messages. We useI2C_LCD_Write_Number()
to display the numerical values (temperature and humidity).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:
- PIC16f877A LED Blink Code
- How to Control an LED Using a Switch with PIC16F877A
- ADC Example Code for PIC16F877A
- How to use UART with PIC16F877A
- How to Calculate the PR2 register value in PIC16F877A
- DC Motor Control with PIC16F877A PWM Signals
- PIC16F877A Timers Explained: Understanding and Using Timers in Embedded Projects
- Controlling Servo Motors with PIC16F877A: Applications in Robotics