Library for dht22

I am trying to run a sketch with a dht22 sensor and always I get a NaN value as read. I am using ada fruit library downloaded from here: GitHub - adafruit/DHT-sensor-library: Arduino library for DHT11, DHT22, etc Temperature & Humidity Sensors

I opened the .cpp file to see what it does and I found these lines:

switch (_type) {
    case DHT11:
      f = data[0];
      return f;
    case DHT22:
    case DHT21:
      f = data[0];
      f *= 256;
      f += data[1];
      f /= 10;
      return f;
    }
Serial.print("Read fail");
  return NAN;

And something similar for temperature. It always prints "Read fail". But I don't know if it is because the library or not. I am using the correct pin and my sketch just is the example they provide. So I think it should work. Do you know another library for testing?

It always prints "Read fail".

That means you have probably made a typo with the type parameter

Do you know another library for testing?

http://www.hobbyist.co.nz/?q=bump-sensor-dht22-oled-and-arduino

I found a library for dht11 and it worked! Clink the link read the text it will tell you to download a DHT library. Click on the link and save file. Open file and extract the contents. Then on Arduino IDE add the library, be careful cos the file you want to add is inside the file you save the contents to? The scroll down the web page and it gives you a link to some code.

There is also a code at Wiring up DHT11 Temp & Humidity sensor to the Arduino | Hobbyist.co.nz for dht11 but I think it should work for dht22 as well.

Basically instal the library from the link to Arduino IDE, copy and paste this code and upload. :slight_smile:

#include <dht.h>

#define dht_dpin A0 //no ; here. Set equal to channel sensor is on

dht DHT;

void setup(){
  Serial.begin(9600);
  delay(300);//Let system settle
  Serial.println("Humidity and temperature\n\n");
  delay(700);//Wait rest of 1000ms recommended delay before
  //accessing sensor
}//end "setup()"

void loop(){
  //This is the "heart" of the program.
  DHT.read11(dht_dpin);

    Serial.print("Current humidity = ");
    Serial.print(DHT.humidity);
    Serial.print("%  ");
    Serial.print("temperature = ");
    Serial.print(DHT.temperature); 
    Serial.println("C  ");
  delay(800);//Don't try to access too frequently... in theory
  //should be once per two seconds, fastest,
  //but seems to work after 0.8 second.
}// end loop()

ollie97:
NetSensor step 2: Connect bump switch and DHT22 sensor to Arduino | Hobbyist.co.nz

I found a library for dht11 and it worked! Clink the link read the text it will tell you to download a DHT library. Click on the link and save file. Open file and extract the contents. Then on Arduino IDE add the library, be careful cos the file you want to add is inside the file you save the contents to? The scroll down the web page and it gives you a link to some code.

There is also a code at Wiring up DHT11 Temp & Humidity sensor to the Arduino | Hobbyist.co.nz for dht11 but I think it should work for dht22 as well.

The second link points to an older version of - Arduino Playground - DHTLib -
there are a number of improvements / bugs fixed