I am trying to read temperature and humidity from a DHT-11 sensor with a arduino uno R3
#include <DHT.h>
#include <LiquidCrystal.h>
#define DHTPIN A3
#define DHTTYPE DHT11
DHT dht(DHTPIN,DHTTYPE);
LiquidCrystal lcd(5,8,9,10,11,12);
String hum="Humidity:";
String temptext="Temp:";
void setup() {
Serial.begin(9600);
lcd.clear();
lcd.begin(16,2);
dht.begin();
}
void loop() {
float humidity = dht.readHumidity();
delay(500);
float temp = dht.readTemperature();
delay(500);
Serial.println(hum+humidity);
Serial.println(temptext+temp);
lcd.clear();
lcd.print(hum + humidity);
lcd.setCursor(0,2);
lcd.print(temptext+temp);
delay (5000);
}
I am sure that my wiring is correct. What would be possible reasons for the DHT-11 reporting nothing but NAN? Might it be broken (I just unpacked it)?
Reverse order:
Q: Might it be broken?
A: Unlikely. The only time I have had one go bad was when I connected the voltage backwards for too long. If it is just a short while it can survive that OK. But when it was too long it melted the thing.
Q: What are the possible reasons to see NaN?
A: Possible reasons:
1) You are using an Analog pin to receive a Digital signal.
2) A wire is loose
I regularly run several sensing stations that have 2 or 3 DHT-22s on them, and I use digital pins. Some of them are 20 feet away from the Arduino.
But if a wire comes loose I can see 0.00 for sure.
Here is a Q&A where I ask how to deal with those: gnuplot: Faulty sensor sometimes reading 0.00 - how to convert them to missing?
Of course the sensor isn't really faulty. It is always that I have pulled on a wire and it comes loose. Losing contact with any of the pins always gives the 0.00 readings.
So switch it over to a digital pin and see how it acts.
Here is my code:
Before setup(), do this:
// 2 devices: Input pins D2 and D5
#include <Adafruit_Sensor.h>
#include "DHT.h"
#define DHTPIN 2
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
#define DHT2PIN 5
#define DHT2TYPE DHT22
DHT dht2(DHT2PIN, DHT2TYPE);
Then
void loop() {
float h = dht.readHumidity();
float t = dht.readTemperature();
float df = t*(9.0/5.0)+32;
if (isnan(h) || isnan(t)) { h=0; df=0; }
float h2 = dht2.readHumidity();
float t2 = dht2.readTemperature();
float df2 = t2*(9.0/5.0)+32;
if (isnan(h2) || isnan(t2)) { h2=0; df2=0; }
Serial.print(h); Serial.print("\t");
Serial.print(df); Serial.print("\t");
Serial.print(h2); Serial.print("\t");
Serial.println(df2);
delay(30000);
}