Practical 6

Digital Room Temperature and Humidity Monitoring System

DHT22 readings shown live on an LCD

Aim

To measure room temperature and relative humidity with a DHT22 sensor and display both values in real time on a 16x2 LCD.

Overview

The DHT22 digital sensor sends calibrated temperature and humidity readings over a single-wire protocol. The Arduino reads it every two seconds, applies sanity checks (valid range, NaN handling), and prints the values to a 16x2 character LCD over the 4-bit data bus (or I2C). The serial monitor provides a secondary logging output.

Components Required

  • Arduino Uno
  • DHT22 / AM2302 temperature-humidity sensor
  • 10k pull-up resistor for the data line (often built into modules)
  • 16x2 LCD display (parallel or I2C backpack)
  • 10k potentiometer for LCD contrast (parallel version)
  • Breadboard and jumper wires

Circuit Connections

FromToNote
DHT22 VCCArduino 5V-
DHT22 GNDArduino GND-
DHT22 DATAArduino D2With 10k pull-up to 5V
I2C LCD VCC / GNDArduino 5V / GND-
I2C LCD SDA / SCLArduino A4 / A5-

Interactive Circuit Diagram

Simulate the circuit: adjust the sensor inputs below and watch the outputs react according to the control logic.

Loading Wokwi components...

Working

  1. The Arduino initializes the DHT22 and the LCD in setup().
  2. Every two seconds it requests a fresh reading from the sensor.
  3. If isnan(t) or isnan(h), the display shows a Sensor error message instead of bogus values.
  4. Valid readings are printed as temperature in C on line 1 and humidity in % on line 2.
  5. The same values are echoed to the serial port for logging.

Control Logic

Sampling period must be at least 2 s for the DHT22. Readings outside 0.1-99.9 or NaN are rejected as errors, which is the standard validation pattern for one-wire sensors.

Arduino Code

1// Practical 6: Digital Room Temperature and Humidity Monitor
2#include <DHT.h>
3#include <LiquidCrystal_I2C.h>
4
5#define DHTPIN 2 // DHT22 data pin
6#define DHTTYPE DHT22
7
8DHT dht(DHTPIN, DHTTYPE);
9LiquidCrystal_I2C lcd(0x27, 16, 2); // I2C LCD: SDA-A4, SCL-A5
10
11void setup() {
12 lcd.init();
13 lcd.backlight();
14 lcd.print("T/H Monitor");
15 dht.begin();
16 Serial.begin(9600);
17}
18
19void loop() {
20 float humidity = dht.readHumidity();
21 float tempC = dht.readTemperature(); // Celsius
22
23 lcd.clear();
24 if (isnan(humidity) || isnan(tempC)) { // Validate the reading
25 lcd.print("Sensor error!");
26 Serial.println("Failed to read from DHT sensor!");
27 } else {
28 lcd.setCursor(0, 0);
29 lcd.print("Temp: ");
30 lcd.print(tempC, 1);
31 lcd.print((char)223); // Degree symbol
32 lcd.print("C");
33 lcd.setCursor(0, 1);
34 lcd.print("Hum: ");
35 lcd.print(humidity, 1);
36 lcd.print(" %");
37
38 Serial.print("Temp: ");
39 Serial.print(tempC);
40 Serial.print(" C, Hum: ");
41 Serial.print(humidity);
42 Serial.println(" %");
43 }
44 delay(2000); // DHT22 needs >= 2 s between reads
45}