In this tutorial, we will learn to interface DHT22 temperature and humidity sensor with pic microcontroller. We will begin with an introduction of DHT22, its pinout diagram, Features, internal circuit. After that we will discuss interfacing circuit diagram. At the end of tutorial, we see programming examples in MPLAB compiler and MikroC Pro for pic.

DHT22 Introduction

The DHT-22 is a sensor which measures relative humidity and temperature sensor and provides a calibrated digital output. It is an inexpensive sensor that is almost similar to the DHT11 but measures temperature and humidity with higher accuracy and wider range. You don’t need extra components for operation. It is pre-calibrated, and you can directly connect it to obtain the output. The DHT22 sensor is composed of a humidity sensing sensor and a thermistor. These two components measure the humidity and temperature and send out a digital signal on the data pin.

Pin Configuration Diagram

This figure shows the pinout diagram of DHT22 humidity and temperature sensor. It consists of three pins.

Pin Description

The DHT22 sensor is very simple and easy to use. It has only four pins.

Vcc is the power pin. Apply voltage in a range of 3.5 V to 5.0 V at this pin.

Data Out is the digital output pin. It sends out the value of measured temperature and humidity in the form of serial data.

N/C is a not connect pin.

GND: Connect the GND pin to main ground.

Features

The important features of DHT22 sensor are:

The range of operating voltage () is 3 V to 5 V power.

It measures temperature in a range of -40°C to +125°C with an accuracy of ± 0.5 degrees.

The measuring range for humidity is from 0 to 100% with an accuracy of 2-5%.

The maximum operating current for DHT22 sensor is 2.5mA.

The sampling rate for DHT22 sensor is 0.5 Hz. It takes measurement once every 2 seconds.

The sensor has four pins with 0.1″ spacing and a size of body is 15.1mm x 25mm x 7.7mm.

Where to use it?

It is small in size and utilizes low power. It has a long transmission distance of about 20m. Normally, we use DHT22 sensor in applications for measuring ambient temperature and humidity. You can also use DHT11 sensor as it has same function but DHT22 sensor is more reliable as compared to DHT11 sensor. It is pre-calibrated and it has internal temperature compensation.

These sensors are well suited for use in all kinds of harsh applications and you can interface this device with microcontrollers easily. If you need a sensor for measuring temperature in a range of-40°C to +125°C or you need to measure humidity, then this sensor would be ideal for use in such an application.

Connection Diagram with any Microcontroller

The connections are simple as it does not require any external electrical components. It has only four pins in which one is a not connect pin. You can directly connect the other three pins with any microcontroller. The DHT22 sensor does not have any clock pin that means it operates in serial asynchronous mode. It needs a pull-up resistor between and data line to keep it in High logic. This pull-up resistor is essential for communication between the sensor and a microcontroller. Figure (3) shows the basic connection diagram for connecting this sensor.

Interfacing with Pic Microcontroller

The breakout board of this sensor has an onboard pull-up resistor hence you can directly connect its breakout board with PIC microcontroller or Arduino.

In this example, we have used PIC18F25K20 microcontroller and used the RA0 pin to receive data from DHT22. We use a pull-up resistor of value 4.7 kΩ or 10 kΩ. Connect the data output pin to the RA0 pin of PIC18F25K20. The figure below shows the connections of a DHT22 sensor with Pic microcontroller.

Apply 5V power to the sensor. In some applications, we connect a 100nF capacitor between ground and power in which provides wave filtering. After applying power, don’t send any instructions withing 1 second because the sensor takes one second to give stable output values of temperature and humidity.

We have used 16×2 LCD with PORTB of PIC18F25K20 microcontroller. If you didn’t work with LCD before, you can read this post:

Proteus Simulation Result

MikroC Code

This code is written using MikroC for Pic compiler. Create a new project with MikroC compiler by selecting PIC16F877A microcontroller and set frequency to 8MHz. If you don’t know how create new project in mikroC, we suggest you read this post: