#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <DHT.h>
#define DHTPIN 3 // Digital pin connected to DHT22
#define DHTTYPE DHT22 // DHT 22 (AM2302) sensor type
#define POT_PIN A3 // Analog pin connected to potentiometer
// Initialize the LCD with I2C address 0x27 (common for 20x4 LCDs with I2C module)
LiquidCrystal_I2C lcd(0x27, 20, 4);
DHT dht(DHTPIN, DHTTYPE);
void setup() {
lcd.begin(16, 2); // Initialize the LCD with 16 columns and 2 rows
lcd.backlight(); // Turn on the backlight
dht.begin(); // Initialize the DHT sensor
Serial.begin(9600);
}
void loop() {
delay(1000);
lcd.clear();
// Read temperature and humidity from DHT22
float temperature = dht.readTemperature();
float humidity = dht.readHumidity();
int potValue = analogRead(POT_PIN);
// Check if readings failed and exit early to avoid displaying 'NaN'
if (isnan(temperature) || isnan(humidity)) {
Serial.println("Failed to read from DHT sensor!");
} else {
char tempStr[7];
char humiStr[7];
dtostrf(temperature, 3, 2, tempStr);
dtostrf(humidity, 3, 2, humiStr);
}
// Display Temperature and Humidity on LCD
lcd.setCursor(0, 0);
lcd.print("Temp: ");
lcd.print(temperature);
lcd.print((char)223);
lcd.print("C");
lcd.setCursor(0, 1);
lcd.print("Humidity: ");
lcd.print(humidity);
lcd.print("%");
// Display Potentiometer value on LCD
lcd.setCursor(0, 2);
lcd.print("Pot. Val: ");
lcd.print(potValue);
}