Problem with ESP32-Wroom when connected to DHT22-Sensor

Hello everyone,

I set up an asynchronic webserver with the ESP32 to read data from a DHT22, which is working fine.
But when I switch on or restart the ESP, I have to disconnect the DHT22 every time. Otherwise the microcontroller can not connect to the wifi.
If I switch on the microcontroller without sensor and connect the sensor afterwards, its works.
I have tried three different ESP32-Wroom.
Is this a normal behavior ?

Thank you in advance :slight_smile:

Yes, this kind of thing is very common.

thank you very much !

Which pin are you using for the DHT22 ?
If you had posted your code I would not have to ask

thanks for your reply, since i have to disconnect the power from the dht22 i did not thought the code would be helpfull.

I use GPIO4, but i also tested it with other pins.. sometimes it works, most of the times it does not. But it works 100% when the powersupply of the dht22 is not connected. Also tested this with 3.3V or 5V, did not make a difference.

Unfortunaly I cant post the code at the moment because im not at home but its a slighty modified version of this:

The code would have told us which pins were supposedly being used.
A schematic would have confirmed this

As I wrote, this kind of thing is all too common, as is posting in the wrong forum section. (Now corrected)

@lunkwill , your topic has been moved to a more suitable location on the forum. Installation and Troubleshooting is not for problems with (nor for advise on) your project :wink: See About the Installation & Troubleshooting category.

so, as i suspected, the code was not necessary afterall...

Thank you both very very much for your time and answers.
Sorry for posting in the wrong section, the name is terrible misleading even when you read the "troubleshooting guide" beforehand...

I wish you the very best!

It would be nice if you shared the solution :wink:

I already have, don't I ?

I am confused as to whether the problem is solved or not and if so how it was solved

Please clarify the situation

Sorry for that.
Perhaps i misunderstood "TheMemberFormerlyKnownAsAWOL" but the initial response to my general question was, that the described behavior is "very common".
Which I interpreted as there is no solution to the problem and I have to live with the workaround

The issue as described is a common issue. there are available solutions that be realized. Things like posted code and images of your project can lend to a solution.

Pretty silly to do so. Beginners often make the same mistakes, often by following popular but terrible tutorials, so certain problems are "very common".

Yeah, that was stupid from me!
I have now attached the code, alongside with a hand drawn schematic.
Sorry for that, i thought it has not anything to do with the code.

// Import required libraries
#include "WiFi.h"
#include "ESPAsyncWebServer.h"
#include <Adafruit_Sensor.h>
#include <DHT.h>

// Replace with your network credentials
const char* ssid = "**";
const char* password = "***";
unsigned long previous_time = 0;
unsigned long lag = 20000;  // 20 seconds delay

#define DHTPIN 4     // Digital pin connected to the DHT sensor

// Uncomment the type of sensor in use:
//#define DHTTYPE    DHT11     // DHT 11
#define DHTTYPE    DHT22     // DHT 22 (AM2302)
//#define DHTTYPE    DHT21     // DHT 21 (AM2301)

DHT dht(DHTPIN, DHTTYPE);

// Create AsyncWebServer object on port 80
AsyncWebServer server(80);

String readDHTTemperature() {
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  // Read temperature as Celsius (the default)
  float t = dht.readTemperature();
  // Read temperature as Fahrenheit (isFahrenheit = true)
  //float t = dht.readTemperature(true);
  // Check if any reads failed and exit early (to try again).
  if (isnan(t)) {    
    Serial.println("Failed to read from DHT sensor!");
    return "--";
  }
  else {
    Serial.println(t);
    return String(t);
  }
}

String readDHTHumidity() {
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  float h = dht.readHumidity();
  if (isnan(h)) {
    Serial.println("Failed to read from DHT sensor!");
    return "--";
  }
  else {
    Serial.println(h);
    return String(h);
  }
}

const char index_html[] PROGMEM = R"rawliteral(
<!DOCTYPE HTML><html>
<head>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.7.2/css/all.css" integrity="sha384-fnmOCqbTlWIlj8LyTjo7mOUStjsKC4pOpQbqyi7RrhN7udi9RwhKkMHpvLbHG9Sr" crossorigin="anonymous">
  <style>
    html {
     font-family: Arial;
     display: inline-block;
     margin: 0px auto;
     text-align: center;
    }
    h2 { font-size: 3.0rem; }
    p { font-size: 3.0rem; }
    .units { font-size: 1.2rem; }
    .dht-labels{
      font-size: 1.5rem;
      vertical-align:middle;
      padding-bottom: 15px;
    }
  </style>
</head>
<body>
  <h2>Humidity Sensor</h2>
  <p>
    <i class="fas fa-thermometer-half" style="color:#059e8a;"></i> 
    <span class="dht-labels">Temperature</span> 
    <span id="temperature">%TEMPERATURE%</span>
    <sup class="units">&deg;C</sup>
  </p>
  <p>
    <i class="fas fa-tint" style="color:#00add6;"></i> 
    <span class="dht-labels">Humidity</span>
    <span id="humidity">%HUMIDITY%</span>
    <sup class="units">&percnt;</sup>
  </p>
</body>
<script>
setInterval(function ( ) {
  var xhttp = new XMLHttpRequest();
  xhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
      document.getElementById("temperature").innerHTML = this.responseText;
    }
  };
  xhttp.open("GET", "/temperature", true);
  xhttp.send();
}, 10000 ) ;

setInterval(function ( ) {
  var xhttp = new XMLHttpRequest();
  xhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
      document.getElementById("humidity").innerHTML = this.responseText;
    }
  };
  xhttp.open("GET", "/humidity", true);
  xhttp.send();
}, 10000 ) ;
</script>
</html>)rawliteral";

// Replaces placeholder with DHT values
String processor(const String& var){
  //Serial.println(var);
  if(var == "TEMPERATURE"){
    return readDHTTemperature();
  }
  else if(var == "HUMIDITY"){
    return readDHTHumidity();
  }
  return String();
}

void setup(){
  // Serial port for debugging purposes
  Serial.begin(115200);

  dht.begin();
  
  // Connect to Wi-Fi
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connecting to WiFi..");
  }

  // Print ESP32 Local IP Address
  Serial.println(WiFi.localIP());

  // Route for root / web page
  server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
    request->send_P(200, "text/html", index_html, processor);
  });
  server.on("/temperature", HTTP_GET, [](AsyncWebServerRequest *request){
    request->send_P(200, "text/plain", readDHTTemperature().c_str());
  });
  server.on("/humidity", HTTP_GET, [](AsyncWebServerRequest *request){
    request->send_P(200, "text/plain", readDHTHumidity().c_str());
  });

  // Start server
  server.begin();
}
 
void loop(){
   unsigned long current_time = millis();

  // checking for WIFI connection
  if ((WiFi.status() != WL_CONNECTED) && (current_time - previous_time >= lag))
    {
    Serial.print(millis());
    Serial.println("Reconnecting to WIFI network");
    WiFi.disconnect();
    WiFi.reconnect();
    previous_time = current_time;
  }

}

What is the output voltage of the DHT22 when powered by a 5V supply ? Have you tried powering the DHT22 with 3V3 ?

The ESP32 is a 3.3V device. Applying 5V to an input pin can destroy it.

Hence my question

The output voltage is also 5V. When i was using the 3.3 V supply i regularly lost connection to the sensor and the wifi connection problem also occured from time to time..
I read somewhere (which apparently is wrong) that switching to the higher voltage output could fix the problem with the sensor.

I will test powering the sensor with 3,3V again

I changed the voltage to 3.3 V and let the ESP restart every 30 seconds for the last 20 minutes and the connection worked everytime despite the connected DHT22.
Sorry for this trivial problem, which would have been solved instantaneously if I had sent a schematic right away.
I learned a lot and will definitely do better with the next question.
Thank you very much for your time !