Arduino Sensors – DHT11 Temperature/ Humidity Sensor Setup

The DHT11, 21 and 22 Sensors can be used to determine both temperature and humdity.

Note:

  • Make sure to comment/ uncomment the right sensor in setup

Links:

Functional Parts in the Project:

#include <DHT.h>

#define DHTPIN 2    

// Uncomment for Version 
#define DHTTYPE DHT11   // DHT 11
//#define DHTTYPE DHT22   // DHT 22  (AM2302), AM2321
//#define DHTTYPE DHT21   // DHT 21 (AM2301)

DHT dht(DHTPIN, DHTTYPE);

void setup() {
  Serial.begin(9600);
  Serial.println("DHTxx test!");

  dht.begin();
}

void loop() {
  delay(2000);

  //Humidity
  float h = dht.readHumidity();
  //Temp in Celsius
  float t = dht.readTemperature();
  //Temp in Fahrenheit (isFahrenheit = true)
  float f = dht.readTemperature(true);

  // Check for Failure
  if (isnan(h) || isnan(t) || isnan(f)) {
    Serial.println(F("Failed to read from DHT sensor!"));
    return;
  }

  //Heat Index in Farenheit
  float hif = dht.computeHeatIndex(f, h);
  //Heat Index in Celsius
  float hic = dht.computeHeatIndex(t, h, false);

  Serial.print("Humidity: ");
  Serial.print(h);
  Serial.print("%  Temperature: ");
  Serial.print(t);
  Serial.print("°C ");
  Serial.print(f);
  Serial.print("°F  Heat index: ");
  Serial.print(hic);
  Serial.print("°C ");
  Serial.print(hif);
  Serial.println("°F");
}

Be the first to comment

Leave a Reply