#include <LiquidCrystal_I2C.h>
#include "DHT.h"
#define DHTPIN 2
#define DHTTYPE DHT11
#define POTPIN A0 // Define the analog pin for the potentiometer
LiquidCrystal_I2C lcd(0x27, 20, 4); // I2C address 0x27, 20 columns and 4 rows
DHT dht(DHTPIN, DHTTYPE);
void setup()
{
dht.begin(); // initialize the sensor
lcd.init(); // initialize the lcd
lcd.backlight(); // open the backlight
}
void loop()
{
delay(2000); // wait a few seconds between measurements
float humi = dht.readHumidity(); // read humidity
float tempC = dht.readTemperature(); // read temperature
int potValue = analogRead(POTPIN); // read the potentiometer value
lcd.clear();
// check if any reads failed
if (isnan(humi) || isnan(tempC)) {
lcd.setCursor(0, 0);
lcd.print("Failed");
} else {
char tempStr[7]; // buffer to hold the formatted temperature string
char humiStr[7]; // buffer to hold the formatted humidity string
dtostrf(tempC, 3, 2, tempStr); // convert temperature to string with 2 decimal places
dtostrf(humi, 3, 2, humiStr); // convert humidity to string with 2 decimal places
lcd.setCursor(0, 0); // start to print at the first row
lcd.print("Temp: ");
lcd.print(tempStr); // print the formatted temperature
lcd.print((char)223); // print ° character
lcd.print("C");
lcd.setCursor(0, 1); // start to print at the second row
lcd.print("Humi: ");
lcd.print(humiStr); // print the formatted humidity
lcd.print("%");
lcd.setCursor(0, 2); // start to print at the third row
lcd.print("Pot: ");
lcd.print(potValue); // print the potentiometer value
}
}