Arduino Uno: room climate with DHT22 and LCD
DHT22 measures, I²C LCD displays.
- Arduino Uno
- HD44780 LCD
- DHT22
Open in the simulatorNo account needed: press Simulate and it runs.
How it works
The DHT22 on D2 measures temperature and humidity (one-wire protocol, Adafruit DHT library). The 16×2 text LCD with I²C adapter (PCF8574, 0x27) shows them with two custom characters (thermometer, droplet). After power-on the library waits 1 s before it initializes the LCD; until then the LCD shows the typical blocks in line 1. Change temperature and humidity in the inspector, even while the simulation runs.
Wiring
Each line is one connection (net) and the pins it joins.
| Net | Pins |
|---|---|
| GND | A1 GNDDS1 GNDU2 GNDV1 − |
| 5V | A1 5VDS1 VCCU2 VCCV1 + |
| DHT | A1 D2U2 DATA |
| SCL | A1 A5/SCLDS1 SCL |
| SDA | A1 A4/SDADS1 SDA |
Parts
Firmware
sketch.ino · 27 lines
// Room climate display: the DHT22 on D2 measures, the 16×2 LCD with I²C adapter (0x27, A4/A5) displays
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <DHT.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
DHT dht(2, DHT22);
byte thermo[8] = { 0b00100, 0b01010, 0b01010, 0b01110, 0b01110, 0b11111, 0b11111, 0b01110 }; // custom character (CGRAM)
byte drop[8] = { 0b00100, 0b00100, 0b01010, 0b01010, 0b10001, 0b10001, 0b01110, 0b00000 };
void setup() {
Serial.begin(115200);
lcd.init(); // waits 1 s after power-on (library)
lcd.backlight();
lcd.createChar(0, thermo);
lcd.createChar(1, drop);
dht.begin();
}
void loop() {
float t = dht.readTemperature(), h = dht.readHumidity();
if (isnan(t) || isnan(h)) { lcd.clear(); lcd.print(F("Sensor error")); delay(2000); return; }
lcd.setCursor(0, 0); lcd.write(0); lcd.print(F(" Temp: ")); lcd.print(t, 1); lcd.print((char)223); lcd.print(F("C "));
lcd.setCursor(0, 1); lcd.write(1); lcd.print(F(" Humidity: ")); lcd.print(h, 0); lcd.print(F(" % "));
Serial.print(t, 1); Serial.print(F(" C ")); Serial.print(h, 1); Serial.println(F(" %"));
delay(2000); // the DHT22 measures at most every 2 s
}Open in the simulator