2

This question seems to have come up a few times, but I'm attempting to read from a DHT22 with a ESP-12e (ESP8266).

Wiring diagram is here: Wiring Diagram

Code is here:

status, temp, humi, temp_dec, humi_dec = dht.read(1)
if status == dht.OK then
    -- Integer firmware using this example
    print(string.format("DHT Temperature:%d.%03d;Humidity:%d.%03d\r\n",
          math.floor(temp),
          temp_dec,
          math.floor(humi),
          humi_dec
    ))

    -- Float firmware using this example
    print("DHT Temperature:"..temp..";".."Humidity:"..humi)

elseif status == dht.ERROR_CHECKSUM then
    print( "DHT Checksum error." )
elseif status == dht.ERROR_TIMEOUT then
    print( "DHT timed out." )
end

I've found articles that state the GPIO numbers don't match up with the pins on the NodeMCU board, and that you should use the pin on the board, which will be internally mapped to the real GPIO pin number (https://nodemcu.readthedocs.io/en/dev/en/modules/gpio/) . So, I've used what is labled as D1 on the board, which actually maps to GPIO5. I've tried both "1" and "5" for the pin number in the code and it makes no difference. I've also tried other pins, to no avail.

When the code attempts to run, I get the following error message:

> dofile("tempMon")
DHT timed out.

Other than a possible hardware issue with the sensor, is there anything that I could possibly be missing?

  • Is your pull-up 1k or 10k? I can't tell from the picture if it's brown-black-red or brown-black-orange. Also I see others wiring it to 5v not 3v3. – leetibbett Sep 20 '16 at 10:05

4 Answers4

1

IMO, it looks like hardware issue but to be sure I wrote a simple program in Arduino IDE for your ESP8266 that should give the answer whether this is software or hardware issue.

The only thing the program does is outputing temperatures of DHT22 sensors connected to pin 1, 5 or 14.

#include <DHT.h>

DHT dht1(1, DHT22, 11);
DHT dht5(5, DHT22, 11);
DHT dht14(14, DHT22, 11);

float temp;

void setup() {
  Serial.begin(115200);
  dht1.begin();
  dht5.begin();
  dht14.begin();
}

void loop() {
  temp = dht1.readTemperature(false);
  Serial.print("DHT22 on pin1 - temp: ");
  Serial.println(temp);

  temp = dht5.readTemperature(false);
  Serial.print("DHT22 on pin5 - temp: ");
  Serial.println(temp);

  temp = dht14.readTemperature(false);
  Serial.print("DHT22 on pin14 - temp: ");
  Serial.println(temp);
}

I compiled binary file for you so that you don't have to download Arduino IDE and compile it again: dht22test.bin

Defozo
  • 2,946
  • 6
  • 32
  • 51
  • I'm going to guess it's a hardware issue at this point. Whenever I run the sketch, I get garbage output to the console. Upon investigation, it starts spewing garbage after the first time that dht1.begin() is called. I also wrote a lua script that looped through all the pins looking for a DHT message and got nothing there either. – Michael Wheeler Sep 20 '16 at 00:12
1

I tried the same example with the pull-up resistor and without it and received the same error in the two cases.

In order to be sure that no hardware issue with the sensor, I built up a code for arduino and using the same sensor everything went ok (consul displayed the temperate and humidity).

Omran
  • 11
  • 1
0

Has to be a hardware issue in the DHT22 module. Going to order another module and try again.

0

I've dug into this situation and found out that I'm having the same problem (and actually stepped on a few different errors). Came 4 years later but further information is always helpfull for future reference.

Regarding you comment above:

I'm going to guess it's a hardware issue at this point. Whenever I run the sketch, I get garbage output to the console. Upon investigation, it starts spewing garbage after the first time that dht1.begin() is called. I also wrote a lua script that looped through all the pins looking for a DHT message and got nothing there either. – Michael Wheeler Sep 20 '16 at 0:12

Note: Garbage Output usually means that Serial Bit Rates are different in your Serial Initialization and Console Output. Check it then if you still have problems; give the Troubleshooter bellow a try.


Troubleshooting:

1. Garbage Output : Receiving Garbage Output Continuously

- Set Bit Rate to 115200.
- Check Serial Bit Rates (Code and Serial Monitor).
- Delay DHT Initialization for: 2 Seconds (2000ms).
- Check Wiring.

2. Sensor Output is nan or 0

- Check Wiring.
- Test with Different Micro-Controller.
- See: 2.1 Bellow

2.1 Testing With Different Micro Controller

- If the Test Fails with a Different Controller Consider: 

       a) Possibly using the Wrong Library.
       b) Possible Defective Module.

**Note:** For ESP32 and ESP8266 you need use a different library.
Include "DHTesp.h" instead of "DHT.h".
See code example bellow.

- If it Succeeds with different Controller: 

       a) DHT22 is not Compatible with Esp8266.

**Note:** I couldn't find a viable Pin that actually worked on my
          ESP8266 (NodeMCU).
          I'm relying on my own experience; and considering some people
          have managed to make it work; consider that perhaps a
          different DHT22 sensor model or ESP8266 could work.

3. No Output

- Usually Error in Code.
- Possible Short Circuit in Module and/or Controller.  
enter code here


Remarks : Micro Controllers and Pins for DHT

  • Uno: Pin D02
  • Nano: Pin D02
  • ESP32: RX2 (Pin17)
  • ESP8266: None Worked (Try: 1,2,4,5,7)



Code for each Micro Controller (Including ESP8266)

Arduino Uno / Nano

#include "DHT.h"

/* Sensor Type */
#define dhtType DHT22

/* Define DHT22 Pin on Arduino */
#define dhtPin 0 // ESP8266 D1 (GPIO 5)

/* Configure DHT Pin and Model */
DHT dht(dhtPin, dhtType);

/* Initialize DHT22 Sensor */
void Init_DHT22()
{ 
  dht.begin();

  // Wait a little for the Sensor to Start and Calibrate.
  delay(1000);
}

/* Read and Retrieve Temperature from DHT Pin. */
float GetTemperature() { return dht.readTemperature(); }

/* Read and Retrieve Humidity from DHT Pin. */
float GetHumidity() { return dht.readHumidity(); }

ESP32

#include "DHTesp.h"

#ifndef ESP32
#pragma message(THIS EXAMPLE IS FOR ESP32 ONLY!)
#error Select ESP32 board.
#endif

DHTesp dht;

/** Pin Number for DHT Data Pin */
#define dhtPin 17

void DHT22_Init() 
{
  // Initialize temperature sensor
  dht.setup(dhtPin, DHTesp::DHT22);
}

float Temperature() { return dht.getTemperature(); }

float Humidity() { return dht.getHumidity(); }

ESP8266

/*
 * Common Pins used for on ESP8266 are: D1, D2; D4 (None Worked for Me).
 */

#include "DHTesp.h"

#ifdef ESP32
#pragma message(THIS EXAMPLE IS FOR ESP8266 ONLY!)
#error Select ESP8266 board.
#endif

#define dhtPin 4
#define dhtType DHT22

DHTesp dht;

void DHT22_Init()
{
  dht.setup(dhtPin, DHTesp::dhtType);
}

float Temperature()
{
  delay(dht.getMinimumSamplingPeriod());
  return dht.getTemperature();
}

float Humidity()
{
  delay(dht.getMinimumSamplingPeriod());
  return dht.getHumidity();
}

Best Regards