DHT11/DHT22 Humidity and Temperature sensors

1. Overview

This library provides functions for interfacing with DHT11 or DHT22 low cost Humidity and Temperature sensors. Both of them have the same electrical interface but different measurement range and accuracy. Connection with the sensor is done using any IO pin of Ansteron Board. The number of sensors is only limited by the number of available pins.

Note that these sensors can only be read once every 1 or 2 seconds. It may not respond if reading at a higher rate.

2. Circuit

The sensor comes in a 4 pins packet, pin number 1 is on the left when looking at front side, where the air vents are. Data pin (pin #2) can be connected to any of the IO pins of Ansteron Board. In the circuit below, it is connected to pin D7 which will work for the example program. Pull up resistor of value 1K to 10K is required.

3. Example

This example will read Humidity and Temperature from the sensor and print to Serial Terminal:

Humidity and temperature data from the sensor are 16 bit values. The high byte is integral part and the low byte is decimal. For example, 40.0% will be 0x2800 (high byte=40, low byte=0). If only the integral part is needed, shift raw value to the right 8 bit (value=value>>8;). For DHT22, MSB bit of integral part is sign of the value, 0 is positive, 1 is negative (temperature can be negative).

4. Functions


Read humidity and temperature value from the sensor connected to pin and write values into given variable references (pointers). This function returns 1 if succeeded, 0 if sensor did not respond or data error occurred.

Inputs humidity_ptr and temp_ptr are references (pointers) to a variable that will receive result. Use operator "&" in front to get pointer to the variable.

The sensor may not respond if this function is called in a period shorter than 1 or 2 seconds (zero will be returned).



Return humidity or temperature in a 16bit value with high byte is the integral part and low byte is decimal part of the measurements. These functions returns zero if an error occurred or the measurement is zero.

The time between readings must be 1 or 2 seconds apart.


Check if a sensor is connected to given pin. The function returns 1 if sensor is detected, 0 if not.

5. Notes

The time it takes for each of the functions above to complete is between 22 and 24 milliseconds.

All of the functions above should not be called more than once every 1 or 2 seconds (1 second for DHT11 and 2 seconds for DHT22).