#include <Servo.h>
#include <DHT.h>
#include <LiquidCrystal.h>
#define DHTPIN 2 // Pin for DHT22 sensor
#define DHTTYPE DHT22 // Change this to DHT22
#define PIRPIN 3 // Pin for PIR Motion Sensor
#define LIGHTPIN A0 // Pin for Light Sensor (LDR)
#define LEDPIN 13 // Pin for LED
#define RELAYPIN 8 // Pin for Relay (Fan Control)
#define BUZZERPIN 9 // Pin for Buzzer
#define SERVOPIN 10 // Pin for Servo Motor
DHT dht(DHTPIN, DHTTYPE);
Servo servo;
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int motionCounter = 0;
void setup() {
Serial.begin(9600);
pinMode(PIRPIN, INPUT);
pinMode(LIGHTPIN, INPUT);
pinMode(LEDPIN, OUTPUT);
pinMode(RELAYPIN, OUTPUT);
pinMode(BUZZERPIN, OUTPUT);
dht.begin();
servo.attach(SERVOPIN);
servo.write(0); // Door closed initially
lcd.begin(16, 2);
lcd.print("Smart Classroom");
delay(2000);
lcd.clear();
}
void loop() {
// Reading sensors
float temperature = dht.readTemperature();
float humidity = dht.readHumidity();
int lightLevel = analogRead(LIGHTPIN);
int motionDetected = digitalRead(PIRPIN);
// Automatic Lighting Control
if (lightLevel < 500) {
digitalWrite(LEDPIN, HIGH); // Turn on LED lights if it's dark
} else {
digitalWrite(LEDPIN, LOW); // Turn off LED lights if it's bright
}
// Fan Control based on Temperature
if (temperature > 25.0) {
digitalWrite(RELAYPIN, HIGH); // Turn on the fan
} else {
digitalWrite(RELAYPIN, LOW); // Turn off the fan
}
// Door Control and Motion Counter
if (motionDetected) {
servo.write(90); // Open the door
motionCounter++; // Increment counter for each detection
delay(1000); // Debounce delay for motion detection
} else {
servo.write(0); // Close the door
}
// Emergency Buzzer
if (temperature > 40.0) { // Example condition for an emergency
digitalWrite(BUZZERPIN, HIGH); // Sound the buzzer
} else {
digitalWrite(BUZZERPIN, LOW); // Turn off the buzzer
}
// Displaying values on LCD
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Temp: ");
lcd.print(temperature);
lcd.print(" C");
lcd.setCursor(0, 1);
lcd.print("Hum: ");
lcd.print(humidity);
lcd.print(" %");
lcd.setCursor(10, 1);
lcd.print("Count:");
lcd.print(motionCounter);
delay(2000); // Update every 2 seconds
}