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
1 Answer 1
Two things:
- 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.
- 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.
-
Where should I put resistorFrozen Blade– Frozen Blade2015年08月03日 21:29:51 +00:00Commented Aug 3, 2015 at 21:29
-
1Solder between pin 1 and 2 on DHT. Google image it. It's a very common question.JW2– JW22015年08月03日 21:32:01 +00:00Commented Aug 3, 2015 at 21:32
#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); } }`