Computing stuff tied to the physical world

Reading out DHTxx sensors

In Software on Apr 14, 2012 at 00:01

The DHT11 and DHT22 sensors measure temperature and humidity, and are easy to interface because they only require a single I/O pin. They differ in their measurement accuracy, but are in fact fully interchangeable.

There is code for these sensors floating around on the web, but it all seems more complicated than necessary, and I really didn’t want to have to use floating point. So I added a new “DHTxx” class to JeeLib which reads them out and reports temperature in tenths of a degree and humidity in tenths of a percent.

Along with a new demo sketch called dht_demo:

Screen Shot 2012 04 02 at 03 40 38

That’s about as simple as it gets, and it compiles to less than 4 KB.

Sample output, in this case a DHT11 which only reports whole degrees and percentages:

Screen Shot 2012 04 02 at 03 26 05

Humidity is gradually shooting up as I breathe next to it (there’s a slight lag).


  1. Xlnt, I have just started to use these and now it’s built into jeelib! – thanks.

  2. For those interested (like me), here are the differences between the DHT11 and the DHT22 (source:


    Ultra low cost
    3 to 5V power and I/O
    2.5mA max current use during conversion (while requesting data)
    Good for 20-80% humidity readings with 5% accuracy
    Good for 0-50°C temperature readings ±2°C accuracy
    No more than 1 Hz sampling rate (once every second)
    Body size 15.5mm x 12mm x 5.5mm
    4 pins with 0.1" spacing


    Low cost
    3 to 5V power and I/O
    2.5mA max current use during conversion (while requesting data)
    Good for 0-100% humidity readings with 2-5% accuracy
    Good for -40 to 125°C temperature readings ±0.5°C accuracy
    No more than 0.5 Hz sampling rate (once every 2 seconds)
    Body size 15.1mm x 25mm x 7.7mm
    4 pins with 0.1" spacing

    • Yes, I got bit by the sampling rate. I attached this guy to a UAV which was typically going at 10 m/s so at the prescribed sampling rate that would be every 20m for a DHT22. I like the sensor for several reasons, but for the UAV application, I should look at something for better sampling resolution.

      p.s. JCW thanks for the nice library implementation! Do you add any access to error checking?

  3. Is there any temperature sensors with more accuracy ?

  4. Hi JCW and Everyone,

    Recently I added one DHT22 to a second wireless temperature data logger. These wireless data loggers are both using bare minimum Atmega328P, caps and are powered with two AA batteries (~3.3v).

    Running only the DS18B20 (and the ATmega328P) once every minute (the system is in low power mode for about 59 secs each minute) and consumes less than 0.01v / week. After adding the DHT22, I realized that in one week, the consumption raised to approximately 0,5v/week (yes, that’s 1/2 volt) after a week running.

    This seems a “power-hungry” sensor to run in this minimal approach. (remotely and with batteries)

    I think I’m not putting it to sleep after every minute reading… Is there any way to put the DHT22 in low-power/sleep mode while not reading data?


    • Even in low-power mode it draws a fair amount of current (150 µA, I think). The only way to avoid that is probably to power it from an I/O pin and turn that pin off.

  5. Hi JCW,

    Thanks for the tip! I will try that and see how it goes. Regards, Luís

Comments are closed.