Arduino Temperature & Humidity Monitoring using DHT22/DHT11

Arduino Temperature & Humidity Monitoring using DHT22/DHT11

Story

Hello guys welcome to my blog with updated Tutorial. I wrote first tutorial about Arduino and DHT22 back in 2016 and it got more then half million views. Now I have started my own blog. I thought I need to update my first and famous tutorial with an updated diagram and code. So here I am presenting my updated tutorial. I always wanted to explore things on my own. This tutorial is part of my exploration. Always wanted to monitor my room temperature & humidity with my DIY device instead of pre-built thermostats. This tutorial is good for beginners who want to get started with Arduino development.

🧰 Components Required

  • Arduino Uno R3 or any Arduino Development Board
  • DHT22/DHT11 Temperature & Humidity Sensor
  • Breadboard and jumper wires

🔌 Connect the Circuit

Before moving next here we need to understand DHT22/DHT11 sensor pinout. On the right side of below diagram you will find out the DHT pinout layout. First pin is designated to VCC and you can either connect it with Arduino Uno 5V or 3.3V. Second pin is a Data pin which is connected to Arduino Uno Pin#7 according to below diagram. Third Pin is NC which is not used in this example. And Last pin is Ground pin which is connected to Arduino Uno GND Pin.

Below is the diagram which is practically demonstrating Arduino Uno R3 and DHT11 connections.

💻 Write the Code

Before moving towards the code you should have Adafruit DHT Sensor Library. Click on the library manager icon on the left which is marked by red box. Type “adafruit dht sensor” in a library search bar. Then select the DHT sensor library by Adafruit. Finally, click on the INSTALL button.

Next you will see the pop to install dependencies or not just click on INSTALL ALL button.

After sucessfull installation of library now it’s time to explore the example. Click on File–>Examples–>DHT sensor library and select DHT_Unified_Sensor Example. Here you just need to update the Digital Pin#. And assign the proper type of sensor like either it will be DHT11 or DHT22.

Now we will see the following code into editor. And we updated the DHTPIN constant to arduino uno digital pin 7. And DHTTYPE constant value to DHT11 as right now i am using DHT11 sensor instead of DHT22.

// DHT Temperature & Humidity Sensor
// Unified Sensor Library Example
// Written by Tony DiCola for Adafruit Industries
// Released under an MIT license.

// Tutorial by IoTSnacks.com
// REQUIRES the following Arduino libraries:
// - DHT Sensor Library: https://github.com/adafruit/DHT-sensor-library
// - Adafruit Unified Sensor Lib: https://github.com/adafruit/Adafruit_Sensor

#include <Adafruit_Sensor.h>
#include <DHT.h>
#include <DHT_U.h>

#define DHTPIN 7     // Arduino Uno R3 D7 as per Diagram

// Uncomment the type of sensor in use:
#define DHTTYPE    DHT11     // DHT 11
//#define DHTTYPE    DHT22     // DHT 22 (AM2302)


DHT_Unified dht(DHTPIN, DHTTYPE);

uint32_t delayMS;

void setup() {
  Serial.begin(9600);
  // Initialize device.
  dht.begin();
  // Set delay between sensor readings
  delayMS = 1000;
}

void loop() {
  // Delay between measurements.
  delay(delayMS);
  // Get temperature event and print its value.
  sensors_event_t event;
  dht.temperature().getEvent(&event);
  if (isnan(event.temperature)) {
    Serial.println(F("Error reading temperature!"));
  }
  else {
    Serial.print(F("Temperature: "));
    Serial.print(event.temperature);
    Serial.println(F("°C"));
  }
  // Get humidity event and print its value.
  dht.humidity().getEvent(&event);
  if (isnan(event.relative_humidity)) {
    Serial.println(F("Error reading humidity!"));
  }
  else {
    Serial.print(F("Humidity: "));
    Serial.print(event.relative_humidity);
    Serial.println(F("%"));
  }
}

💡Output

Click on the Serial Monitor icon. Set the baud rate to 9600. This will allow you to see the real-time values of temperature and humidity.