DHT22 Sensor interface with PIC microcontroller – mikroC Projects

Interfacing DHTxx sensors with Microchip PIC microcontroller is so easy as what was done in the last project where PIC16F887 microcontroller is used to read relative humidity and temperature data from DHT11 sensor. Now this topic shows how to interface the PIC16F887 microcontroller with DHT22 sensor with mikroC Pro for PIC compiler.

Related Projects:
Interfacing PIC MCU with DHT11 sensor – mikroC
PIC16F887 Microcontroller with DHT22 digital sensor – CCS C

Hardware Required:

  • PIC16F887 microcontroller
  • DHT22 sensor (AM2302 – RHT03)  —  datasheet
  • 16×2 LCD screen
  • 10k variable resistor (or potentiometer)
  • 4.7k ohm resistor
  • 5V Power source
  • Breadboards
  • Jumper wires

Interfacing PIC microcontroller with DHT22 (AM2302 – RHT03) sensor circuit:
Circuit diagram is shown below.

mikroC DHT22 sensor with PIC microcontroller (AM2302 RHT03)

(All grounded terminals are connected together)

The DHT22 sensor has 4 pins which are (from left to right): VCC (+5V), data, NC (not connected) pin and GND. The data pin is connected to pin RB0 of the PIC16F887 microcontroller.

The LCD module is connected to PORTD pins with:
RS —> RD0
E  —> RD1
D4 —> RD2
D5 —> RD3
D6 —> RD4
D7 —> RD5

In this example the PIC16F887 microcontroller runs with its internal oscillator (@ 8MHz) and MCLR pin is configured as digital input pin.

Interfacing PIC microcontroller with DHT22 sensor mikroC code:
mikroC configuration words are:
CONFIG1 : 0x2CD4
CONFIG2 : 0x0700

The configuration words can be changed from: Project –> Edit Project …

Functions used in the code:
void Start_Signal(void): used to send start signal to the DHT22 sensor.
unsigned short Check_Response(): used to detect the response signal which comes from the DHT22 sensor, this function returns 1 if OK and 0 if error.
unsigned short Read_Data(unsigned short* dht_data): this function reads 1 byte (8 bits) from the sensor, data are saved in the variable dht_data. Returns 0 if OK and 1  if error (time out error).

Rest of code is described through comments.

The video below shows a Proteus simulation of the project:

Leave a Reply

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