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%
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.
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);
}