Components and supplies
1
Arduino Uno
1
BreadBoard
1
DHT22 Digital Temperature & Humidity Sensor Module
1
10k Resistor
Apps and platforms
1
Arduino IDE
Project description
Code
Arduino - Humidity Sensor Code
c_cpp
1// Example testing sketch for various DHT humidity/temperature sensors 2 3#include "DHT.h" 4#define DHTPIN 2 // what digital pin we're connected to 5// Uncomment whatever type you're using! 6//#define DHTTYPE DHT11 // DHT 11 7#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321 8//#define DHTTYPE DHT21 // DHT 21 (AM2301) 9// Connect pin 1 (on the left) of the sensor to +5V 10// NOTE: If using a board with 3.3V logic like an Arduino Due connect pin 1 11// to 3.3V instead of 5V! 12// Connect pin 2 of the sensor to whatever your DHTPIN is 13// Connect pin 4 (on the right) of the sensor to GROUND 14// Connect a 10K resistor from pin 2 (data) to pin 1 (power) of the sensor 15// Initialize DHT sensor. 16// Note that older versions of this library took an optional third parameter to 17// tweak the timings for faster processors. This parameter is no longer needed 18// as the current DHT reading algorithm adjusts itself to work on faster procs. 19DHT dht(DHTPIN, DHTTYPE); 20 21void setup() { 22 Serial.begin(9600); 23 Serial.println("DHTxx test!"); 24 dht.begin(); 25} 26 27void loop() { 28 delay(2000); // Wait a few seconds between measurements 29 float h = dht.readHumidity(); 30 // Reading temperature or humidity takes about 250 milliseconds! 31 float t = dht.readTemperature(); 32 // Read temperature as Celsius (the default) 33 float f = dht.readTemperature(true); 34 // Read temperature as Fahrenheit (isFahrenheit = true) 35 // Check if any reads failed and exit early (to try again). 36 if (isnan(h) || isnan(t) || isnan(f)) { 37 Serial.println("Failed to read from DHT sensor!"); 38 return; 39 } 40 41 // Compute heat index in Fahrenheit (the default) 42 float hif = dht.computeHeatIndex(f, h); 43 // Compute heat index in Celsius (isFahreheit = false) 44 float hic = dht.computeHeatIndex(t, h, false); 45 Serial.print ("Humidity: "); 46 Serial.print (h); 47 Serial.print (" %\ "); 48 Serial.print ("Temperature: "); 49 Serial.print (t); 50 Serial.print (" *C "); 51 Serial.print (f); 52 Serial.print (" *F\ "); 53 Serial.print ("Heat index: "); 54 Serial.print (hic); 55 Serial.print (" *C "); 56 Serial.print (hif); 57 Serial.println (" *F"); 58}
Arduino - Humidity Sensor Code
c_cpp
1// Example testing sketch for various DHT humidity/temperature sensors 2 3#include "DHT.h" 4#define DHTPIN 2 // what digital pin we're connected to 5// Uncomment whatever type you're using! 6//#define DHTTYPE DHT11 // DHT 11 7#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321 8//#define DHTTYPE DHT21 // DHT 21 (AM2301) 9// Connect pin 1 (on the left) of the sensor to +5V 10// NOTE: If using a board with 3.3V logic like an Arduino Due connect pin 1 11// to 3.3V instead of 5V! 12// Connect pin 2 of the sensor to whatever your DHTPIN is 13// Connect pin 4 (on the right) of the sensor to GROUND 14// Connect a 10K resistor from pin 2 (data) to pin 1 (power) of the sensor 15// Initialize DHT sensor. 16// Note that older versions of this library took an optional third parameter to 17// tweak the timings for faster processors. This parameter is no longer needed 18// as the current DHT reading algorithm adjusts itself to work on faster procs. 19DHT dht(DHTPIN, DHTTYPE); 20 21void setup() { 22 Serial.begin(9600); 23 Serial.println("DHTxx test!"); 24 dht.begin(); 25} 26 27void loop() { 28 delay(2000); // Wait a few seconds between measurements 29 float h = dht.readHumidity(); 30 // Reading temperature or humidity takes about 250 milliseconds! 31 float t = dht.readTemperature(); 32 // Read temperature as Celsius (the default) 33 float f = dht.readTemperature(true); 34 // Read temperature as Fahrenheit (isFahrenheit = true) 35 // Check if any reads failed and exit early (to try again). 36 if (isnan(h) || isnan(t) || isnan(f)) { 37 Serial.println("Failed to read from DHT sensor!"); 38 return; 39 } 40 41 // Compute heat index in Fahrenheit (the default) 42 float hif = dht.computeHeatIndex(f, h); 43 // Compute heat index in Celsius (isFahreheit = false) 44 float hic = dht.computeHeatIndex(t, h, false); 45 Serial.print ("Humidity: "); 46 Serial.print (h); 47 Serial.print (" %\ "); 48 Serial.print ("Temperature: "); 49 Serial.print (t); 50 Serial.print (" *C "); 51 Serial.print (f); 52 Serial.print (" *F\ "); 53 Serial.print ("Heat index: "); 54 Serial.print (hic); 55 Serial.print (" *C "); 56 Serial.print (hif); 57 Serial.println (" *F"); 58}
Downloadable files
Circuit Diagram
Circuit Diagram

Comments
Only logged in users can leave comments