How to use the DHT11 Temperature – Humidity Sensor with Arduino

//How to use the DHT11 Temperature – Humidity Sensor with Arduino
TUTORIAL

DHT11 Tutorial


How to use the DHT11 Temperature and Humidity Sensor with an Arduino!

OVERVIEW

In this tutorial we will learn how to use a DHT (DHT11 version) Temperature and Humidity Sensor.

It’s accurate enough for most projects that need to keep track of humidity and temperature readings.

Again we will be using a Library specifically designed for these sensors that will make our code short and easy to write.

CONNECTIONS

As you can see we only need 3 connections to the sensor, since one of the pin is not used.

The connection are : Voltage, Ground and Signal which can be connected to any Analog Pin on our UNO.

THE CODE

Since we will be using a Library that is available for this sensor, our code will be very short and simple.

Once you have the library, just go ahead and extract it to the Library folder inside your Arduino IDE software folder.

#include "dht.h"
#define dht_apin A0 // Analog Pin sensor is connected to
 
dht DHT;
 
void setup(){
 
  Serial.begin(9600);
  delay(500);//Delay to let system boot
  Serial.println("DHT11 Humidity & temperature Sensor\n\n");
  delay(1000);//Wait before accessing Sensor
 
}//end "setup()"
 
void loop(){
  //Start of Program 
 
    DHT.read11(dht_apin);
    
    Serial.print("Current humidity = ");
    Serial.print(DHT.humidity);
    Serial.print("%  ");
    Serial.print("temperature = ");
    Serial.print(DHT.temperature); 
    Serial.println("C  ");
    
    delay(5000);//Wait 5 seconds before accessing sensor again.
 
  //Fastest should be once every two seconds.
 
}// end loop() 
TUTORIAL VIDEO
DOWNLOAD

For the Arduino code, just copy and paste the code above in your Arduino IDE software.

You can download the DHT Library we used here: DHT-Library

By | 2018-07-18T23:27:35-04:00 January 8th, 2015|Tutorials|

36 Comments

  1. FireballTitan June 4, 2019 at 8:56 am - Reply

    Got this error once again, can someone help:

    Arduino: 1.8.5 (Windows 10), Board: “Arduino/Genuino Mega or Mega 2560, ATmega2560 (Mega 2560)”

    Sketch uses 4944 bytes (1%) of program storage space. Maximum is 253952 bytes.
    Global variables use 293 bytes (3%) of dynamic memory, leaving 7899 bytes for local variables. Maximum is 8192 bytes.
    avrdude: stk500v2_ReceiveMessage(): timeout
    avrdude: stk500v2_ReceiveMessage(): timeout
    avrdude: stk500v2_ReceiveMessage(): timeout
    avrdude: stk500v2_ReceiveMessage(): timeout
    avrdude: stk500v2_ReceiveMessage(): timeout
    avrdude: stk500v2_ReceiveMessage(): timeout
    avrdude: stk500v2_getsync(): timeout communicating with programmer
    An error occurred while uploading the sketch
    Invalid library found in C:\Users\admin\Documents\Arduino\libraries\sketch_jun04a: C:\Users\admin\Documents\Arduino\libraries\sketch_jun04a
    Invalid library found in C:\Users\admin\Documents\Arduino\libraries\sketch_jun04a: C:\Users\admin\Documents\Arduino\libraries\sketch_jun04a

    This report would have more information with
    “Show verbose output during compilation”
    option enabled in File -> Preferences.

    This was the code I used:

    #include “dht.h”
    #define dht_apin A0 // Analog Pin sensor is connected to

    dht DHT;

    void setup(){

    Serial.begin(9600);
    delay(500);//Delay to let system boot
    Serial.println(“DHT11 Humidity & temperature Sensor\n\n”);
    delay(1000);//Wait before accessing Sensor

    }//end “setup()”

    void loop(){
    //Start of Program

    DHT.read11(dht_apin);

    Serial.print(“Current humidity = “);
    Serial.print(DHT.humidity);
    Serial.print(“% “);
    Serial.print(“temperature = “);
    Serial.print(DHT.temperature);
    Serial.println(“C “);

    delay(5000);//Wait 5 seconds before accessing sensor again.

    //Fastest should be once every two seconds.

    }// end loop()

    • FireballTitan June 4, 2019 at 9:42 am - Reply

      Fixed some things, now I’m still getting an error but it’s shorter:

      Arduino: 1.8.9 (Windows 10), Board: “Arduino/Genuino Mega or Mega 2560, ATmega2560 (Mega 2560)”

      Sketch uses 4914 bytes (1%) of program storage space. Maximum is 253952 bytes.
      Global variables use 293 bytes (3%) of dynamic memory, leaving 7899 bytes for local variables. Maximum is 8192 bytes.
      avrdude: stk500v2_ReceiveMessage(): timeout
      avrdude: stk500v2_ReceiveMessage(): timeout
      avrdude: stk500v2_ReceiveMessage(): timeout
      avrdude: stk500v2_ReceiveMessage(): timeout
      avrdude: stk500v2_ReceiveMessage(): timeout
      avrdude: stk500v2_ReceiveMessage(): timeout
      avrdude: stk500v2_getsync(): timeout communicating with programmer
      An error occurred while uploading the sketch

      This report would have more information with
      “Show verbose output during compilation”
      option enabled in File -> Preferences.

  2. ASHWANI April 13, 2019 at 2:01 pm - Reply

    CAN CAN WE SET DIGITAL OUTPUT AT SOME DIGITAL PIN AT CERTAIN TEMPERATURE LEVEL

  3. Gameez February 6, 2019 at 3:35 pm - Reply

    For everybody who have problem like 0C and 0% humidity just change the port to A1 in the code and plug in back in the a1 and it will work

    • priya March 15, 2019 at 6:17 pm - Reply

      I changed port to a1 still getting 0C and 0%

      • Alain de Pauw June 12, 2019 at 8:05 pm - Reply

        change the positions of the wirings, i had the same problems

  4. Adolfo December 4, 2018 at 8:59 pm - Reply

    How do you change it from Celcius to Fahrenheit?

  5. divya ciaz November 28, 2018 at 4:44 am - Reply

    why is it keep on showing no such file or directory

  6. Murali Balakrishnan November 26, 2018 at 8:34 pm - Reply

    Thanks a lot. Great!!!

  7. harsh sharma November 20, 2018 at 9:28 am - Reply

    it shows please select a port before upload . how can i solve it

    • Shawn July 12, 2019 at 10:30 pm - Reply

      Go to tool n select ure port

  8. Nick October 12, 2018 at 3:12 am - Reply

    Hi I keep getting this error, shown below, and I cant figure out why. Can someone please help.

    sketch_oct12a:6: error: ‘dht’ does not name a type

    dht DHT;

    ^

    C:\Users\nicholas\Documents\Arduino\Temp and Humidity\sketch_oct12a\sketch_oct12a.ino: In function ‘void loop()’:

    sketch_oct12a:20: error: expected unqualified-id before ‘.’ token

    DHT.read11(dht_apin);

    ^

    sketch_oct12a:23: error: expected primary-expression before ‘.’ token

    Serial.print(DHT.humidity);

    ^

    sketch_oct12a:26: error: expected primary-expression before ‘.’ token

    Serial.print(DHT.temperature);

    ^

    C:\Users\nicholas\Documents\Arduino\Temp and Humidity\sketch_oct12a\sketch_oct12a.ino: In function ‘void setup()’:

    sketch_oct12a:34: error: redefinition of ‘void setup()’

    void setup() {

    ^

    C:\Users\nicholas\Documents\Arduino\Temp and Humidity\sketch_oct12a\sketch_oct12a.ino:8:6: note: ‘void setup()’ previously defined here

    void setup() {

    ^

    C:\Users\nicholas\Documents\Arduino\Temp and Humidity\sketch_oct12a\sketch_oct12a.ino: In function ‘void loop()’:

    sketch_oct12a:39: error: redefinition of ‘void loop()’

    void loop() {

    ^

    C:\Users\nicholas\Documents\Arduino\Temp and Humidity\sketch_oct12a\sketch_oct12a.ino:17:6: note: ‘void loop()’ previously defined here

    void loop() {

    ^

    exit status 1
    ‘dht’ does not name a type

    • Ron October 21, 2018 at 12:11 pm - Reply

      Go onto the youtube video. The first comment has a solution.

    • moh March 15, 2019 at 5:36 pm - Reply

      add library before use the code !

  9. Albert September 6, 2018 at 9:39 pm - Reply

    Hello nice post. is the same code if i have the DHT22 and my arduino is mega 2560? i do the same as you but it doesnt work. Thanks!

  10. Parveen Verma July 27, 2018 at 9:13 am - Reply

    The best &easiest way you have shown to get data from DHT11 with very easy connections. thanks a ton

  11. Horacio Venturino July 24, 2018 at 10:44 am - Reply

    I like very much your presentations. For your clarity, tidy and complete documentation.
    Thanky you very much” I learn a lot.

  12. al July 24, 2018 at 5:18 am - Reply

    Probably you have different pins: 1 – signal (left), 2 – 5V (middle), 3 – GND (right)

  13. ashish July 13, 2018 at 3:38 am - Reply

    when I am uploading the program its showing that DHT director not found plz help me out

  14. Daniel June 30, 2018 at 4:43 pm - Reply

    hey! I am having a problem… it is giving me the wrong outputs, it looks something like this:
    Curdity = -999.00% temperature = -999.00C
    Current humidity = -999.00% temperature = -999.00C
    DHT11 Humidity & temperature Sensor

    Current humidity = -999.00% temperature = -999.00C
    Current humidity = -999.00% temperature = -999.00C
    Current humidity = -999.00% temperature = -999.00C

    What did I do wrong?
    Thanks!

    • Yosuar Vazquez July 31, 2018 at 2:35 pm - Reply

      Switch the pins, you probably have signal on the far left, VCC IN MIDDLE AND ground on far right.

      • Faraaz November 25, 2018 at 9:06 am - Reply

        It’s still coming 999

    • moh March 15, 2019 at 5:39 pm - Reply

      dht_apin A(x) = ANALOG pins

  15. Vivek Raj Gupta May 18, 2018 at 8:12 am - Reply

    value is 0 only

  16. Pamila Klinich May 16, 2018 at 10:46 am - Reply

    Your site is so fantastic. I’m going to come back here again.

  17. EMIL4 May 7, 2018 at 5:34 am - Reply

    pins were wrong by me

    right pin = Ground
    middle pin = VCC
    left pin = Signal

  18. ANKIT April 13, 2018 at 9:17 am - Reply

    showing compilation error for arduino uno

    • Ash June 12, 2018 at 1:57 pm - Reply

      in line 1: it should be #include “dht.h” instead of #include “DHT.h”

      • Myk July 18, 2018 at 9:38 pm - Reply

        I had to change the line #include “DHT.h” to #include “dht.h” as the .h file in the dht library that you link to does not have a DHT.h file, it has a dht.h file

        • brainy-bits July 18, 2018 at 11:29 pm - Reply

          Thank you Ash and Myk for pointing that out, I’ve gone ahead and change the code above to reflect this. Cheers!

  19. Mauricio April 9, 2018 at 8:49 pm - Reply

    Same here.

    0% and 0°C

  20. vishnu February 26, 2018 at 9:46 pm - Reply

    its doesnt work properly it shows
    Current humidity = 0.00% temperature = 0.00C
    Current humidity = 0.00% temperature = 0.00C
    Current humidity = 0.00% temperature = 0.00C
    Current humidity = 0.00% temperature = 0.00C
    Current humidity = 0.00% temperature = 0.00C
    Current humidity = 0.00% temperature = 0.00C

    • Sinon March 29, 2018 at 10:31 pm - Reply

      same here

      • hectormtc May 6, 2018 at 2:26 am - Reply

        same too : /

    • Shankara May 17, 2018 at 1:37 pm - Reply

      Make sure you have plugged your sensor in right. I think that the problem here maybe due to you plugging the sensor into the wrong analog pin. Try plugging it into a0 and change your code adequately.

    • Shankara May 17, 2018 at 1:39 pm - Reply

      The problem you have may be due to the fact that the arduino is reading data from the wrong pins. Try changing the analog pin for the sensor in the code or unplug the sensor and plug it back into the analog pin that is used in the code. For instance, I used A0, so you need to connect the sensor to the analog pin A0.

Leave A Comment