temperature sensor

Arduino – DHT22 temperature and humidity sensor driver

DHT22/AM2302  is an inexpensive temperature and relative humidity sensor. The sensor, which can be bought on ebay, implements a digital communication protocol by pulling high or low on a data line. The protocol is described here and it has three states:

  1. transmission start – initiated by a host and acknowledged by the sensor
  2. data transfer – binary transmission
  3. transmission end – performed by the sensor

Setup

The communication protocol instructs that the data line needs to always be kept high using a pull up resistor (this is because the sensor will pull low on the line to transmit data). However, in this specific implementation, an external pull up resistor is not needed because the driver makes usage of the internal pull up resistor of the Arduino pin.

The cables need to be connected as follows:

  • pin1 = VDD
  • pin2 = data
  • pin3 = not used
  • pin4 = GNDarduino_dht22

Usage

  • First you need to download the driver source code from here or you can browse it here.
  • Next, copy the unzipped contents to your Arduino libraries directory in a subdirectory named DHTSensor.
  • Restart your IDE

You are now ready to do a reading

As it can be observed from the example, the temperature of the measurement result can be interpreted in Celsius, Fahrenheit or Kelvin degrees, while the humidity is percentual.

Furthermore, the sensor can also be powered by a digital pin using the following construct

Keep in mind that a sensor measurement will take around a second and during this time the execution is not yielded back (your board will stop processing anything else). The one second is due to how the sensor is engineered to communicate and the non-yielding execution is due to the implementation of the driver.

Disclaimer

Implementing the driver was performed as an educational project and it comes with no guarantees. You are free to use and modify it in any way it makes sense for you.