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)?
Try to plug it to a USB hub powered by an external power source. It might also help replacing the USB cable with a better or shorter one. Having a USB port that supplies enough power or using a good USB cable often fixes this problem.
To read sensor data from the DHT11 sensor, Arduino must first send it the start signal. To do so, the pin that DHT11's DATA pin is interfaced with must be set to a digital output. A digital pulse of 18 milliseconds must be passed to the DATA pin, followed by a rising edge.
The DHT11 is a commonly used Temperature and humidity sensor that comes with a dedicated NTC to measure temperature and an 8-bit microcontroller to output the values of temperature and humidity as serial data.
The DHT11 sensors usually require external pull-up resistor of 10KΩ between VCC and Out pin for proper communication between sensor and the Arduino. However, the module has a built-in pull-up resistor, so you need not add it. The module also has a decoupling capacitor for filtering noise on the power supply.
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);
}
NaN stands for "Not a Number". Sometimes the sensor fails to read and gives you a nan value. You can't do anything against it, but in the DHT.h library is a function called isnan(). So you can make a backup variable were you store the last value that was correct. Then you can check if your sensor reads nan and if he does you can print out the backup variable:
float temperature;
float bTemperature;
temperature = dht.readTemperature();
if(!isnan(temperature)){
bTemperature = temperature;
Serial.println(temperature);
}
else{
Serial.println(bTemperature);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With