// Include the libraries useful for the project
#include <Adafruit_Sensor.h>
#include <DHT.h>
// Define the DHT PIN
#define DHTPin 2
// Define the type of the DHT
#define DHT_TYPE DHT22
// Initialize the DHT sensor for 16 MHz Arduino
DHT dht=DHT(DHTPin,DHT_TYPE);
void setup() {
// put your setup code here, to run once:
// Enable serial communication
Serial.begin(9600);
// Setup the sensor
dht.begin();
}
void loop() {
// Make a pause to do the measurements
delay(2000);
// Note: The DHT sensor is a very slow sensor, so time measurement can go up to 2 seconds more
// Read the humidity (in percentage %)
float h= dht.readHumidity();
// Read the temperature
float t=dht.readTemperature();
// Read the temperature in Farenheit
float f=dht.readTemperature(true);
// Check if any reads failed and exit early
if(isnan(h) || isnan(t) || isnan(f)){
Serial.println("Failed to read from the DHT Sensor");
return;
}
// Compute heat index in Fahrenheit (default):
float hif = dht.computeHeatIndex(f, h);
// Compute heat index in Celsius:
float hic = dht.computeHeatIndex(t, h, false);
Serial.print("Humidity: ");
Serial.print(h);
Serial.print(" % ");
Serial.print("Temperature: ");
Serial.print(t);
Serial.print(" \xC2\xB0");
Serial.print("C | ");
Serial.print(f);
Serial.print(" \xC2\xB0");
Serial.print("F ");
Serial.print("Heat index: ");
Serial.print(hic);
Serial.print(" \xC2\xB0");
Serial.print("C | ");
Serial.print(hif);
Serial.print(" \xC2\xB0");
Serial.println("F");
}