0

I am trying to read the humidity and temperature using a DHT11 temperature sensor for an Arduino UNO using the DHT.h library. I have my code below:

#include <DHT.h>
#define dataPin 4
dht DHT;

void setup() {
  Serial.begin(9600)

}

void loop() {
  int readData = DHT.read11(dataPin);
  float t = DHT.temperature;
  float h = DHT.humidity;
  Serial.println("Temperature: ")
  Serial.print(t)
  Serial.print(" ºC")
  Serial.println("Humidity: ")
  Serial.print(h)
  Serial.print(" % ")
  delay(2000)
}

When compiling the program, it gives me an error:

fatal error: DHT.h: No such file or directory

I have also tried with lower case letters, but it will not work. Is there any reason why? Do I have to install it? I have tried to search for the library in the Arduino app Sketch - Include Library - Manage Libraries, but the library is not there.

  • Actually it is a custom library by adafruit. It is present at github.com/adafruit/DHT-sensor-library – svtag Sep 18 '17 at 19:27
  • adafruit/DHT-sensor-library is not the correct library for the code in the question. That code is written for a different library, which I have provided instructions for below. – per1234 Sep 18 '17 at 21:30
  • it seems nothing problem with your library. Have you calibrate your formula(math eq inside coding)? – ben Jul 9 '19 at 9:29
1
  1. Download https://github.com/RobTillaart/Arduino/archive/master.zip
  2. Copy the Arduino-master/libraries/DHTstable subfolder of the downloaded file to the libraries subfolder of your sketchbook folder. You can find the location of your sketchbook folder at File > Preferences > Sketchbook location in the Arduino IDE.
  3. Change the first line of your sketch from:

    #include <DHT.h>
    

    to:

    #include <dht.h>
    

Now when you try to compile the sketch you will still encounter many errors. However, these are caused by a paucity of semicolons, not by the DHT library, and thus off-topic for this question.

  • Thanks, @per1234, now it is not showing an error. But, the readings of humidity and temperature are not correct. Here the temperature is nearly 20-degree Celsius with humidity nearly 60%. But Arduino shows the temperature of 2.3-degree Celsius and humidity of 13%. Why so? How to correct this? – Naseeb Gill Jul 9 '19 at 6:10
0

I have had problems using some DHT library versions. I suggest that you try using a different version of dht.h

This version works for me (there are newer versions, but I recommend starting with this oldie but goldie):

dht.h (606B 6/12/2012)

// 
//    FILE: dht.h
// VERSION: 0.1.00
// PURPOSE: DHT Temperature & Humidity Sensor library for Arduino
//
//     URL: http://arduino.cc/playground/Main/DHTLib
//
// HISTORY:
// see dht.cpp file
// 

#ifndef dht_h
#define dht_h

#if ARDUINO >= 100
 #include "Arduino.h"
#else
 #include "WProgram.h"
#endif

#define DHT_LIB_VERSION "0.1.00"

class dht
{
public:
    int read11(uint8_t pin);
    int read22(uint8_t pin);
    double humidity;
    double temperature;

private:
    uint8_t bits[5];  // buffer to receive data
    int read(uint8_t pin);
};
#endif
//
// END OF FILE
//

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.