DHT11 Digital Temperature & Humidity Sensor

This was my first sensor. It’s a digital temperature and humidity sensor. It uses three pins, two for power and one for data. It worked perfectly…except….the libraries provided by Virtuabotix ($9.99 via Amazon.com) didn’t work with the Arduino IDE v1.0.

So if fixed it!

You can download the library from this post. Remember to place the folder DHT11 in your libraries folder. Restart the IDE if it was running when you copied it.

Download the DHT11LIB v.04.5 library files.

Download the DHT11 Data Sheet

Here’s the code for the Arduino that uses the library. It’s been modified a little bit from its original state:

[php]
/*####################################################################
FILE: dht11_functions.pde
VERSION: 0.4.5
PURPOSE: Test DHT11 Temperature & Humidity Sensor library functions
LICENSE: GPL v3 (http://www.gnu.org/licenses/gpl.html)

DATASHEET: http://feed.virtuabotix.com/wp-content/uploads/2011/11/DHT11.pdf
GET UPDATES: http://feed.virtuabotix.com/?239

–##–##–##–##–##–##–##–##–##–##–
## ## ## ## ## ## ## ## ## ## ##
## ## ## ## ## ## ## ## ## ## ##
| ## ## ## ## ## ## ## ## ## ## |
## ## ## ## ## ## ## ## ## ## ##
## ## ## ## ## ## ## ## ## ## ##
| ## ## ## ## ## ## ## ## ## ## |
## ## ## ## DHT11 SENSOR ## ## ## ##
## ## ## ## ##FRONT ## ## ## ## ##
| ## ## ## ## ## ## ## ## ## ## |
## ## ## ## ## ## ## ## ## ## ##
## ## ## ## ## ## ## ## ## ## ##
| ## ## ## ## ## ## ## ## ## ## |
## ## ## ## ## ## ## ## ## ## ##
## ## ## ## ## ## ## ## ## ## ##
–##–##–##–##–##–##–##–##–##–##–
|| || || (Not ||
|| || || Used) ||
VDD(5V) Readout(I/O) Ground

HISTORY:
Mod by Joseph Dattilo (Virtuabotix LLC) – Version 0.4.5 (11/11/11)
Mod by Joseph Dattilo (Virtuabotix LLC) – Version 0.4.0 (06/11/11)
Mod by Rob Tillaart – Version 0.3 (28/03/2011)
Mod by SimKard – Version 0.2 (24/11/2010)
George Hadjikyriacou – Original version (??)
#######################################################################*/

#include <dht11.h>

dht11 DHT11;

#define DHT11PIN 2

void setup()
{
Serial.begin(9600);
Serial.println(“DHT11 TEST PROGRAM “);
Serial.print(“LIBRARY VERSION: “);
Serial.println(DHT11LIB_VERSION);
}

void loop()
{
Serial.println(“n”);

int chk = DHT11.read(DHT11PIN);

Serial.print(“Read sensor: “);
switch (chk)
{
case 0: Serial.println(“OK”); break;
case -1: Serial.println(“Checksum error”); break;
case -2: Serial.println(“Time out error”); break;
default: Serial.println(“Unknown error”); break;
}

Serial.print(“Humidity (%): “);
Serial.println((float)DHT11.humidity, DEC);

Serial.print(“Temperature ( C): “);
Serial.println((float)DHT11.temperature, DEC);

Serial.print(“Temperature ( F): “);
Serial.println(DHT11.fahrenheit(), DEC);

Serial.print(“Temperature ( K): “);
Serial.println(DHT11.kelvin(), DEC);

Serial.print(“Dew Point ( C): “);
Serial.println(DHT11.dewPoint(), DEC);

Serial.print(“Dew PointFast ( C): “);
Serial.println(DHT11.dewPointFast(), DEC);

delay(5000);
}

[/php]

Author: Helicopter Jeff

I'm a vinyl decal cutting and designing, Astrophotography and general photography capturing, RC helicopter/quad copter pilot, Arduino and Raspberry Pi hardware/software developer, network, scripting and troubleshooting ninja living in the metro NY area. I am passionate about my hobbies so much I do them for a living. There's nothing better than getting paid to do what you love.

21 thoughts on “DHT11 Digital Temperature & Humidity Sensor”

  1. Hello everybody. I’m trying to do a DHT11 to work using a 16F873A microcontroller. Can you guys give me some tips about how to make them work together? My biggest problem is that I have to make the DHT11 send the data to the microcontroller and I never used any kind of sensor before.

    1. I don’t think this is the right forum for a non-arduino micro controller, but you may get a response. The Arduino has libraries specifically written to communicate with the DHT11 sensor. You should try to google code for your micro controller to see if anyone has done this before.

      1. I have already tried to google how to make the DHT11 work, but I couldn’t find anything useful.. So I tried to look for anything about the DHT11 including arduino to find a way to solve my problem.. My main problem is how to make the pulse necessary for making the DHT11 send the data to the microcontroller.
        NOTE: I need to use assembly.

    2. Felipe,

      It has been a long time since I personally wrote anything for a PIC chip, but I am a little familiar with PICBASIC, and hopefully I can give some insight as the author of the Virtuabotix DHT11/DHT22 libraries. You basically have the choice of rewriting using a pulsin technique, or of line by line translation of the existing C code.

      Okay, so this is the core part of the Virtuabotix DHT11 library (the code that handles the protocol itself).

      int read(int pin)
      {
      // BUFFER TO RECEIVE
      uint8_t bits[5];
      uint8_t cnt = 7;
      uint8_t idx = 0;

      // EMPTY BUFFER
      for (int i=0; i 5 BYTES or TIMEOUT
      for (int i=0; i 40) bits[idx] |= (1 << cnt);
      if (cnt == 0) // next byte?
      {
      cnt = 7; // restart at MSB
      idx++; // next byte!
      }
      else cnt–;
      }

      // WRITE TO RIGHT VARS
      // as bits[1] and bits[3] are always zero they are omitted in formulas.
      humidity = bits[0];
      temperature = bits[2];

      uint8_t sum = bits[0] + bits[2];

      if (bits[4] != sum) return -1;
      return 0;
      }

      If you really look at what is going on here line for line it is fairly simply filling a 5 byte array using pulse-width modulation. If you go line by line you should be able to translate all of the C++ code into PICBASIC with relative ease.

      If you make that much progress and still have trouble I would be happy to look it over for inconsistencies, and put it up on the wiki so folks of all types can play with the sensor.

      Respectfully,
      Joseph Dattilo
      Founder Virtuabotix LLC

      1. Hello Joseph,
        My main problem is that I have to make the pulse necessary so that the DHT11 send the data to the microcontroller and then the PC, I have to use assembly, but I think my problem is majorly make the pulse.

  2. hello everybody. i would like to ask a question related to dht11 digital humidity sensor
    1- is it possible to use dh11 with PLC? i m trying to use it in PLC but no result when connected.
    2-is it NO type NC type
    3-sinking or sourcing?
    i couldnt find this information from my sensor manual

    1. This is a digital sensor. It’s not NO/NC or analog. You need a digital io port to read the data from it. Arduino has a library to communicate with it because the digital data requires some decoding.

      A PLC should be able to use it but not in an on/off capacity, one wire digital interfaces doesn’t work like that. If you need an analog (variable voltage) sensor the DHT11 isn’t going to work.

      1. Jeff,

        I have done a little work with PLCs in the past and as long as you can write a driver in a C based language you should be able to “fairly easily” translate the code labeled as the “simplest possible code” on the wiki page I put together for the DHT11/DHT22 libraries I wrote.

        Here is a link for you https://www.virtuabotix.com/reference/index.php?title=DHT11_Wiki

        You have to be able to change a single pin from output to input pretty quickly, but as long as you can do that, I have found few PLCs that were not capable of the slow data speeds used by the DHT series. If you come up with working code be sure to share it with me or add it to the Virtuabotix wiki yourself.

        Respectfully,
        Joseph Dattilo

      2. Excellent point Joseph- I’m under the assumption that he’s using a contact/relay port and not a digital io port.

        I’d love to see a relay jump back and forth fast enough to be considered a digital port… 😉

  3. works good, i would to ask how i can configure when the sensor of temp up to 23 C, to tell to another parallel port switch on the fan ?

    1. Not sure what you mean by parallel- I assume meaning another i/o port.
      In the code block that triggers during the temp range add a line to put a digital port high to turn on a fan. The fan of course would need to be connected through a relay as the digital port can’t supply enough power for it.
      If you want me to write the code or an example let me know.

      1. Hi, I would like you to write a code or an example about this. In my project I’m using a DHT22 and when the temperature reaches 25 C it has to activate a DC motor (adapted as a little fan). Could you help me? Thanks

      2. Hi,

        I’m on my second arduino project and I’m trying to get a DHT-11 to switch a 4.5v fan on when humidity levels get too high in my lounge due to condensation.

        I’ve been googling and this is the closest I’ve found to what I am looking to do…

        I was just wondering whether you did ever write any code – as I am a total newb to coding – and if you could kindly post it if you did.

        Best regards

        Matt

  4. Error Compiling:
    In file included from dht11_functions.cpp:38:
    E:arduino-0023librariesDHT11LIB/dht11.h:41:21: error: Arduino.h: No such file or directory

  5. Joe from Virtuabotix here, I just wanted to let you all know that all of our libraries have been IDE 1.0 compatible for a while now, but thank you for making the changes and letting us know about it.

    I have written a bunch more libraries since so be sure to let me know if you find anything you suggest fixing in the future. Just in case you don’t have it here is a link to our libraries section https://www.virtuabotix.com/feed/?page_id=1587

    Keep being nerdy!
    ~Joe

  6. I used the code you listed above and the library you have, but changed the library title to dht11.h I get an error message on code line: dht11 DHT11; saying that ‘dht11’ does not contain a type. Have you encountered this issue and has it been resolved?

    1. I just tried the example and it worked ok. Make sure that the libraries are in a folder called libraries in the Arduino IDE folder. Then the folder for the library should be DHT11LIB. You shouldn’t have to modify anything. Make sure you also restart the IDE after you put the library files in there.

      If that doesn’t help send me an email and I’ll see what I can figure out. Include a screenshot of the error too.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s