#55Intermediate
Temperature and Humidity with DHT11
4.7(47)
60 completed

Project that measures the ambient temperature and humidity value with the DHT11 sensor and displays it on the I2C LCD screen. Teaches the use of digital temperature/humidity sensor.
Video
Circuit Diagram

Source Code
1#include <LiquidCrystal_I2C.h>
2#include <Wire.h>
3#include <DHT.h>
4
5#define DHTPIN 3
6#define DHTTYPE DHT11
7
8LiquidCrystal_I2C lcd(0x27, 16, 2);
9DHT dht(DHTPIN, DHTTYPE);
10
11void setup() {
12 dht.begin();
13 lcd.init(); // Modern libraries prefer init() over begin()
14 lcd.backlight();
15}
16
17void loop() {
18 int humidity = dht.readHumidity();
19 float temperature = dht.readTemperature();
20
21 // Check if readings failed
22 if (isnan(humidity) || isnan(temperature)) {
23 lcd.setCursor(0, 0);
24 lcd.print("Sensor Error! ");
25 return;
26 }
27
28 lcd.setCursor(0, 0);
29 lcd.print("Temp: ");
30 lcd.print(temperature);
31 lcd.print((char)223); // Degree symbol (°)
32 lcd.print("C ");
33
34 lcd.setCursor(0, 1);
35 lcd.print("Humidity: %");
36 lcd.print(humidity);
37 lcd.print(" "); // Trailing spaces to clear old data
38
39 delay(2000); // DHT11 is slow, 2 seconds is more stable
40}