// DHT22 Temperature and Humudity sensor
// Install the "DHT Sensor Library"
// wiring DHT22
// Connect the DHT22 sensor to the Arduino as follows:
// VCC to 5V on the Arduino
// GND to GND on the Arduino
// DATA to a digital pin on the Arduino (e.g., pin 2)
#include <DHT.h>
// Define the pin where the data line is connected
#define DHTPIN 2
// Define the type of sensor used
#define DHTTYPE DHT22
// Create an instance of the DHT class
DHT dht(DHTPIN, DHTTYPE);
void setup() {
// Start the serial communication
Serial.begin(9600);
// Initialize the DHT sensor
dht.begin();
}
void loop() {
// Wait a few seconds between measurements
delay(2000);
// Read humidity and temperature
float humidity = dht.readHumidity();
float temperature = dht.readTemperature();
// Check if any reads failed and exit early (to try again)
if (isnan(humidity) || isnan(temperature)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
// Print the results to the Serial Monitor
Serial.print("Humidity: ");
Serial.print(humidity);
Serial.print(" %\t");
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.println(" *C");
}