// Include the libraries
#include <DHT.h>
#include <LiquidCrystal.h>
// Define the pins
#define DHTPIN 4 // DHT22 data pin
#define DHTTYPE DHT22 // DHT22 sensor type
#define PIR1 12 // PIR sensor 1 pin
#define PIR2 13 // PIR sensor 2 pin
#define PIR3 14 // PIR sensor 3 pin
#define PIR4 15 // PIR sensor 4 pin
#define LED1 16 // LED 1 pin
#define LED2 17 // LED 2 pin
#define LED3 18 // LED 3 pin
#define RS 19 // LCD RS pin
#define EN 21 // LCD EN pin
#define D4 22 // LCD D4 pin
#define D5 23 // LCD D5 pin
#define D6 25 // LCD D6 pin
#define D7 26 // LCD D7 pin
// Initialize the objects
DHT dht(DHTPIN, DHTTYPE); // DHT22 object
LiquidCrystal lcd(RS, EN, D4, D5, D6, D7); // LCD object
// Define the constants
const float optTemp = 25.0; // Optimum temperature
const int delayTime = 2000; // Delay time
// Define the variables
float temp; // Temperature
float hum; // Humidity
bool pir1; // PIR sensor 1 state
bool pir2; // PIR sensor 2 state
bool pir3; // PIR sensor 3 state
bool pir4; // PIR sensor 4 state
bool ac; // Air conditioner state
bool lamp1; // Lamp 1 state
bool lamp2; // Lamp 2 state
void setup() {
// Initialize the serial monitor
Serial.begin(9600);
// Initialize the DHT22 sensor
dht.begin();
// Initialize the LCD display
lcd.begin(16, 2);
// Initialize the pins
pinMode(PIR1, INPUT);
pinMode(PIR2, INPUT);
pinMode(PIR3, INPUT);
pinMode(PIR4, INPUT);
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
pinMode(LED3, OUTPUT);
// Initialize the variables
temp = 0.0;
hum = 0.0;
pir1 = false;
pir2 = false;
pir3 = false;
pir4 = false;
ac = false;
lamp1 = false;
lamp2 = false;
}
void loop() {
// Read the temperature and humidity from the DHT22 sensor
temp = dht.readTemperature();
hum = dht.readHumidity();
// Check if the readings are valid
if (isnan(temp) || isnan(hum)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
// Print the readings to the serial monitor
Serial.print("Temperature: ");
Serial.print(temp);
Serial.print(" *C, Humidity: ");
Serial.print(hum);
Serial.println(" %");
// Display the readings on the LCD display
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Temp: ");
lcd.print(temp);
lcd.print(" C");
lcd.setCursor(0, 1);
lcd.print("Hum: ");
lcd.print(hum);
lcd.print(" %");
// Read the states of the PIR sensors
pir1 = digitalRead(PIR1);
pir2 = digitalRead(PIR2);
pir3 = digitalRead(PIR3);
pir4 = digitalRead(PIR4);
// Print the states of the PIR sensors to the serial monitor
Serial.print("PIR1: ");
Serial.print(pir1);
Serial.print(", PIR2: ");
Serial.print(pir2);
Serial.print(", PIR3: ");
Serial.print(pir3);
Serial.print(", PIR4: ");
Serial.println(pir4);
// Control the air conditioner based on the temperature and the PIR sensors
if (temp > optTemp && (pir1 || pir2 || pir3 || pir4)) {
// Turn on the air conditioner if the temperature is above the optimum and there is people in the room
ac = true;
digitalWrite(LED1, HIGH); // Turn on the LED 1
Serial.println("Air conditioner is ON");
} else {
// Turn off the air conditioner otherwise
ac = false;
digitalWrite(LED1, LOW); // Turn off the LED 1
Serial.println("Air conditioner is OFF");
}
// Control the lamps based on the PIR sensors
if (pir1 || pir2) {
// Turn on the lamp 1 if there is people in the front row
lamp1 = true;
digitalWrite(LED2, HIGH); // Turn on the LED 2
Serial.println("Lamp 1 is ON");
} else {
// Turn off the lamp 1 otherwise
lamp1 = false;
digitalWrite(LED2, LOW); // Turn off the LED 2
Serial.println("Lamp 1 is OFF");
}
if (pir3 || pir4) {
// Turn on the lamp 2 if there is people in the back row
lamp2 = true;
digitalWrite(LED3, HIGH); // Turn on the LED 3
Serial.println("Lamp 2 is ON");
} else {
// Turn off the lamp 2 otherwise
lamp2 = false;
digitalWrite(LED3, LOW); // Turn off the LED 3
Serial.println("Lamp 2 is OFF");
}
// Delay for a while
delay(delayTime);
}