Go Down

Topic: DHT22 on ESP32 dev board (Read 193 times) previous topic - next topic

menhir

Hello,

(If this is the wrong category, please move it to correct one)

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]
=================================
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.


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]
#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]
#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.

chucktodd

Hello,

(If this is the wrong category, please move it to correct one)

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.

Measuring temp and humidity is essential for the project with the new ESP32 so I hope you know the problem.
You might want to check out the Arduino-esp32 on github

There has been some message traffic associated with these sensors.

Chuck.
Currently built mega http server, Now converting it to ESP32.

menhir

You might want to check out the Arduino-esp32 on github

There has been some message traffic associated with these sensors.
Do you mean the chat on Gitter, linked to from your url? I can't find a way to search for old chat messages there.

There are three Issues on Github about DHT-sensors. I will take a look at the PietteTech DHT library mentioned in one issue thread and see if it helps.

menhir

I have now tested the PietteTech DHT library (https://github.com/chaeplin/PietteTech_DHT-8266) as mentioned in https://github.com/espressif/arduino-esp32/issues/759. This library seem to work very well with the ESP32. I have made one measurement every 2 seconds and havn't got one single error on 670 readings. I will do more tests tomorrow evening, but it seems very promising.

wvmarle

A very common issue with DHT22 readings is a too weak pull-up.
You should have a 10k external pull-up. Using the internal pull-up may or may not work reliably. I know the ESP8266 and Arduino are in the 20-30kΩ range, and often the DHT sensors can be made to work with just that pull-up, maybe the ESP32 uses a weaker pull-up and it stops working?
Quality of answers is related to the quality of questions. Good questions will get good answers. Useless answers are a sign of a poor question.

menhir

Thanks for your suggestion. I have tried the Adafruit library with a 20kOhm pull-up. No difference. I will use the PietteTech DHT library instead. It works fine but first reading after connection to wifi gives error. It is however easy to handle with code.

Go Up