DEV Community

張旭豐
張旭豐

Posted on

5 DHT22 Sensor Projects That Monitor the World Around You

5 DHT22 Sensor Projects That Monitor the World Around You

Build environmental monitoring systems: room thermostat, greenhouse fan, reptile terrarium, weather station, and closet dehumidifier

DHT22 Temperature-Humidity Sensor Hero

The DHT22 (or DHT11) temperature and humidity sensor is one of the most practical environmental monitoring tools for Arduino projects. It gives you two measurements from one module — temperature and relative humidity — with accuracy good enough for most home automation tasks. In this guide, we'll build five monitoring and control projects that respond to environmental conditions automatically.

Topics covered: DHT library, threshold logic with hysteresis, relay control for fans/heaters, LCD I2C display, SD card data logging, state machine design.


What You'll Need

  • DHT22 sensor (×1-3 depending on project)
  • Arduino Nano or Uno (×1)
  • 5V relay module (for fan/heater projects)
  • 16×2 LCD I2C display (for monitoring projects)
  • Buzzer (for alarm projects)
  • Jumper wires and breadboard
  • USB cable for programming

How the DHT22 Works

The DHT22 uses a single-wire digital communication protocol. It measures temperature via a thermistor and humidity via a capacitive sensor element. Readings are returned as calibrated digital values — no analog calibration needed.

Arduino          DHT22
  Pin 2  ──────  DATA
  5V     ──────  VCC
  GND    ──────  GND
Enter fullscreen mode Exit fullscreen mode

A 10K pull-up resistor between DATA and VCC is recommended for stable communication over long wires.

// WF1 Run #039 - Basic DHT Reading
#include <DHT.h>

#define DHT_PIN  2
#define DHT_TYPE DHT22

DHT dht(DHT_PIN, DHT_TYPE);

void setup() {
  Serial.begin(115200);
  dht.begin();
}

void loop() {
  float temp = dht.readTemperature();
  float humidity = dht.readHumidity();

  Serial.print("Temp: ");
  Serial.print(temp);
  Serial.print(" C  Humidity: ");
  Serial.print(humidity);
  Serial.println(" %");

  delay(2000);
}
Enter fullscreen mode Exit fullscreen mode

Project 1: Room Thermostat

Goal: Display room temperature and humidity on an LCD, with automatic fan control when it gets too hot or humid.

Room Thermostat

Hardware

  • DHT22 sensor
  • Arduino Nano
  • 16×2 LCD I2C display
  • 5V relay module
  • Small USB fan

Code

// WF1 Run #039 - Project 1: Room Thermostat
#include <DHT.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>

#define DHT_PIN     2
#define RELAY_PIN   7
#define DHT_TYPE    DHT22

#define TEMP_THRESHOLD   28.0  // Fan on above 28C
#define HUM_THRESHOLD    70.0  // Fan on above 70% RH
#define CLEAR_THRESHOLD  25.0  // Fan off below 25C
#define HUM_CLEAR        65.0  // Fan off below 65% RH

DHT dht(DHT_PIN, DHT_TYPE);
LiquidCrystal_I2C lcd(0x27, 16, 2);
bool fanOn = false;

void setup() {
  dht.begin();
  lcd.begin();
  lcd.backlight();
  pinMode(RELAY_PIN, OUTPUT);
  Serial.begin(115200);
}

void loop() {
  float temp = dht.readTemperature();
  float hum = dht.readHumidity();

  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Temp: ");
  lcd.print(temp, 1);
  lcd.print("C");
  lcd.setCursor(0, 1);
  lcd.print("Hum: ");
  lcd.print(hum, 1);
  lcd.print("%");

  // Hysteresis: fan ON above threshold, OFF below clear threshold
  if ((temp > TEMP_THRESHOLD || hum > HUM_THRESHOLD) && !fanOn) {
    digitalWrite(RELAY_PIN, HIGH);
    fanOn = true;
    lcd.setCursor(12, 1);
    lcd.print(" FAN");
  } else if (temp < CLEAR_THRESHOLD && hum < HUM_CLEAR && fanOn) {
    digitalWrite(RELAY_PIN, LOW);
    fanOn = false;
    lcd.setCursor(12, 1);
    lcd.print("    ");
  }

  delay(2000);
}
Enter fullscreen mode Exit fullscreen mode

Project 2: Greenhouse Fan Controller

Goal: Monitor greenhouse temperature and automatically open a vent or turn on a fan when it gets too hot, with LCD showing real-time readings.

Greenhouse Fan Controller

Hardware

  • DHT22 sensor
  • Arduino Mega (for more I/O)
  • 16×2 LCD I2C display
  • 2× 5V relay modules (fan + vent motor)
  • 12V DC fan or ventilation motor

Code

// WF1 Run #039 - Project 2: Greenhouse Controller
#include <DHT.h>
#include <LiquidCrystal_I2C.h>

#define DHT_PIN      2
#define FAN_RELAY    7
#define VENT_RELAY   8
#define DHT_TYPE     DHT22

#define HOT_TEMP     30.0
#define COOL_TEMP    26.0

DHT dht(DHT_PIN, DHT_TYPE);
LiquidCrystal_I2C lcd(0x27, 16, 2);

void setup() {
  dht.begin();
  lcd.begin();
  lcd.backlight();
  pinMode(FAN_RELAY, OUTPUT);
  pinMode(VENT_RELAY, OUTPUT);
  digitalWrite(FAN_RELAY, LOW);
  digitalWrite(VENT_RELAY, LOW);
}

void loop() {
  float temp = dht.readTemperature();
  float hum = dht.readHumidity();

  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Greenhouse");
  lcd.setCursor(0, 1);
  lcd.print(temp, 1);
  lcd.print("C ");
  lcd.print(hum, 1);
  lcd.print("%RH");

  // Fan only
  if (temp > HOT_TEMP) {
    digitalWrite(FAN_RELAY, HIGH);
    digitalWrite(VENT_RELAY, HIGH);  // Open vent too
  } else if (temp < COOL_TEMP) {
    digitalWrite(FAN_RELAY, LOW);
    digitalWrite(VENT_RELAY, LOW);
  }

  delay(5000);  // Check every 5 seconds
}
Enter fullscreen mode Exit fullscreen mode

Project 3: Reptile Terrarium Controller

Goal: Maintain proper temperature and humidity for reptile enclosures, with heat lamp and misting system.

Reptile Terrarium Controller

Hardware

  • DHT22 sensor
  • Arduino Nano
  • Heat lamp (via relay)
  • Misting system / fogger (via relay)
  • 16×2 LCD I2C display

Code

// WF1 Run #039 - Project 3: Terrarium Controller
#include <DHT.h>
#include <LiquidCrystal_I2C.h>

#define DHT_PIN       2
#define HEAT_LAMP     7
#define MISTER        8
#define DHT_TYPE      DHT22

#define TEMP_DAY      28.0
#define TEMP_NIGHT    24.0
#define HUM_HIGH      75.0
#define HUM_LOW       55.0

DHT dht(DHT_PIN, DHT_TYPE);
LiquidCrystal_I2C lcd(0x27, 16, 2);

void setup() {
  dht.begin();
  lcd.begin();
  pinMode(HEAT_LAMP, OUTPUT);
  pinMode(MISTER, OUTPUT);
}

void loop() {
  float temp = dht.readTemperature();
  float hum = dht.readHumidity();

  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Terrarium");
  lcd.setCursor(0, 1);
  lcd.print(temp, 1);
  lcd.print("C ");
  lcd.print(hum, 1);
  lcd.print("%");

  // Temperature control with heat lamp
  if (temp < TEMP_DAY) {
    digitalWrite(HEAT_LAMP, HIGH);  // Heat on
  } else {
    digitalWrite(HEAT_LAMP, LOW);  // Heat off
  }

  // Humidity control with mister
  if (hum < HUM_LOW) {
    digitalWrite(MISTER, HIGH);  // Mist on
  } else if (hum > HUM_HIGH) {
    digitalWrite(MISTER, LOW);  // Mist off
  }

  delay(3000);
}
Enter fullscreen mode Exit fullscreen mode

Project 4: Weather Station with Data Logging

Goal: Log outdoor temperature and humidity to an SD card with timestamps for long-term climate analysis.

Weather Station

Hardware

  • DHT22 sensor (with weatherproof enclosure)
  • Arduino Mega
  • SD card module
  • DS3231 RTC module
  • 16×2 LCD I2C display
  • Solar panel with USB charging

Code

// WF1 Run #039 - Project 4: Weather Station
#include <DHT.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <RTClib.h>
#include <SD.h>

#define DHT_PIN   2
#define DHT_TYPE  DHT22

DHT dht(DHT_PIN, DHT_TYPE);
LiquidCrystal_I2C lcd(0x27, 16, 2);
RTC_DS3231 rtc;
File logFile;

void setup() {
  dht.begin();
  lcd.begin();
  lcd.backlight();

  if (!rtc.begin()) {
    lcd.print("RTC Error");
    while(1);
  }

  if (!SD.begin(10)) {
    lcd.setCursor(0, 1);
    lcd.print("SD Error");
    while(1);
  }

  logFile = SD.open("weather.csv", FILE_WRITE);
  if (logFile) {
    logFile.println("Timestamp,TempC,Humidity");
    logFile.close();
  }

  Serial.begin(115200);
}

void loop() {
  float temp = dht.readTemperature();
  float hum = dht.readHumidity();
  DateTime now = rtc.now();

  lcd.clear();
  lcd.setCursor(0, 0);
  char timeStr[9];
  sprintf(timeStr, "%02d:%02d:%02d", now.hour(), now.minute(), now.second());
  lcd.print(timeStr);
  lcd.setCursor(0, 1);
  lcd.print(temp, 1);
  lcd.print("C ");
  lcd.print(hum, 1);
  lcd.print("%");

  // Log every 10 minutes
  if (now.minute() % 10 == 0 && now.second() < 5) {
    logFile = SD.open("weather.csv", FILE_WRITE);
    if (logFile) {
      logFile.print(now.timestamp());
      logFile.print(",");
      logFile.print(temp, 2);
      logFile.print(",");
      logFile.println(hum, 2);
      logFile.close();
    }
  }

  delay(1000);
}
Enter fullscreen mode Exit fullscreen mode

Project 5: Closet Dehumidifier Controller

Goal: Detect high humidity in a closet or small room and automatically run a small dehumidifier or fan.

Closet Dehumidifier Controller

Hardware

  • DHT11 sensor (lower cost, suitable for small spaces)
  • Arduino Nano
  • 5V relay module
  • Small 5V fan or dehumidifier

Code

// WF1 Run #039 - Project 5: Closet Dehumidifier
#include <DHT.h>

#define DHT_PIN   2
#define RELAY_PIN 7
#define DHT_TYPE  DHT11

#define HUM_ON    70.0
#define HUM_OFF   60.0

DHT dht(DHT_PIN, DHT_TYPE);
bool dehumOn = false;

void setup() {
  dht.begin();
  pinMode(RELAY_PIN, OUTPUT);
  digitalWrite(RELAY_PIN, LOW);
  Serial.begin(115200);
}

void loop() {
  float hum = dht.readHumidity();

  Serial.print("Closet Humidity: ");
  Serial.println(hum);

  if (hum > HUM_ON && !dehumOn) {
    digitalWrite(RELAY_PIN, HIGH);
    dehumOn = true;
    Serial.println("Dehumidifier ON");
  } else if (hum < HUM_OFF && dehumOn) {
    digitalWrite(RELAY_PIN, LOW);
    dehumOn = false;
    Serial.println("Dehumidifier OFF");
  }

  delay(600000);  // Check every 10 minutes
}
Enter fullscreen mode Exit fullscreen mode

Troubleshooting

Problem Cause Fix
DHT returns NaN Wiring or library issue Check DATA pin connection; add 10K pull-up resistor
Humidity reading off Sensor near heat source Move sensor at least 10cm from heat-generating components
LCD shows wrong characters I2C address wrong Scan I2C addresses (common: 0x27, 0x3F)
Relay clicking rapidly No hysteresis on threshold Add separate ON and OFF thresholds (at least 5% apart)
SD card won't write CS pin conflict Use pin 10 or 53 for SD CS; don't share with other SPI devices

Calibration note: DHT22 accuracy is ±0.5°C and ±2%RH. For more precise applications, compare readings against a calibrated thermometer and apply a small offset correction in software.


Start Here

Affiliate disclosure: As an Amazon Associate, I earn from qualifying purchases.

The right parts make the difference:

DHT22 Temperature Humidity Sensor — High accuracy, suitable for most monitoring projects.

DHT11 Sensor — Lower cost, good for non-critical applications.

Arduino Mega 2560 — More I/O for multi-relay projects.

5V Relay Module — For controlling fans, lamps, and pumps.

16x2 LCD I2C Display — Clear status display for monitoring projects.


Next Step: From Scene to Sensor, Without Writing Code

If this guide gave you ideas for your own setup — but you're not sure which sensors and outputs work best for your specific space — I can help you map that out.

I offer a personalized interactive device design guide at Fiverr:

👉 https://www.fiverr.com/phd_hfchang/generate-an-arduino-interactive-prototypef

What you get:

  • A custom guide based on your actual scene (not generic recommendations)
  • Sensor selection matched to user behavior and physical constraints
  • Interaction logic without needing to write code from scratch
  • Testing methodology with pass/fail criteria for each output

Tags: Arduino DHT22 Temperature Humidity Environmental Monitoring IoT Smart Home

Top comments (0)