AM2302 sensor (Failed to read from DHT sensor!)

This is my code:

#include "DHT.h"

#define DHTPIN 4     // what digital pin the DHT22 is conected to
#define DHTTYPE DHT22   // there are multiple kinds of DHT sensors

DHT dht(DHTPIN, DHTTYPE);

void setup() {
  Serial.begin(9600);
  Serial.setTimeout(2000);

  // Wait for serial to initialize.
  while(!Serial) { }

  Serial.println("Device Started");
  Serial.println("-------------------------------------");
  Serial.println("Running DHT!");
  Serial.println("-------------------------------------");

}

int timeSinceLastRead = 0;
void loop() {

  // Report every 2 seconds.
  if(timeSinceLastRead > 2000) {
    // Reading temperature or humidity takes about 250 milliseconds!
    // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
    float h = dht.readHumidity();
    // Read temperature as Celsius (the default)
    float t = dht.readTemperature();
    // Read temperature as Fahrenheit (isFahrenheit = true)
    float f = dht.readTemperature(true);

    // Check if any reads failed and exit early (to try again).
    if (isnan(h) || isnan(t) || isnan(f)) {
      Serial.println("Failed to read from DHT sensor!");
      timeSinceLastRead = 0;
      return;
    }

    // Compute heat index in Fahrenheit (the default)
    float hif = dht.computeHeatIndex(f, h);
    // Compute heat index in Celsius (isFahreheit = false)
    float hic = dht.computeHeatIndex(t, h, false);

    Serial.print("Humidity: ");
    Serial.print(h);
    Serial.print(" %\t");
    Serial.print("Temperature: ");
    Serial.print(t);
    Serial.print(" *C ");
    Serial.print(f);
    Serial.print(" *F\t");
    Serial.print("Heat index: ");
    Serial.print(hic);
    Serial.print(" *C ");
    Serial.print(hif);
    Serial.println(" *F");

    timeSinceLastRead = 0;
  }
  delay(100);
  timeSinceLastRead += 100;
}

Very “original” method of counting the time intervals, not really compatible with Blynk :wink:
But aside from that (and posted unformatted code :stuck_out_tongue: ) it should work - without Blynk. If it doesn’t, and you are sure the IN/OUT pin is correct , then check your dht, choose another one…

The same code and sensor work fine with Node MCU but it doesn’t work with ESP8266 thing dev

Then I will repeat myself:

I’d add the “replace the board” as a last resort

But I do get voltage at pin number 2. Secondly, is it possible to use any other pin assignment other then pin 4 (pin 2 on esp8266) ?

Sure! You can use almost every GPIO, with some exceptions an warnings. Look at great post @wanek has prepared for us:

What you mean “voltage”? It says nothing. A voltage might be existent on damaged pin/esp too. Did you get some pulses?

What I meant earlier and I think marvin means the same is that you have your pins wrong. For example on a wemos d1 pin D2 is gpio pin 4, so if I connect a sensor to pin D2 and then read pin ‘2’ I’ll get nothing because I’m reading the wrong pin.

so 1. check if you got it right
2. if you did and it fails, try another pin
3. if that fails too try another dht (it might be broken).

However you say it does work on a nodemcu so its fairly save to assume you got the pins wrong.

1 Like

Exactly! But if the OP is sure about this…

then the esp thing is broken…
edit: I see you ninja’d my last post

I see no other options too. Well, except the DHT, which also might be broken, but it is said it works.

Also one more thing I would like to bring at your attention is that blink LED test also work on the esp board. Can it still be faulty ?

did you try everything else?
also: did you exclude each element from being faulty? (That is test each element you use separately inside a known and working configuration).
also, the fact that you get serial output from the board is a nice indicator that its not completely faulty. you just need to test each element separately until you figure out what’s going on.

another thing you can try (but it really should not make any difference) is define the input as ‘D2’ instead of ‘4’.

Ok, so this is your board, right?

Then I just looked at it’s schematics:

Schowek-1

Are you STILL sure it is GPIO4 (as you had defined)??

One more picture:

Here is the summary:
I connected ground with ground, power with 3v3 and data pin with pin 2 (#define DHTPIN 4).
Then, I tried using ground with ground, power with 3v3 and data pin with pin 4 (#define DHTPIN 2).
Then I tried ground with ground, power with 3v3 and data pin with pin 3 (#define DHTPIN 5).
In all above cases I got the same error message on serial monitor. What else should I try ?

And connecting the IN/OUT to 2 and #define DHTPIN 2 not working?

yeah it’s not working.

What you selected as a target board?

Generic ESP8266 Module

I did try that but still no progress.

Man this is quite the struggle.

maybe you should first go back to the basics and simply attach a LED to the port and see if it lights up when you set it high. Preferably let it blink so you know for sure it works and both HIGH/LOW are good

edit: Marvin was so kind to work the above suggestion out for you in detail :smile: