DHT22 returns temp = 0, hum = 6553

Hi. I’m trying to get my photon to publish the temp and hum from my DHT22 to the cloud. It’s publishing values of temperature of 0 and humidity of 6553%. It’s not cold or raining where the device is.

Here’s the code:

// This #include statement was automatically added by the Particle IDE.
#include <Adafruit_DHT.h>

// This #include statement was automatically added by the Particle IDE.
#include <Adafruit_DHT/Adafruit_DHT.h>

// DHT parameters
#define DHTPIN 5
#define DHTTYPE DHT22

// Variables
int temperature;
int humidity;


// DHT sensor
DHT dht(DHTPIN, DHTTYPE);

void setup() {
    
    // Start DHT sensor
    dht.begin();
}

void loop() {
    
    delay(5000);
    // Humidity measurement
    temperature = dht.getTempCelcius();
    
    // Humidity measurement
    humidity = dht.getHumidity();
    
    // Publish data
    Particle.publish("temperature", String(temperature), PRIVATE);
    delay(5000);
    Particle.publish("humidity", String(humidity) + "%", PRIVATE);
    delay(5000);
    
   
}

The DHT22 has an integrated resistor, (so it’s only got 3x pins). LH pin is “+” and I have it connected to 3V3, middle pin “out” is to D5 and RH pin “-” is to GND.
Here’s a photo of the setup:

Help would be appreciated. Thanks!

@Blacksmithash can you indicate which DHT22 module you are using?

You declare temperature and humidity as int but dht.getTempCelcius() and dht.getHumidity() return float. This will definitely gum up your values so change your declarations to float.

Changed them to floats. Same thing:

// This #include statement was automatically added by the Particle IDE.
#include <Adafruit_DHT.h>

// This #include statement was automatically added by the Particle IDE.
#include <Adafruit_DHT/Adafruit_DHT.h>

// DHT parameters
#define DHTPIN 5
#define DHTTYPE DHT22

// Variables
float temperature;
float humidity;


// DHT sensor
DHT dht(DHTPIN, DHTTYPE);

void setup() {
    
    // Start DHT sensor
    dht.begin();
}

void loop() {
    
    delay(5000);
    // Humidity measurement
    temperature = dht.getTempCelcius();
    
    // Humidity measurement
    humidity = dht.getHumidity();
    
    // Publish data
    Particle.publish("temperature", String(temperature), PRIVATE);
    delay(5000);
    Particle.publish("humidity", String(humidity) + "%", PRIVATE);
    delay(5000);
    
   
}

@Blacksmithash, can you answer this question:

DHT22 AM2302 Digital Temperature Humidity Sensor Module For Arduino

@Blacksmithash, try the example that comes with the Adafruit_DHT library first, making sure you specify the DTHPIN as D5. It things still don’t work, try a different pin like D6 for example.

@peekay123 tried a couple of different pins. Also tried using the e.g. in Adafruit_DHT and it’s saying “fail to read” with that code. Might be a faulty sensor - I’ll try another one unless you think there might be something wrong with the following (incidentally, i commented out all the serial printing because it’s not working atm so I only have the console).

// This #include statement was automatically added by the Particle IDE.
#include <Adafruit_DHT.h>

// Example testing sketch for various DHT humidity/temperature sensors
// Written by ladyada, public domain

#define DHTPIN D2     // what pin we're connected to

// Uncomment whatever type you're using!
//#define DHTTYPE DHT11		// DHT 11 
#define DHTTYPE DHT22		// DHT 22 (AM2302)
//#define DHTTYPE DHT21		// DHT 21 (AM2301)

// Connect pin 1 (on the left) of the sensor to +5V
// Connect pin 2 of the sensor to whatever your DHTPIN is
// Connect pin 4 (on the right) of the sensor to GROUND
// Connect a 10K resistor from pin 2 (data) to pin 1 (power) of the sensor

DHT dht(DHTPIN, DHTTYPE);
int loopCount;

void setup() {
	//Serial.begin(9600); 
    //Serial.println("DHT22 test!");
	Particle.publish("state", "DHT22 test start");
	Particle.publish("test1");

	dht.begin();
	loopCount = 0;
	delay(2000);
}

void loop() {
// Wait a few seconds between measurements.
	delay(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.getHumidity();
// Read temperature as Celsius
	float t = dht.getTempCelcius();
// Read temperature as Farenheit
	float f = dht.getTempFarenheit();
	

	
// 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!");
		Particle.publish("Failed to read from  DHT sensor!");
		Particle.publish("test3");
		return;
	}

// Compute heat index
// Must send in temp in Fahrenheit!
	float hi = dht.getHeatIndex();
	float dp = dht.getDewPoint();
	float k = dht.getTempKelvin();
	
	Particle.publish("test4");
	Particle.publish("Temp", String(t), PRIVATE);
	Particle.publish("Hum", String(h), PRIVATE);
	
	
/*
	Serial.print("Humid: "); 
	Serial.print(h);
	Serial.print("% - ");
	Serial.print("Temp: "); 
	Serial.print(t);
	Serial.print("*C ");
	Serial.print(f);
	Serial.print("*F ");
	Serial.print(k);
	Serial.print("*K - ");
	Serial.print("DewP: ");
	Serial.print(dp);
	Serial.print("*C - ");
	Serial.print("HeatI: ");
	Serial.print(hi);
	Serial.println("*C");
	Serial.println(Time.timeStr());
	//String timeStamp = Time.timeStr();
	*/
	Particle.publish("readings", String::format("{\"Hum(\%)\": %4.2f, \"Temp(°C)\": %4.2f, \"DP(°C)\": %4.2f, \"HI(°C)\": %4.2f}", h, t, dp, hi));
	delay(10000);
	loopCount++;
	if(loopCount >= 6){
	  	  delay(1000);
	 //System.sleep(SLEEP_MODE_DEEP, 300);  
	}
	Particle.publish("test4");
	
}

@Blacksmithash, the way you have the Particle.publish() statement will exceed the one-per-second rate and the Particle cloud will stop further publishes. I suggest commenting out the Particle.publish() statements and KEEPING the Serial.print statements instead.

I don’t see the data pin being pulled to 3.3V. Pretty sure that’s a requirement and could explain the issues. Give that a try with something like a 10K resistor.

@ctmorrison,

Hmmm…I know I always have a 10K pull-up resistor and have had no issues with DHT22s and have many in the field.

@ctmorrison, the DHT22 is on a board which has the pull-up on it and it is definitely required.

@peekay123, I’m confused by your comment. You appear to be disputing my comment about the need for an external pull-up. However, from Adafruit’s product description: “Comes with a 4.7K - 10K resistor, which you will want to use as a pullup from the data pin to VCC.”

@ctmorrison, LOL. I agree that a pull-up is needed but the OP’s DHT22 module comes with on built-in :wink:

1 Like

@peekay123 Serial.print statements have never worked for me. Looked into it and it’s probably something to do with the photon driver (which I don’t think I can manage because I only have a work computer and no privileges to make the changes). I’ve put in some delays between the Particle.publish() statements, return the serial.print ones which will be useless (to me) anyway.

I also put in some test lines in the code to see where it gets stuck - gets stuck in the nan part of the code.

I’ve tried different pins and a different DHT22 with the pull-up 10k resistor.

Maybe it’s the photon itself?

Here’s my code:

// This #include statement was automatically added by the Particle IDE.
#include <Adafruit_DHT.h>



// Example testing sketch for various DHT humidity/temperature sensors
// Written by ladyada, public domain

#define DHTPIN 3     // what pin we're connected to

// Uncomment whatever type you're using!
//#define DHTTYPE DHT11		// DHT 11 
#define DHTTYPE DHT22		// DHT 22 (AM2302)
//#define DHTTYPE DHT21		// DHT 21 (AM2301)

// Connect pin 1 (on the left) of the sensor to +5V
// Connect pin 2 of the sensor to whatever your DHTPIN is
// Connect pin 4 (on the right) of the sensor to GROUND
// Connect a 10K resistor from pin 2 (data) to pin 1 (power) of the sensor

DHT dht(DHTPIN, DHTTYPE);

void setup() {
	Serial.begin(9600); 
	Serial.println("DHT22 test!");
	Particle.publish("Test1");

	dht.begin();
}

void loop() {
// Wait a few seconds between measurements.
	delay(2000);
	Particle.publish("Test2");
	delay(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.getHumidity();
// Read temperature as Celsius
	float t = dht.getTempCelcius();
// Read temperature as Farenheit
	float f = dht.getTempFarenheit();
  
// 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!");
		delay(2000);
		Particle.publish("Failed to read from DHT sensor!");
		delay(2000);
		return;
	}
    
    delay(2000);
    Particle.publish("Test3");
    delay(2000);
    Particle.publish("Temp: ",String(t),PRIVATE);
    
// Compute heat index
// Must send in temp in Fahrenheit!
	float hi = dht.getHeatIndex();
	float dp = dht.getDewPoint();
	float k = dht.getTempKelvin();

	Serial.print("Humid: "); 
	Serial.print(h);
	Serial.print("% - ");
	Serial.print("Temp: "); 
	Serial.print(t);
	Serial.print("*C ");
	Serial.print(f);
	Serial.print("*F ");
	Serial.print(k);
	Serial.print("*K - ");
	Serial.print("DewP: ");
	Serial.print(dp);
	Serial.print("*C - ");
	Serial.print("HeatI: ");
	Serial.print(hi);
	Serial.println("*C");
	Serial.println(Time.timeStr());
	
}

It was this version of the board that influenced my purchase - I have little experience in electronics.
It only has 3 pins: +ive, out, -ve: