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