The DHT11 Temperature and Humidity Sensor - uraich/IoT4AQ GitHub Wiki
Introduction
The DHT11 is a temperature and humidity sensor using a single GPIO pin. It includes a resistive type humidity sensor and an NTC thermistor. In an NTC (Negative Temperature Coefficient) type thermistor the resistance decreases as temperature rises. So... for both sensors the resistance of a material, depending on the humidity in one case and on the temperature on the other, is measured. The analog values are digitized with an Analog to Digital Converter (ADC) and a processor sends the results to the CPU via a proprietary serial protocol.
To understand how precisely the DHT11 works you will have to read the DHT11 data sheet. Here is a photo of the DHT11 mounted on a small Printed Circuit Board (PCB)

The DHT11 serial protocol
The DHT11 uses Vcc and GND and a single GPIO data line. We use GPIO 15 for this purpose. The image below is from the DHT11 data sheet and shows the global layout off its protocol:

First the DHT11 data line is programmed as output and the CPU pulls down the line for 18 ms to start a measurement. Then CPU releases the data line (pulls it high) for 20-40 us. After that, the data line is programmed to be input and the response from the DHT11 is observed. The DHT11 pulls down the line for 80 us and then up for another 80 us, indicating that the measurement data are going to follow.
During data transfer the data line is pulled low for 50 us, after which a high pulse of 26-28 us indicates a logic zero bit, while a high pulse of 70 us indicates a logic 1 bit. 5*8 (2 bytes humidity, 2 bytes temperature and 1 byte checksum) of such pulses make up the complete data of the protocol.
To demonstrate this, we provide a program that starts the measurement and then reads the data line every 4 us. When we plot the data, this is what we see:

You can figure out the results manually:
| Byte meaning | data | hex value | decimal value |
|---|---|---|---|
| integral humidity | 0100 1001 | 0x49 | 73 |
| decimal humidity | 0000 0000 | 0x00 | 0 |
| integral temperature | 0001 0101 | 0x15 | 21 |
| decimal temperature | 0000 0010 | 2 | |
| checksum | 0110 0000 | 0x60 | 96 |
The checksum is calculated adding the data values: 73 + 21 + 2 = 96 and we measure a relative humidity of 73% and a temperature of 21.2 °C.
The library
The above explication is given, simply to show you, what the DHT11 library must do. The library makes our life a lot easier. It is implemented as a C++ class with the following methods:
- readTemperature()
- readHumidity() The data sheet states that you must wait for 1s before issuing the next measurement command. Since readTemperature() and readHumidity() each start a measurement cycle you wait for 1s between the two calls. If the class does not see data coming in it will generate an error and return DHT11::ERROR_TIMEOUT instead of valid data. If there is a checksum error it returns DHT11:ERROR_CHECKSUM If you want a human readable text explaining the error you can call:
- DHT11::getErrorString(error_code) All the protocol intricacies are handled in the library code and you do not see it when using its functions.