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

Got Questions? Click here and post them in the Brainy-Bits Forum to get help!

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.



SCHEMATIC

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() 

Download

You can download the Library we used here:  DHT_Library.zip

You can download the code here:  DHT11_Sketch.zip

12 Comments

Theo - Jan 06, 2017

Uh…isn’t the DHT11 a digital sensor? As such, shouldn’t it be connected to a digital pin? Does it really work on an analog pin? :|

Solide - Jan 06, 2017

Hi !

Thanks for the tutorial.

Working like a charm but as stated in one of the comments, I had to invert + and output.

Current humidity = 41.00% temperature = 26.00C which is close to what my home sensor displays.

Looks like it’s definitely not digital and why other tutorials were not working …

hasnol - Jan 06, 2017

hye. when i run this code it said “exit status 1
Error compiling for board Arduino/Genuino Uno.” so wht should i do??

lorenzo - Jan 06, 2017

the dht file won’t open

Oleg - Jan 06, 2017

“Current humidity 255,00% current temperature 255,00С”
Something strange. Could you recomend something, please.

Ja - Jan 06, 2017

Hye Paul, How to change the Signal plus pin?

Joost - Jan 06, 2017

I also get the output:
DHT11 Humidity & temperature Sensor
Current humidity = 0.00% temperature = 0.00C

However, if I change the S and + pin the Arduino stops functioning. All leds go off and I loose connection with the com port.

If I connect the sensor to a digital pin I get the output -999.

Can I safely assume my sensor is broken, or is there something I could try?

Thanks!

Aaron - Jan 06, 2017

Humidity = 0.00% temperature =0.00C
please tell me a solution. Inverting the + and output changes the output to 255.00% and 255.00 C. I have a project to submit. please?

Thank you.

tom - Jan 06, 2017

To get it to compile change line1

DHT.h to dht.h

the .h file should be all lower case.

lee - Jan 06, 2017

temperature=0º humidity=0%

How do I fix this?

Got Questions? Click here and post them in the Brainy-Bits Forum to get help!

Leave a comment

Comments have to be approved before showing up