Freedom Sale
Independence Day Special — Unlock the AI Path 70% off our most popular AI course · Limited time offer
--Days
--Hrs
--Min
--Sec
Claim Your Discount
✦ Beginner ⏱ 30 min

🌡️ Build a Temperature and Humidity Monitor with Arduino

🎯 What You'll Build

An Arduino circuit that reads temperature and humidity from a DHT11 sensor every 2 seconds and displays live readings in the Serial Monitor.

📋 What You'll Need

1

Wire the DHT11 sensor

The DHT11 has 3 pins (on the module version): VCC, Data, and GND. Connect it to the Arduino in 3 wires.

// Wiring (DHT11 module with 3 pins):
//
//  DHT11 Module    Arduino Uno
//  ─────────────   ───────────
//  VCC     ───────  5V
//  Data    ───────  Digital Pin 2
//  GND     ───────  GND
//
// If using bare DHT11 (4 pins):
//  Pin 1 (VCC)  → 5V
//  Pin 2 (Data) → Digital 2 + 10kΩ resistor to 5V (pull-up)
//  Pin 3        → not connected
//  Pin 4 (GND)  → GND
💡 Tip: The module version of DHT11 (3 pins, on a small PCB) already has the pull-up resistor built in — no extra resistor needed. It costs about ₹50–100 and is the easiest version to start with.
2

Install the DHT library

Open the Arduino IDE Library Manager and install the DHT sensor library.

// In Arduino IDE:
// 1. Go to Sketch → Include Library → Manage Libraries
// 2. Search for "DHT sensor library" by Adafruit
// 3. Click Install
// 4. Also install "Adafruit Unified Sensor" if prompted

// Or via Arduino CLI:
// arduino-cli lib install "DHT sensor library"
// arduino-cli lib install "Adafruit Unified Sensor"
3

Write the sketch

Read temperature and humidity every 2 seconds and print to Serial Monitor.

#include <DHT.h>

#define DHTPIN  2       // Digital pin connected to DHT11
#define DHTTYPE DHT11   // DHT11 or DHT22

DHT dht(DHTPIN, DHTTYPE);

void setup() {
  Serial.begin(9600);
  dht.begin();
  Serial.println("Temperature & Humidity Monitor");
  Serial.println("==============================");
}

void loop() {
  delay(2000);  // DHT11 needs at least 1 second between reads

  float humidity    = dht.readHumidity();
  float tempC       = dht.readTemperature();     // Celsius
  float tempF       = dht.readTemperature(true); // Fahrenheit
  float heatIndex   = dht.computeHeatIndex(tempC, humidity, false);

  // Check for read errors
  if (isnan(humidity) || isnan(tempC)) {
    Serial.println("Failed to read from DHT sensor — check wiring!");
    return;
  }

  Serial.print("Humidity:    "); Serial.print(humidity);    Serial.println(" %");
  Serial.print("Temperature: "); Serial.print(tempC);       Serial.print(" °C  /  ");
                                  Serial.print(tempF);       Serial.println(" °F");
  Serial.print("Heat Index:  "); Serial.print(heatIndex);   Serial.println(" °C");
  Serial.println("------------------------------");
}
Temperature & Humidity Monitor
==============================
Humidity:    62.00 %
Temperature: 28.00 °C  /  82.40 °F
Heat Index:  30.21 °C
------------------------------
Humidity:    61.00 %
Temperature: 28.00 °C  /  82.40 °F
Heat Index:  30.05 °C
------------------------------
4

Add a high-temperature alert

Blink the built-in LED when temperature exceeds a threshold.

#include <DHT.h>

#define DHTPIN     2
#define DHTTYPE    DHT11
#define ALERT_TEMP 35.0   // Alert if above this temperature (°C)

DHT dht(DHTPIN, DHTTYPE);

void setup() {
  Serial.begin(9600);
  pinMode(LED_BUILTIN, OUTPUT);
  dht.begin();
}

void loop() {
  delay(2000);

  float humidity = dht.readHumidity();
  float tempC    = dht.readTemperature();

  if (isnan(humidity) || isnan(tempC)) return;

  Serial.print(tempC); Serial.print("°C  "); Serial.print(humidity); Serial.println("%");

  if (tempC > ALERT_TEMP) {
    Serial.println("⚠️  HIGH TEMPERATURE ALERT!");
    // Blink LED 3 times
    for (int i = 0; i < 3; i++) {
      digitalWrite(LED_BUILTIN, HIGH); delay(200);
      digitalWrite(LED_BUILTIN, LOW);  delay(200);
    }
  } else {
    digitalWrite(LED_BUILTIN, LOW);
  }
}
💡 Tip: To log readings to a CSV file on your computer, use the Arduino Serial Plotter (Tools → Serial Plotter) to see a live graph, or write a Python script using pyserial to read the serial port and save readings to a file.

🎉 You Did It!

Your Arduino is now a live environmental sensor. Connect it to an LCD display (I2C 16x2) to show readings without a computer. The same code works with DHT22 — just change DHTTYPE to DHT22 for higher precision readings.

Found something wrong?

Spotted a bug, broken code, or something that doesn't look right? Tell us what's off and we'll fix it.