DHT22 does not boot/init correctly
Posted: Sat Mar 16, 2019 4:16 pm
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.
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?
Thanks
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.
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?
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);
}