Problem with DHT22

Hi,
I have written a program that uses a DHT sensor. But I have a problem, before I used a DHT11 and all of it worked good. Now I have changed the sensor with DHT22 and an error occured.
In particular there is this error: DHTLIB_ERROR_TIMEOUT.
I post under an example that, as I told above, worked with DHT11 but not with DHT22.

#include <dht.h>

dht DHT;

#define DHT11_PIN 4
#define DHT21_PIN 5
#define DHT22_PIN 13

void setup()
{
  Serial.begin(9600);
  Serial.println("DHT TEST PROGRAM ");
  Serial.print("LIBRARY VERSION: ");
  Serial.println(DHT_LIB_VERSION);
  Serial.println();
  Serial.println("Type,\tstatus,\tHumidity (%),\tTemperature (C)");
}

void loop()
{
  // READ DATA
  Serial.print("DHT22, \t");
  int chk = DHT.read22(DHT22_PIN);
  switch (chk)
  {
    case DHTLIB_OK:  
		Serial.print("OK,\t"); 
		break;
    case DHTLIB_ERROR_CHECKSUM: 
		Serial.print("Checksum error,\t"); 
		break;
    case DHTLIB_ERROR_TIMEOUT: 
		Serial.print("Time out error,\t"); 
		break;
    default: 
		Serial.print("Unknown error,\t"); 
		break;
  }
  // DISPLAY DATA
  Serial.print(DHT.humidity, 1);
  Serial.print(",\t");
  Serial.println(DHT.temperature, 1);

  delay(1000);


//    // READ DATA
//  Serial.print("DHT21, \t");
//  chk = DHT.read21(DHT21_PIN);
//  switch (chk)
//  {
//    case DHTLIB_OK:  
//		Serial.print("OK,\t"); 
//		break;
//    case DHTLIB_ERROR_CHECKSUM: 
//		Serial.print("Checksum error,\t"); 
//		break;
//    case DHTLIB_ERROR_TIMEOUT: 
//		Serial.print("Time out error,\t"); 
//		break;
//    default: 
//		Serial.print("Unknown error,\t"); 
//		break;
//  }
//  // DISPLAY DATA
//  Serial.print(DHT.humidity, 1);
//  Serial.print(",\t");
//  Serial.println(DHT.temperature, 1);
//
//  delay(1000);
//
//  // READ DATA
//  Serial.print("DHT11, \t");
//  chk = DHT.read11(DHT11_PIN);
//  switch (chk)
//  {
//    case DHTLIB_OK:  
//		Serial.print("OK,\t"); 
//		break;
//    case DHTLIB_ERROR_CHECKSUM: 
//		Serial.print("Checksum error,\t"); 
//		break;
//    case DHTLIB_ERROR_TIMEOUT: 
//		Serial.print("Time out error,\t"); 
//		break;
//    default: 
//		Serial.print("Unknown error,\t"); 
//		break;
//  }
// // DISPLAY DATA
//  Serial.print(DHT.humidity,1);
//  Serial.print(",\t");
//  Serial.println(DHT.temperature,1);

//  delay(1000);
}
//
// END OF FILE
//

I use this library:

http://playground.arduino.cc/Main/DHTLib

Try anothere library,i am working with " DHT22.h " library and is working great, 7 meter long wire.
Remember to connect a 4.7K resistor between VCC and the data pin (strong pullup) :wink: Goodluck

Can you give me a link of this library?

I had used this same library to DHT22 and DHT11 and it worked fine to me.
One of my test sketches was:

#include <dht.h>
#include <LiquidCrystal.h>

dht DHT22;

#define DHTPIN 28

// select the pins used on the LCD panel
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);

void setup()
{
  lcd.begin(16, 2);              // start the library
  
  Serial.begin(115200);
  Serial.println("DHT11 TEST PROGRAM ");
  Serial.print("LIBRARY VERSION: ");
  Serial.println(DHT_LIB_VERSION);
  Serial.println();
}

void loop()
{
  Serial.println("\n");

  int chk = DHT22.read22(DHTPIN);

  switch (chk)
  {
    case DHTLIB_OK:
       lcd.setCursor(15,0);
       lcd.print(" ");
       break;
       
    case DHTLIB_ERROR_CHECKSUM:
    case DHTLIB_ERROR_TIMEOUT:
    default:
       lcd.setCursor(15,0);
       lcd.print("*");
    
  }

  lcd.setCursor(0,0);
  lcd.print("Humi:");
  lcd.print((float)DHT22.humidity, 1);
  lcd.print("%  ");
  
  lcd.setCursor(0,1);
  lcd.print("Temp:");
  lcd.print((float)DHT22.temperature, 1);
  lcd.write(0xDF);
  lcd.print("C  ");

  delay(500);
}
[\code]

Can you tell which IDE you are using
And which board you are using?

can you post the exact output you get?

I had used this same library to DHT22 and DHT11 and it worked fine to me.

My program is similar to yours and I use the same library, but it doesn't work.

here it is : GitHub - nethoncho/Arduino-DHT22: Arduino library for the DHT22 humidity and temperature sensor

I use IDE Arduino 0022 with Arduino Uno.

My output is:

DHT TEST PROGRAM
LIBRARY VERSION: 0.1.10

Type, status, Humidity (%), Temperature (C)
DHT22, Time out error, -999.0, -999.0
DHT22, OK, 72.0, 22.0
DHT22, OK, 72.0, 22.0
DHT22, OK, 71.9, 22.0
DHT22, OK, 72.0, 22.0
DHT22, OK, 71.9, 22.0
DHT22, OK, 71.9, 22.0
DHT22, OK, 72.0, 22.0
DHT22, OK, 71.9, 22.0
DHT22, OK, 72.0, 22.0
DHT22, OK, 72.0, 22.0
DHT22, OK, 72.0, 22.0
DHT22, OK, 71.9, 22.0
DHT22, OK, 72.0, 22.0
DHT22, OK, 72.0, 22.0
DHT22, OK, 72.1, 22.0
DHT22, OK, 72.1, 22.0
DHT22, OK, 72.1, 22.0
DHT22, OK, 72.1, 22.0
DHT22, Time out error, -999.0, -999.0
DHT22, OK, 72.1, 22.0
DHT22, OK, 72.1, 22.0
DHT22, OK, 72.1, 22.0
DHT22, OK, 72.1, 22.0
DHT22, Time out error, -999.0, -999.0
DHT22, OK, 72.1, 22.0
DHT22, Time out error, -999.0, -999.0
DHT22, OK, 72.0, 22.0
DHT22, Time out error, -999.0, -999.0
DHT22, Time out error, -999.0, -999.0
DHT22, OK, 72.5, 22.0
DHT22, Time out error, -999.0, -999.0
DHT22, Time out error, -999.0, -999.0
DHT22, Time out error, -999.0, -999.0
DHT22, Time out error, -999.0, -999.0
DHT22, Time out error, -999.0, -999.0
DHT22, Time out error, -999.0, -999.0
DHT22, Checksum error, 66.4, 22.0
DHT22, Time out error, -999.0, -999.0
DHT22, Time out error, -999.0, -999.0
DHT22, Time out error, -999.0, -999.0
DHT22, Time out error, -999.0, -999.0
DHT22, Time out error, -999.0, -999.0
DHT22, Time out error, -999.0, -999.0
DHT22, Time out error, -999.0, -999.0
DHT22, Time out error, -999.0, -999.0
DHT22, Time out error, -999.0, -999.0
DHT22, Time out error, -999.0, -999.0

here it is : GitHub - nethoncho/Arduino-DHT22: Arduino library for the DHT22 humidity and temperature sensor

This is my output:

DHT22 Library Demo
Requesting data...Polled to quick 
Requesting data...BUS Hung 
Requesting data...BUS Hung 
Requesting data...Not Present 
Requesting data...Not Present 
Requesting data...Not Present 
Requesting data...BUS Hung 
Requesting data...BUS Hung 
Requesting data...Not Present 
Requesting data...BUS Hung 
Requesting data...Not Present 
Requesting data...BUS Hung 
Requesting data...BUS Hung

I think that you are repeating the readings to the sensor very fast. Try to increase the pooling time and see what happens.

+1 for advice luisilva
delay(500); is outside the specs of the datasheet, it might work for some sensors but not all.
use 1000 / 2000

LIBRARY VERSION: 0.1.10
upgrade to the 0.1.13 version, latest stable version

IDE 0.22
upgrade to 1.06 version (0.22 is quite stable but also a bit old)

this is part of the library... not sure if it is in all versions:

delay(dht.getMinimumSamplingPeriod())

Read the Data sheet and you'll find that the minimum sampling speed is ~ 2 sec.. 2 seconds will return errors but 2.5 sec will be a little more stable.
The point is that the RH sensor is a capacitive type similar to the HS1101 and has a very finite response time.. RH is a relatively slowly changing variable and it is of little importance to sample it continuously in loop().. Thus the 2000 ms delay at the end of loop
I made a Temp / RH sensor with a DHT22, A tiny 85 and an HC06 BT radio and it worked very well... Code attached.
I modified this code to use Rob's DHT library instead of the Adafruit library and it was also a winner..

Doc

Adafruit_T85_DHT.ino (1.19 KB)

Ok thank you, now it works very well :smiley:

good to hear it works! again a happy customer :wink: