Measurement and control of temperature and relative humidity finds applications in numerous areas. These days devices are available which have both temperature and humidity sensors with signal conditioning, ADC, calibration and communication interface all built inside them. The use of such smart sensors greatly simplify the design and reduces the overall cost. We discussed in past aboutΒ Humidity and temperature measurements with Sensirionβs SHT1x/SHT7x sensors. These sensors are capable of measuring both temperature and relative humidity and provide fully calibrated digital outputs. While Β SHT1x/SHT7x are very accurate sensors, they are still expensive for hobbyists use. This articles discusses the DHT11 sensor which also provides calibrated digital outputs for temperature and humidity but is relatively lot cheaper than the Sensirion sensors. The DHT11 sensor uses a proprietary 1-wire protocol which we will be exploring here and implementing with the PIC16F628A microcontroller that will receive the temperature and humidity values from the sensor and display them on a 16Γ2 character LCD.
About DHT11 sensor
The DHT11 sensor comes 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%. The sensor provides fully calibrated digital outputs for the two measurements. It has got its own proprietary 1-wire protocol, and therefore, the communication between the sensor and a microcontroller is not possible through a direct interface with any of its peripherals. The protocol must be implemented in the firmware of the MCU with precise timing required by the sensor.
The following timing diagrams describe the data transfer protocol between a MCU and the DHT11 sensor. The MCU initiates data transmission byΒ issuing a βStartβ signal. The MCU pin must be configured as output for this purpose. The MCU first pulls the data line low for at least 18 ms and then pulls it high for next 20-40 ΞΌs before it releases it. Next, the sensor responds to the MCU β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 should be ready to receive data from the sensor. The sensor then sends 40 bits (5 bytes) of data continuously in the data line.Β Note that while transmitting bytes, the sensor sends the most significant bit first.
The 40-bit data from the sensor has the following structure.
Data (40-bit) = 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 most important thing, which is signalling for transmitting β0β³ and β1β³. In order to send a bit of data, the sensor first pulls the line low for 50Β ΞΌs. Then it raises the line to high for 26-28 ΞΌs if it has to send β0β³, or for 70Β ΞΌs if the bit to be transmitted is β1β³.Β So it is the width of the positive pulse that 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. The DHT11 sensor requires an external pull-up resistor to be connected between its Vcc and the data line so that under idle condition, the data line is always pulled high. After finishing the data transmission and releasing the data line, the DHT11 sensor goes to the low-power consumption mode until a new βStartβ signal arrives from the MCU.
Circuit diagram
Here is the circuit diagram showing the DHT11 sensor and a HD44780-based character LCD interfaced to the PIC16F628A microcontroller. The microcontroller 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.
For more detail: Measurement of temperature and relative humidity using DHT11 sensor and PIC microcontroller using PIC16F628A