AM2302 sensor (Failed to read from DHT sensor!)

Hello,
I am using AM2302 sensor with ESP8266 Thing Dev board. My code is below:

 *** Example for reading temperature and humidity**
** * using the DHT22 and ESP8266**
** ***
** * Copyright (c) 2016 Losant IoT. All rights reserved.**
** * https://www.losant.com**
** */**

**#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;**
**}**

However, the issue is that each time I upload the code and open serial monitor it gives me this error:

Failed to read from DHT sensor!
Failed to read from DHT sensor!
Failed to read from DHT sensor!
Failed to read from DHT sensor!
Failed to read from DHT sensor!
Failed to read from DHT sensor!
Failed to read from DHT sensor!
Failed to read from DHT sensor!
Failed to read from DHT sensor!
Failed to read from DHT sensor!
Failed to read from DHT sensor!
Failed to read from DHT sensor!
Failed to read from DHT sensor!
Failed to read from DHT sensor!
Failed to read from DHT sensor!
Failed to read from DHT sensor!

Exception (29):
epc1=0x4000e1b2 epc2=0x00000000 epc3=0x00000000 excvaddr=0x00000000 depc=0x00000000

ctx: sys 
sp: 3ffffcc0 end: 3fffffb0 offset: 01a0

>>>stack>>>
3ffffe60:  00000000 3fffbfb4 0000002c 401007a1  
3ffffe70:  00000000 3fffbfb4 40231e50 40107370  
3ffffe80:  00000005 02c61123 60000600 402175a4  
3ffffe90:  0000004c 40217eaf 00000018 0000000c  
3ffffea0:  00000001 00001388 3ff20200 ffffffd3  
3ffffeb0:  3ffffed0 4021bb2f 3ffed4c4 00000005  
3ffffec0:  4020f452 00000005 3ffedc70 00000064  
3ffffed0:  4020b1dd 3ffedbe4 3ffe83b8 3ffe83b8  
3ffffee0:  3fff019f 3fffc6fc 3ffefd9c 0000000c  
3ffffef0:  4021d78b 3ffedb98 3ffedc70 02c61123  
3fffff00:  00000001 4020c458 3ffed548 4010673c  
3fffff10:  4021d440 4021d414 0000001c 40100774  
3fffff20:  4021f1c5 4021d414 00000064 4010732c  
3fffff30:  3ffed578 4021f18c 3ffedbb0 3ffe8900  
3fffff40:  4021d40a 3ffed4c4 3ffe8900 00000000  
3fffff50:  4021d3b1 3ffedbb0 3ffe8900 02c61123  
3fffff60:  4021d864 40205ed5 3ffe8d6c 3ffe8900  
3fffff70:  40205ed5 60000600 00000000 40205ed5  
3fffff80:  40205f1a 3fffdab0 00000000 3fffdcb0  
3fffff90:  3ffe8918 3fffdad0 3ffeebd4 40203063  
3fffffa0:  40000f49 40000f49 3fffdab0 40000f49  
<<<stack<<<
c_āø®c_āø®

I know pin connection of 4 on arduino is pin number 2 on esp8266. I did pin connection appropriately. I have attached picture of my connection here.

Please help my find out what is wrong and why I am not getting readings on serial monitor ? Thank You!!

I only see one big comment section. What exactly is your code?

also did you triple check whether youā€™re using the correct pin? both on the board and in youā€™re code?

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