0

why do I end up with the display "Failed to read from DHT sensor!" in the serial monitor in most cases when I run the code given below?? In case I reduce the delay to 1000ms the reading shows 0.00 for both temperature and humidity which, as well, is pretty much false......Any help would be highly appreciated...

// Example testing sketch for various DHT humidity/temperature sensors
// Written by ladyada, public domain

#include "DHT.h"

#define DHTPIN 2     // what pin we're connected to

// Uncomment whatever type you're using!
//#define DHTTYPE DHT11   // DHT 11 
#define DHTTYPE DHT22   // DHT 22  (AM2302)
//#define DHTTYPE DHT21   // DHT 21 (AM2301)



// Initialize DHT sensor for normal 16mhz Arduino
DHT dht(DHTPIN, DHTTYPE);
// NOTE: For working with a faster chip, like an Arduino Due or Teensy, you
// might need to increase the threshold for cycle counts considered a 1 or 0.
// You can do this by passing a 3rd parameter for this threshold.  It's a bit
// of fiddling to find the right value, but in general the faster the CPU the
// higher the value.  The default for a 16mhz AVR is a value of 6.  For an
// Arduino Due that runs at 84mhz a value of 30 works.
// Example to initialize DHT sensor for Arduino Due:
//DHT dht(DHTPIN, DHTTYPE, 30);

void setup() {
  Serial.begin(9600); 
  Serial.println("DHTxx test!");

  dht.begin();
}

void loop() {
  // Wait a few seconds between measurements.
  delay(2000);

  // Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  float h = dht.readHumidity();
  // Read temperature as Celsius
  float t = dht.readTemperature();
  // Read temperature as Fahrenheit
  float f = dht.readTemperature(true);

  // Check if any reads failed and exit early (to try again).
  if (isnan(h) || isnan(t) || isnan(f)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }

  // Compute heat index
  // Must send in temp in Fahrenheit!
  float hi = dht.computeHeatIndex(f, h);

  Serial.print("Humidity: "); 
  Serial.print(h);
  Serial.print(" %\t");
  Serial.print("Temperature: "); 
  Serial.print(t);
  Serial.print(" *C ");
  Serial.print(f);
  Serial.print(" *F\t");
  Serial.print("Heat index: ");
  Serial.print(hi);
  Serial.println(" *F");
}
7
  • I am having the same problem. would it be possible to share the sketch and library that works for you?
    – user11310
    Commented Jul 19, 2015 at 21:55
  • Please provide me the solution for this problem as i am also getting this type of error.. -Thanks
    – Jay M
    Commented Mar 16, 2016 at 3:54
  • Make sure you define the digital pin number, I didn't and I got that error // Example testing sketch for various DHT humidity/temperature sensors // Written by ladyada, public domain #include "DHT.h" #define DHTPIN 12 // what digital pin we're connected to
    – KiwiPingu
    Commented Aug 29, 2016 at 11:36
  • Have you tried loading the Adafuit_Sensor library before DHT?
    – SDsolar
    Commented May 7, 2017 at 2:50
  • what changes occurr if i use LM35 instead of DHT22. Anyone would help...
    – mehwish
    Commented Oct 25, 2017 at 18:48

2 Answers 2

3

I suggest you to modify the following lines of code to check which reading has issue.

if (isnan(h) || isnan(t) || isnan(f)) {
   Serial.println("Failed to read from DHT sensor!");
   return;
}
1

Have you put a 4.7kOhm resistor between data pin and Vcc on the dht22? i had problems with a reading until i did. see this HTH

1
  • Yeah, I have that 4.7k plugged in but it didn't quite work....now I have made it work using a different sketch and library altogether( courtesy Rob Tillaart)....
    – biswajit
    Commented May 31, 2015 at 6:55

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