0

I made a circuit that takes humidity and temperature from DHT22 sensor and displays it on Lcd Screen. Simple enough but i get this error "Failed to read from DHT". That happens because i have this code

 if (isnan(t) || isnan(h)) {
  delay(100);
    Serial.println("Failed to read from DHT");
  } else {
    delay(100);
    Serial.print("Humidity: ");
    Serial.print(h);
    Serial.print(" %\t");
    Serial.print("Temperature: ");
    Serial.print(t);
    Serial.println(" *C");
    //lcd.print("Temp: ") ;
    //lcd.print(t);
    //lcd.setCursor(0, 1);
    //lcd.print("Hum: ") ;
    //lcd.print(h);
  } 

So the values are NaN . But 1 minute before it worked fine, and now again it doesnt. Everything is soldered the right way and nothing is false. Lcd is wired ok and it works but i dont get values from DHT22.

Here are some pics to help you out. Overview Wiring Wiring

In the third Image we see its working. And now its not! is the DHT sensor broken?

  #include "DHT.h"
#include <LiquidCrystal.h>`

#define DHTPIN 2     
#define DHTTYPE DHT22   // DHT 22  (AM2302)

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
DHT dht(DHTPIN, DHTTYPE);

void setup() {
  Serial.begin(9600);
  Serial.println("DHT22 test!");
  lcd.begin(16, 2)
  dht.begin();
}

void loop() {

  float h = dht.readHumidity();
  float t = dht.readTemperature();


  if (isnan(t) || isnan(h)) {
    Serial.println("Failed to read from DHT");
  } else {

    lcd.print("Hum: ");
    lcd.print(h);
    lcd.setCursor(0, 1);

    lcd.print("Temp: ");
    lcd.print(t);

  }
}

Thats my code by the way

7
  • 1
    What lib are you using? Can you post your setup code also?
    – javawocky
    Commented Aug 3, 2015 at 18:13
  • #include "DHT.h" #include <LiquidCrystal.h> #define DHTPIN 2 #define DHTTYPE DHT22 // DHT 22 (AM2302) LiquidCrystal lcd(12, 11, 5, 4, 3, 2); DHT dht(DHTPIN, DHTTYPE); void setup() { Serial.begin(9600); Serial.println("DHT22 test!"); lcd.begin(16, 2) dht.begin(); } void loop() { float h = dht.readHumidity(); float t = dht.readTemperature(); if (isnan(t) || isnan(h)) { Serial.println("Failed to read from DHT"); } else { lcd.print("Hum: "); lcd.print(h); lcd.setCursor(0, 1); lcd.print("Temp: "); lcd.print(t); } }` Commented Aug 3, 2015 at 18:59
  • 1
    Please amend your question to post the code. Putting it in a comment is pretty-much unreadable.
    – Nick Gammon
    Commented Aug 3, 2015 at 20:43
  • @NickGammon done Commented Aug 3, 2015 at 20:51
  • I can't see a resistor in your photo (maybe it is hidden). There should be a 10 k pull-up from pin 2 to 5V (that is, between pin 2 and pin 1). Do you have that?
    – Nick Gammon
    Commented Aug 3, 2015 at 21:02

1 Answer 1

2

Two things:

  1. You need to put a delay in between your read requests. Currently you're reading way too fast, every few milliseconds you're hitting the DHT. Max interval should be every two seconds or so.
  2. Add a 10k resistor between pin 1 and 2 of the DHT.

I'd bet adding a delay, or using the simpleTimer library to control how frequently you read from the DHT will clear this up.

2
  • Where should I put resistor Commented Aug 3, 2015 at 21:29
  • 1
    Solder between pin 1 and 2 on DHT. Google image it. It's a very common question.
    – JW2
    Commented Aug 3, 2015 at 21:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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