Your new topic does not fit any of the above??? Check first. Then post here. Thanks.

Moderator: igrr

User avatar
By axelleap
#81150 I am measuring temperature with a DHT22 on a NodeMCU Lolin V3 board. I include my sketch below, but it is pretty basic. DHT22 is connected to D4 pin.

If I upload with Arduino IDE my sketch, the DHT22 returns "nan" for temperature and humidity. All the time. It is not a matter of waiting a few seconds for DHT22 to initialize correctly. :(
While the program is running, if I unplug the DHT22 from the breadboard and reconnect it, I get correct temperature and humidity values. :D

I get the same behaviour when I power on my NodeMCU. At first, DHT22 only returns nan, until I disconnect/reconnect it and then I get good values.

If I use deepSleep (and connect D0 to RST), my DHT22 always returns nan. It looks as if I'd need to disconnect and reconnect the DHT22 during the small time it is awake to get values, but that it is a very small time frame so I always get nan.

This is similar to this post. Except, with those tests, I have been able to pin point that the issue is not deep sleep, but DHT22 not initializing correctly.

Note my sketch calls dht.begin()

Is there a solution? :idea:
Thanks

Code: Select all#include <DHT.h>

// REQUIRES the following Arduino libraries:
// - DHT Sensor Library: https://github.com/adafruit/DHT-sensor-library
// - Adafruit Unified Sensor Lib: https://github.com/adafruit/Adafruit_Sensor
#define DHTTYPE DHT22
uint8_t DHTPIN = D4;

DHT dht(DHTPIN, DHTTYPE);
float temperatureC;
float humidity;

void setup() {
  // this code will only run once
  Serial.begin(115200);

  Serial.println(F("Setting up DHT..."));
  pinMode(DHTPIN, INPUT);
  dht.begin();
}



void loop() {
  temperatureC = dht.readTemperature();
  Serial.print(F("Temperature: ")); Serial.println(temperatureC);
 
  humidity = dht.readHumidity();
  Serial.print(F("Humidity: ")); Serial.println(humidity);   

  delay(1000);
 
}
User avatar
By Bonzo
#81151 It should work as I have tried it a few times before without these problems.

Have you tried the DHT ESP examples as they use a different library? They are under examples in the Arduino IDE .
User avatar
By axelleap
#81183 Hi Bonzo,

Bonzo wrote:Have you tried the DHT ESP examples as they use a different library? They are under examples in the Arduino IDE .


I don't know which example you are referring to. I couldn't find any example for DHT22 in Arduino 1.8.9 IDE. :?:

Anyway, I tried with another library as you were suggesting. For example this one: https://github.com/beegee-tokyo/DHTesp/issues
It is working better : at first when I upload the code, I get yet and again the "nan". But if I power off and power back on then I get the correct temperature and humidity values.

This would nearly be acceptable for me. Except it is not working with deepSleep. I intend to measure temperature and humidity, then enter deep sleep for a while and do it again. In that case, I always get nan as a return value.

Code: Select all#include "DHTesp.h"

#ifdef ESP32
#pragma message(THIS EXAMPLE IS FOR ESP8266 ONLY!)
#error Select ESP8266 board.
#endif

DHTesp dht;

void setup()
{
  Serial.begin(115200);
  Serial.println();

  dht.setup(D4, DHTesp::DHT22);
}

void loop()
{
  delay(dht.getMinimumSamplingPeriod());

  float humidity = dht.getHumidity();
  float temperature = dht.getTemperature();

  Serial.print(dht.getStatusString());
  Serial.print("\tHumidity %: ");
  Serial.print(humidity, 1);
  Serial.print("\t\tTemperature (C): ");
  Serial.println(temperature, 1);

  Serial.println(F("Entering deep sleep for 20 seconds"));
  ESP.deepSleep(20 * 1000000, WAKE_RF_DEFAULT);
  delay(1000);  // wait for deep sleep to occur
}
User avatar
By telemmaite
#84855 I have the exact same issue with ESP8266 NodeMcu v3 Wemos and ASAIR - DHT22 AM230.
Using the latest as of writing this comment adafruit/DHT-sensor-library version 1.38.

Any example that I have tried behaves the same, only getting nan unless I unplug and plug back the power of DHT22 then it works perfectly without any nans.

Any update on this?