[SOLVED] DHT22 issue with Photon

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

1 Like

hey there, welcome to the forums, glad to have you!

As far as the DTH22 goes, go for the piettetech. It has shown to be more reliable overall.

You say you’ve hooked it up to D3, yet you define the pin as:

#define DHTPIN   D2         	    // Digital pin for communications

Have another look at that, might just fix it :smile:

Both sketches use pin2 but you say you are using pin3. I trust that’s just a typo.

Have you tried powering the sensor with 5v?

Thanks @Moors7.

My apologies, I tried to be helpful on my typing and entered the wrong port :smile: . I am indeed using D2.

Thread updated

It is a type yeah… Updated…

Tried to connect v+ on the sensor to VIN 4.8V according to this (section 2.2 power), but also got nothing.

Hmm, I copied your exact code (the piettetech one) on a fresh photon, and it’s working perfectly, powered from Vin. Could you double-check your wiring/resistor, just to be sure?

Thanks for doing that, really appreciate it. I’ve been looking at this for so long I feel like there’s probably something blatantly obvious that just I can’t see :smile:

Took a picture to make sure I’m not going crazy. Seems alright to me. Here it is.

The wiring looks okay to me, and the code is known to be working. The only thing I have differently is the resistor. I’m using a 10k one. If you’ve got one of those on hand, it’d be worthwhile trying. I’ll give a 1k one a try over here, and see what it does :smile:

Aw the damn resistor! Now it works! Brown, Black, Organge != Brow, Black, Red!

Thank you so much @Moors7, it’s all good now even on 3v3! Really appreciate your help

4 Likes

Glad to hear you’ve got it working :smile:!

1 Like

Hello. I have almost the same issue. It works fine, but some times only on the temp it shows NaN and then it goes back to normal. And it only happens with the photon. I tried diferents photons and it does the same thing. But if I connect a Spark it works fine with no errors. Sometimes it works fine meaning that it shows the temp and the hum correct. But after a while on the temp, it shows NaN (Only on the Temp). And is weird because it doesn’t have a patter. It can happen at any time. Sometimes it works fine for 7 hours and I get one NaN on the temp. And then it goes to normal.

If you’re not already, would you mind giving the Piettetech library a try? It’s been very reliable for the past couple of months. Might just make the difference :smile:

2 Likes

You know I just noticed I didn’t have the dht.begin(); on the setup . You think that could be the problem?

1 Like

I’d think so. Doesn’t hurt to add it, does it ;)?

1 Like

Yeah, I just add it. Lets see what happens. Ill keep you post it. Finger Crossed… :grinning:

1 Like

I just got a NaN on the temp. Im going to try the piettetech. Lets see…

I just want to update you Guys for the record. It worked perfect using what Moors7 recommended. piettetech.
It’s being running for the past 24 hours and no NaN so far. It looks that ADAFRUIT_DHT doesn’t work well with the new Photons. Thank You for all the Help…
:wink:

2 Likes

I’m using exactly the piettetech source code from mplacoma/July 3. Wiring as in pic above, resistor that came with the dht22 from adafruit. The code compiles & loads and gets to the "Retrieving information from sensor: " output and hangs. This is with a recently received Photon. Any suggestions. I really need this.

Even though the resistor came with the sensor, do make sure it’s the correct value. It might make the difference.

I had a wiring mistake (off by 1 pin). Fixed that. Tried included resistor also a 1k. Same output:

Press any key to start.
DHT Example program using DHT.acquire and DHT.aquiring
LIB version: 0.2

0: Retrieving information from sensor: Read sensor: Error

.ISR time out error
Humidity (%): -2.00
Temperature (oC): -2.00
Temperature (oF): -2.00
Temperature (K): -2.00
Dew Point (oC): -2.00
Dew Point Slow (oC): -2.00

Setup pic attached.

1 Like