Hi, I’m new to the forum and hoping someone can lend me a hand.
I have a DHT22 hooked up to it pretty much the same way described here (only I’m connected to D2). I have tried to use both AdaFruit’s and PietteTech_DHT with no success. Values always come back as 0 or NaN.
I have tried the following code.
#include "Adafruit_DHT/Adafruit_DHT.h"
// Define Pins
#define DHTPIN 2 // what pin we're connected to
// Setup Sensor
#define DHTTYPE DHT22 // DHT 22 (AM2302)
DHT dht(DHTPIN, DHTTYPE);
void setup()
{
Serial.begin(9600);
dht.begin(); // Startup the sensor
Serial.println("---------------");
}
void loop(){
// hold up
delay(2000); // let everything power up for a bit
// grab some data
float humidity = dht.getHumidity();
float tempf = dht.getTempFarenheit();
float dewptc = dht.getDewPoint();
float dewptf = (dewptc* 9 / 5 + 32);
// Print to console for debugging, fun, and health.
Serial.print("\nRetrieving information from sensor: ");
Serial.print("Read sensor: ");
Serial.print("Humidity: ");
Serial.print(humidity);
Serial.print("% - ");
Serial.print("Temp: ");
Serial.print(tempf);
Serial.print("*F ");
Serial.print("DewP: ");
Serial.print(dewptf);
//Hold up
delay(1000);
}
And also tried PietteTech’s example as follows:
#include "PietteTech_DHT/PietteTech_DHT.h"
// system defines
#define DHTTYPE DHT22 // Sensor type DHT11/21/22/AM2301/AM2302
#define DHTPIN D2 // Digital pin for communications
#define DHT_SAMPLE_INTERVAL 2000 // Sample every two seconds
//declaration
void dht_wrapper(); // must be declared before the lib initialization
// Lib instantiate
PietteTech_DHT DHT(DHTPIN, DHTTYPE, dht_wrapper);
// globals
unsigned int DHTnextSampleTime; // Next time we want to start sample
bool bDHTstarted; // flag to indicate we started acquisition
int n; // counter
void setup()
{
Serial.begin(9600);
while (!Serial.available()) {
Serial.println("Press any key to start.");
delay (1000);
}
Serial.println("DHT Example program using DHT.acquire and DHT.aquiring");
Serial.print("LIB version: ");
Serial.println(DHTLIB_VERSION);
Serial.println("---------------");
DHTnextSampleTime = 0; // Start the first sample immediately
}
// This wrapper is in charge of calling
// mus be defined like this for the lib work
void dht_wrapper() {
DHT.isrCallback();
}
void loop()
{
// Check if we need to start the next sample
if (millis() > DHTnextSampleTime) {
if (!bDHTstarted) { // start the sample
Serial.print("\n");
Serial.print(n);
Serial.print(": Retrieving information from sensor: ");
DHT.acquire();
bDHTstarted = true;
}
if (!DHT.acquiring()) { // has sample completed?
// get DHT status
int result = DHT.getStatus();
Serial.print("Read sensor: ");
switch (result) {
case DHTLIB_OK:
Serial.println("OK");
break;
case DHTLIB_ERROR_CHECKSUM:
Serial.println("Error\n\r\tChecksum error");
break;
case DHTLIB_ERROR_ISR_TIMEOUT:
Serial.println("Error\n\r\tISR time out error");
break;
case DHTLIB_ERROR_RESPONSE_TIMEOUT:
Serial.println("Error\n\r\tResponse time out error");
break;
case DHTLIB_ERROR_DATA_TIMEOUT:
Serial.println("Error\n\r\tData time out error");
break;
case DHTLIB_ERROR_ACQUIRING:
Serial.println("Error\n\r\tAcquiring");
break;
case DHTLIB_ERROR_DELTA:
Serial.println("Error\n\r\tDelta time to small");
break;
case DHTLIB_ERROR_NOTSTARTED:
Serial.println("Error\n\r\tNot started");
break;
default:
Serial.println("Unknown error");
break;
}
Serial.print("Humidity (%): ");
Serial.println(DHT.getHumidity(), 2);
Serial.print("Temperature (oC): ");
Serial.println(DHT.getCelsius(), 2);
Serial.print("Temperature (oF): ");
Serial.println(DHT.getFahrenheit(), 2);
Serial.print("Temperature (K): ");
Serial.println(DHT.getKelvin(), 2);
Serial.print("Dew Point (oC): ");
Serial.println(DHT.getDewPoint());
Serial.print("Dew Point Slow (oC): ");
Serial.println(DHT.getDewPointSlow());
n++; // increment counter
bDHTstarted = false; // reset the sample flag so we can take another
DHTnextSampleTime = millis() + DHT_SAMPLE_INTERVAL; // set the time for next sample
}
}
}
None of them ever work. The first one only gives me 0’s or NaN’s, and Piette’s never makes it past “Retrieving information from sensor”.
I was thinking it could be something to do with my DHT22, so hooked it up to an Arduino I had laying around and it gave me readings as I expected.
I have been through loads of other threads here, and tried many different things to no avail, so hoping someone can shed me a light here.
Thanks