#include <DHT.h>
#include <LiquidCrystal.h>
// Define the type of DHT sensor you're using
#define DHTTYPE DHT11 // Change to DHT22 if using a DHT22 sensor
// Define the pin where the data pin of the DHT sensor is connected
#define DHTPIN 2
// Initialize the DHT sensor
DHT dht(DHTPIN, DHTTYPE);
// Initialize the LCD (RS, E, D4, D5, D6, D7)
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
void setup() {
Serial.begin(9600); // Start Serial communication
pinMode(3, OUTPUT);
digitalWrite(3, HIGH);
pinMode(2, OUTPUT); // Set Pin 3 as output for PWM
analogWrite(2, 100); // Set contrast (value between 0 to 255)
pinMode(3, OUTPUT); // Set Pin 3 as output for PWM
analogWrite(3, 100); // Set contrast (value between 0 to 255)
Serial.println("DHT Sensor with LCD Initialized");
dht.begin(); // Start the DHT sensor
lcd.begin(16, 2); // Set up the LCD's columns and rows
lcd.print("Welcome. 27LINKS");
delay(2000);
lcd.clear(); // Clear the screen
}
void loop() {
delay(1000); // Wait 2 seconds between measurements
// Read humidity
float humidity = dht.readHumidity();
// Read temperature in Celsius
float temperature = dht.readTemperature();
// Check if any readings failed
if (isnan(humidity) || isnan(temperature)) {
lcd.clear();
lcd.print("Sensor Error!");
Serial.println("Failed to read from DHT sensor!");
return;
}
// Display readings on the LCD
lcd.setCursor(0, 0); // Set cursor to first row, first column
lcd.print("Temp.: ");
lcd.print(temperature);
lcd.print(" C");
lcd.setCursor(0, 1); // Set cursor to second row, first column
lcd.print("Humidity: ");
lcd.print(humidity);
lcd.print("%");
// Print readings to Serial Monitor as well
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.print(" *C, Humidity: ");
Serial.print(humidity);
Serial.println(" %");
}
//This code is in Public Domain. Any one can use this code.