Problem with DHT22 on ESP32

menhir
Posts: 2
Joined: Thu Nov 16, 2017 7:14 pm

Problem with DHT22 on ESP32

Postby menhir » Thu Nov 16, 2017 7:33 pm

Hello,

I have bought a ESP32 Development Board (NodeMCU ESP-32S v1.1). I have used Uno, Mega and Wemos D1 mini (ESP8266) before together with DHT22 with good results. However, I can't get the DHT sensor to work properly on the ESP32 board. Hopefully, you know how to solve it.

The problem is that it doesn't give readings all the time. This is an output from SimpleDHT library with one reading every 2.5 seconds:

Code: Select all

=================================
Sample DHT22...
Sample OK: 22.70 *C, 38.20 RH%
=================================
Sample DHT22...
Sample OK: 22.70 *C, 39.00 RH%
=================================
Sample DHT22...
Read DHT22 failed, err=102
=================================
Sample DHT22...
Sample OK: 22.70 *C, 38.80 RH%
=================================
Sample DHT22...
Sample OK: 22.70 *C, 38.70 RH%
=================================
Sample DHT22...
Read DHT22 failed, err=102
=================================
Sample DHT22...
Sample OK: 22.70 *C, 38.40 RH%
=================================
Sample DHT22...
Sample OK: 22.70 *C, 39.20 RH%
=================================
Sample DHT22...
Sample OK: 22.70 *C, 42.50 RH%
=================================
Sample DHT22...
Sample OK: 22.70 *C, 42.10 RH%
Error 102 is "Error to wait for data read signal." according to the documentation.

This is what I have done so far:
  • I have tried with three different DHT22 sensors. All of them works fine with stable readings on Arduino Mega and ESP8266 since several months. All of them gives same problem on ESP32.
  • I have tried two different libraries; SimpleDHT and Adafruits DHT sensor library. Latest version of both plus the second latest of Adafruits. It gives the same error with both libraries.
  • I have tried several different pins with same result.
  • I have tried without pullup resistor, with 4.7k and with 10k. No difference.
  • I have tried different delays between readings. No difference.
  • I have tried to connect DHT on both 3.3v and 5v. No difference.
These are the standard example sketches I have used (copied directly from libraries example folders). Only pin number and delay have been changed:

Code: Select all

#include "DHT.h"

#define DHTPIN 16
//our sensor is DHT22 type
#define DHTTYPE DHT22

//create an instance of DHT sensor
DHT dht(DHTPIN, DHTTYPE);
void setup() {
  pinMode(DHTPIN, INPUT);
  Serial.begin(115200);
  Serial.println("DHT22 sensor!");
  //call begin to start sensor
  dht.begin();
}

void loop() {
  //use the functions which are supplied by library.
  float h = dht.readHumidity();
  // Read temperature as Celsius (the default)
  float t = dht.readTemperature();
  // Check if any reads failed and exit early (to try again).
  if (isnan(h) || isnan(t)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }
  // print the result to Terminal
  Serial.print("Humidity: ");
  Serial.print(h);
  Serial.print(" %\t");
  Serial.print("Temperature: ");
  Serial.print(t);
  Serial.println(" *C ");
  //we delay a little bit for next read
  delay(2000);
}

Code: Select all

#include <SimpleDHT.h>

// for DHT22, 
//      VCC: 5V or 3V
//      GND: GND
//      DATA: 2
int pinDHT22 = 16;
SimpleDHT22 dht22;

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

void loop() {
  // start working...
  Serial.println("=================================");
  Serial.println("Sample DHT22...");
  
  // read without samples.
  // @remark We use read2 to get a float data, such as 10.1*C
  //    if user doesn't care about the accurate data, use read to get a byte data, such as 10*C.
  float temperature = 0;
  float humidity = 0;
  int err = SimpleDHTErrSuccess;
  if ((err = dht22.read2(pinDHT22, &temperature, &humidity, NULL)) != SimpleDHTErrSuccess) {
    Serial.print("Read DHT22 failed, err="); Serial.println(err);delay(2000);
    return;
  }
  
  Serial.print("Sample OK: ");
  Serial.print((float)temperature); Serial.print(" *C, ");
  Serial.print((float)humidity); Serial.println(" RH%");
  
  // DHT22 sampling rate is 0.5HZ.
  delay(3000);
}
Measuring temp and humidity is essential for the project with the new ESP32 so I hope you know the problem.

tele_player
Posts: 90
Joined: Sun Jul 02, 2017 3:38 am

Re: Problem with DHT22 on ESP32

Postby tele_player » Fri Nov 17, 2017 5:45 am

Does your application really require that every read is successful? Have you determined what the failure rate is? Does an immediate retry alway/usually/sometimes/never succeed?

Of course, it would be nice to know what is happening, but if you’re not trying to catch instantaneous changes in readings, some number of failed reads shouldn’t be a problem, with a little logic for retry and/or or interpolation.

tom1803
Posts: 4
Joined: Fri Sep 29, 2017 4:01 am

Re: Problem with DHT22 on ESP32

Postby tom1803 » Fri Nov 17, 2017 6:48 am

Hi,

I know exactly what you mean. I have the same problem. It works fine on ESP8266 but not on ESP32. It seems there's some timing issue.

After searching the internet for answers and trying all the various libraries out there without luck, I ended up writing my own. However, there's still a problem. Namely, when WiFi is used in conjunction, it still fails to read at a rate of about 1%. Otherwise, it's very stable.

Cheers,
Tom

menhir
Posts: 2
Joined: Thu Nov 16, 2017 7:14 pm

Re: Problem with DHT22 on ESP32

Postby menhir » Fri Nov 17, 2017 8:01 am

Hi,

Thanks for your answers.
tom1803 wrote:It seems there's some timing issue.
I read the Github issue https://github.com/espressif/arduino-esp32/issues/759 for Arduino-ESP32 after I had created this topic and gave that library (https://github.com/chaeplin/PietteTech_DHT-8266) a try. As you wrote it seemed to be a timing issue and the PietteTech library is a "Interrupt driven non-blocking library for DHT sensors". And it does work! At least after the modifications recommended in the Github issue. I have made one reading every 2 seconds during the night and when I woke up I had more than 20 000 correct readings and no errors. I will rewrite my "real" code today evening for this library and hope it will continue to work fine.

Who is online

Users browsing this forum: ok-home and 68 guests