One of the many sensors I bought is the DHT22. This is a digital temperature and humidity sensor. It has 4 pins but only 3 are used. To get it to work you will need the DHT library, this library supports both the DHT11 and DHT22 sensors.
I found this nice example sketch for the DHT11 and changed it to work with my DHT22, I only needed to set the type of sensor and change some of the statements used to read the values from the sensor, pretty easy even for a newbie like me 🙂
The sketch will output the values to an I2C lcd display.
/* http://www.bajdi.com DHT22 Humidity and Temperature Sensor test Displayed on I2C LCD Display Credits: Rob Tillaart for providing the DHT library Made the Sketch starting from: http://arduino-info.wikispaces.com/PROJECT-Temp-Humidity-Display */ /*-----( Import needed libraries )-----*/ #include <dht.h> #include <Wire.h> #include <LiquidCrystal_I2C.h> /*-----( Declare objects )-----*/ LiquidCrystal_I2C lcd(0x27,16,2); // set the LCD address to 0x27 dht DHT; /*-----( Declare Constants, Pin Numbers )-----*/ #define DHTTYPE DHT22 // Sensor type #define DHT22_PIN 2 void setup() /*----( SETUP: RUNS ONCE )----*/ { Serial.begin(9600); //(Remove all 'Serial' commands if not needed) lcd.init(); // initialize the lcd lcd.backlight(); // Print a message to the LCD. //lcd.setCursor(0, 1); lcd.print("DHT Temp/Humid"); }/*--(end setup )---*/ void loop() /*----( LOOP: RUNS CONSTANTLY )----*/ { int chk = DHT.read22(DHT22_PIN); Serial.print("Read sensor: "); switch (chk) { case 0: Serial.println("OK"); break; case -1: Serial.println("Checksum error"); break; case -2: Serial.println("Time out error"); break; default: Serial.println("Unknown error"); break; } lcd.setCursor(0, 1); lcd.print("C="); lcd.print((float)DHT.temperature, 0); Serial.print("Temperature (oC): "); Serial.println((float)DHT.temperature, 2); lcd.print(" F="); lcd.print(Fahrenheit(DHT.temperature), 0); Serial.print("Temperature (oF): "); Serial.println(Fahrenheit(DHT.temperature), 2); lcd.print(" H="); lcd.print((float)DHT.humidity, 0); lcd.print("%"); Serial.print("Humidity (%): "); Serial.println((float)DHT.humidity, 2); Serial.print("Temperature (K): "); Serial.println(Kelvin(DHT.temperature), 2); Serial.print("Dew Point (oC): "); Serial.println(dewPoint(DHT.temperature, DHT.humidity)); Serial.print("Dew PointFast (oC): "); Serial.println(dewPointFast(DHT.temperature, DHT.humidity)); delay(5000); }/* --(end main loop )-- */ /*-----( Declare User-written Functions )-----*/ // //Celsius to Fahrenheit conversion double Fahrenheit(double celsius) { return 1.8 * celsius + 32; } //Celsius to Kelvin conversion double Kelvin(double celsius) { return celsius + 273.15; } // dewPoint function NOAA // reference: http://wahiduddin.net/calc/density_algorithms.htm double dewPoint(double celsius, double humidity) { double A0= 373.15/(273.15 + celsius); double SUM = -7.90298 * (A0-1); SUM += 5.02808 * log10(A0); SUM += -1.3816e-7 * (pow(10, (11.344*(1-1/A0)))-1) ; SUM += 8.1328e-3 * (pow(10,(-3.49149*(A0-1)))-1) ; SUM += log10(1013.246); double VP = pow(10, SUM-3) * humidity; double T = log(VP/0.61078); // temp var return (241.88 * T) / (17.558-T); } // delta max = 0.6544 wrt dewPoint() // 5x faster than dewPoint() // reference: http://en.wikipedia.org/wiki/Dew_point double dewPointFast(double celsius, double humidity) { double a = 17.271; double b = 237.7; double temp = (a * celsius) / (b + celsius) + log(humidity/100); double Td = (b * temp) / (a - temp); return Td; } /* ( THE END ) */