Temperature and relative humidity meter using an inexpensive DHT11 sensor

| |

Project Summary

Numerous studies have shown that the indoor temperature and relative humidity at workplace significantly impacts workers' comfort and productivity. The first step towards optimizing these two ambient parameters is their reliable measurement. This project describes an inexpensive technique of using the DHT11 sensor for measuring temperature and relative humidity in parallel. It uses the PIC16F628A microcontroller to read the sensor output and display the results on a 16x2 character LCD.

Project Description

DHT11 is the most inexpensive sensor currently available in the market that provides calibrated digital outputs for temperature and relative humidity. It is available in a single row 4-pin package and operates from 3.5 to 5.5V power supply. It can measure temperature from 0-50 °C with an accuracy of ±2°C and relative humidity ranging from 20-95% with an accuracy of ±5%, thus making it suitable for indoor applications. The sensor has got its own proprietary 1-wire protocol to communicate with a host microcontroller.

 The following timing diagram describes the protocol involved in the communication between a MCU and the DHT11 sensor.

The MCU initiates data transmission by issuing a “Start” signal. The MCU first pulls the data line low for at least 18 ms and then pulls it high for next 20-40 μs. When the line is released by the MCU the sensor responds to the “Start“ signal by pulling the line low for 80 μs followed by a logic high signal that also lasts for 80 μs. Remember that the MCU pin must be configured to input after finishing the “Start“ signal. Once detecting the response signal from the sensor, the MCU is ready to receive data from the sensor. The sensor then sends 5 bytes of data continuously in the data line with the most significant bit first for each byte. The 5 bytes of data consists of,
Data = Integer Byte of RH + Decimal Byte of RH + Integer Byte of Temp. + Decimal Byte of Temp. + Checksum Byte
For DHT11 sensor, the decimal bytes of temperature and humidity measurements are always zero. Therefore, the first and third bytes of received data actually give the numeric values of the measured relative humidity (%) and temperature (°C). The last byte is the checksum byte which is used to make sure that the data transfer has happened without any error. If all the five bytes are transferred successfully then the checksum byte must be equal to the last 8 bits of the sum of the first four bytes, i.e.,
Checksum = Last 8 bits of (Integer Byte of RH + Decimal Byte of RH + Integer Byte of Temp. + Decimal Byte of Temp.)
Now lets talk about the signal conditions used by DHT11 for transmitting data. In order to send a bit of data, the sensor first pulls the line low for 50 μs and then raises it to high for 26-28 μs (for sending “0″), or for 70 μs (for sending “1”). So the width of the positive pulse carries information about 1 and 0.
At the end of the last transmitted bit, the sensor pulls the data line low for 50 μs and then releases it. It is now ready to receive another start signal from the MCU.

Circuit diagram

Here is the circuit diagram of this project. The DHT11 sensor and a HD44780-based character LCD are both interfaced to the PIC16F628A microcontroller, which runs at 4.0 MHz clock using an external resonator connected between OSC1 (16) and OSC2 (15) pins. The use of 4.0 MHz clock makes the timing calculation easier as 1 machine cycle becomes 1 μs. The timing information will be used to calculate the width of the received data pulse from the sensor so that we could identify if it is carrying a 1 or 0.


The firmware for this project is written in C and compiled with mikroC Pro for PIC compiler from mikroElektronika. The Timer2 module is used as a free running counter to measure the width of the received data pulse, which is required to differentiate 1 and 0. Since 1 machine cycle of PIC is 1 μs in this project, the value of Timer2 register gives the pulse width in μs, which makes timing calculation easier. When a low-to-high pulse is detected at the beginning of any data bit, Timer2 is cleared and turned ON. It is stopped whenever the data pulse is low again. The value of the TMR2 register then gives the width of the data pulse in μs. In this project, 40 μs is used as a threshold and therefore, if the received pulse width is greater than 40 μs it represents 1, otherwise it is 0. The checksum byte is verified before displaying the results on the LCD.