Interfacing DHT22 sensor with PIC microcontroller | MPLAB Projects

This post shows how to interface DHT22 (AM2302, RHT03 …) digital temperature and humidity sensor with PIC16F887 8-bit microcontroller where the values of temperature and relative humidity are displayed on 1602 LCD screen.
The compiler used in this example is Microchip MPLAB XC8 (MPLAB X IDE with MPLAB XC8 compiler).

Related Projects:
Interfacing LCD with PIC microcontroller | MPLAB Projects
Interfacing DHT11 sensor with PIC microcontroller | MPLAB Projects

Hardware Required:

  • PIC16F887 microcontroller
  • DHT22 sensor
  • 1602 (16×2) LCD screen
  • 4.7k ohm resistor
  • 10k ohm variable resistor or potentiometer
  • 330 ohm resistor
  • 5V Power source
  • Breadboard
  • Jumper wires

Interfacing DHT22 sensor with PIC microcontroller circuit:
The following image shows project circuit schematic diagram.

PIC microcontroller with DHT22 (AM2302) sensor and LCD - MPLAB XC8 DHT22

(All grounded terminals are connected together)

The DHT22 sensor has 4 pins (from left to right): VCC (+5V), data pin, NC (not connected pin) and GND. The data pin is connected to pin RB0 of the PIC16F887 microcontroller. A pull-up resistor of 4.7k ohm is required for the data pin.

The 16×2 LCD screen is connected to the PIC16F887 microcontroller as follows:
RS —> RD0 pin
E  —> RD1 pin
D4 —> RD2 pin
D5 —> RD3 pin
D6 —> RD4 pin
D7 —> RD5 pin
VSS, RW, D0, D1, D2, D3 and K are connected to circuit GND (ground)
VEE to the variable resistor (or potentiometer) output pin
VDD to +5V and A to +5V through 330 ohm resistor

VEE pin is used to control the contrast of the LCD. A (anode) and K (cathode) are the back light LED pins.

In this project the PIC16F887 microcontroller runs with its internal oscillator @ 8 MHz, MCLR pin is configured as an input pin.

Interfacing DHT22 sensor with PIC microcontroller C code:
The C code below is for MPLAB XC8 compiler, it was tested with version 2.00 installed on MPLAB X IDE version 5.05

To be able to compile the C code, a small LCD library for MPLAB XC8 compiler is required which can be downloaded from the following link:
MPLAB XC8 LCD Library

after the download, add the library file (LCD_Lib.c) to project folder.

Functions used in the code:
Start_Signal(): this function sends the start signal to the sensor, it sends a logic low for 25 ms and a logic high for 30 us.

Check_Response(): this function checks if there is a response from the sensor (after sending the start signal using the previous function), returns 1 (true) if ok and 0 (false) if error (for example a connection problem).

Read_Data(*dht_data): this function reads temperature and humidity data from the sensor (4 bytes), it also reads an other byte (5th byte) which is used as a checksum. This function returns 0 (false) if data read was ok and 1 (true) if there was a time out problem.

As shown in the circuit diagram above, the DHT22 data pin is connected to pin RB0, this connection is defined as follows:

In this project I used Timer1 module to measure signal widths (logic 1 and logic 0 widths), it is configured to increment every 1 us (MCU speed is 8MHz (2 MIPS) and Timer1 prescaler is 1:2  —> Timer1 input clock frequency = 1MHz).

The microcontroller used in this example is PIC16F887, configuration words are:

Where:

  • In-Circuit Debugger disabled
  • Low voltage programming disabled
  • Fail-Safe Clock Monitor enabled
  • Internal/External Switchover mode enabled
  • Brown-out Reset (BOR) disabled
  • Data memory code protection disabled
  • Program memory code protection disabled
  • RE3/MCLR pin function is digital input, MCLR internally tied to VDD
  • Power-up Timer (PWRT) disabled
  • Watchdog Timer (WDT) disabled
  • INTOSCIO oscillator: I/O function on RA6/OSC2/CLKOUT pin, I/O function on RA7/OSC1/CLKIN
  • Flash Program Memory Self Write disabled
  • Brown-out Reset set to 4.0V

Full MPLAB XC8 code:

Proteus simulation of the project should give a result similar to what’s shown in the following video (connections between the PIC16F887 and the 1602 LCD are not the same):

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.