This circuit integrates an Arduino UNO microcontroller with a DHT11 Humidity and Temperature Sensor and an LCD screen (16x2 with I2C interface) to display environmental data. The Arduino UNO reads the temperature and humidity from the DHT11 sensor and outputs the readings to the LCD screen. The LCD uses the I2C communication protocol, which simplifies the wiring by using only two data lines.
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <DHT.h>
// Initialize the DHT11 sensor
#define DHTPIN 2 // Digital pin connected to the DHT sensor
#define DHTTYPE DHT11 // DHT 11
DHT dht(DHTPIN, DHTTYPE);
// Initialize the LCD on address 0x27 for a 16 chars and 2 line display
LiquidCrystal_I2C lcd(0x27, 16, 2);
void setup() {
// Initialize the LCD and DHT11 sensor
lcd.init();
lcd.backlight();
dht.begin();
}
void loop() {
// Read humidity and temperature values
float humidity = dht.readHumidity();
float temperature = dht.readTemperature();
// Check if any reads failed and exit early (to try again).
if (isnan(humidity) || isnan(temperature)) {
lcd.clear();
lcd.print("Failed to read");
return;
}
// Display the humidity and temperature on the LCD
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Humidity: ");
lcd.print(humidity);
lcd.print("%");
lcd.setCursor(0, 1);
lcd.print("Temp: ");
lcd.print(temperature);
lcd.print("C");
// Wait a few seconds between measurements.
delay(2000);
}
This code is designed to run on the Arduino UNO microcontroller. It initializes the DHT11 sensor and the LCD screen, reads the temperature and humidity data from the sensor, and displays the readings on the LCD. If the sensor fails to provide valid readings, an error message is displayed. The measurements are updated every two seconds.