DHT11 sensor reading NAN

can someone help me trouble shoot this problem?

type or paste code
#include "DHT.h"
#include <LiquidCrystal.h>


#define DHTPIN 22   

#define DHTTYPE DHT11   

DHT dht(DHTPIN, DHTTYPE);
LiquidCrystal lcd(8,9,4,5,6,7); 

void setup(void) {
  lcd.begin(16, 2);
  lcd.print("Reading sensor");
  dht.begin();
}

void loop() {
  
  float temperature, humidity;

  humidity = dht.readHumidity();
  temperature = dht.readTemperature();
  delay(2000); 

 lcd.clear();

 char tempF[6]; 
 char humF[6];
 dtostrf(temperature, 5, 1, tempF);
 dtostrf(humidity, 2, 0, humF);

 lcd.print("T:"); 
 lcd.print(tempF);
 lcd.print((char)223);
 lcd.print("C ");
 lcd.print("H: ");
 lcd.print(humF);
 lcd.print("%");
} here

Show the Monitor compiling errors and the Serial Monitor output.

are you certain the lcd can print arrays? You have: lcd.print(tempF); and lcd.print(humF);. do you mean lcd.print(tempF[0]);, etc?

The functions

  dtostrf(temperature, 5, 1, tempF);
  dtostrf(humidity, 2, 0, humF);

convert the data to a char array, so that should not be the issue ...

Hi @1rhin22 ,
I ported you code to Wokwi:

but had dot change the DHT11 to a DHT22 and (not to have to rewire the LCD from an existing example) also the LCD wiring. At least we can test this way if the rest of the sketch works ...

If you get a "NAN" written on the LCD it is saying "Not A Number". This is what the library returns if it does not receive a valid value from the sensor.

You may check the wiring first and then also try to add a pullip resistor to the SDA line as shown here
https://learn.adafruit.com/dht/connecting-to-a-dhtxx-sensor

Good luck!
ec2021

If it isn't a code error, and is probably not a wiring error, I'd think it was a faulty DHT. I don't know how to test those things, other than getting a new one and plugging it in.

Please don't mind but when I compared this

image

with your picture:

image

I hope that you have got a different brand/version regarding the position of GND and VCC ...

If not ... :wink:

ground wire is plugged into 5v...?

EDIT: nvm camera angle is weird. gnd is in gnd and vcc is in 5v

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.