#include <Servo.h>
#include <DHT.h>
#define DHTPIN 6 // Pin where the DHT11 is connected
#define DHTTYPE DHT11 // DHT 11
#define PIRPIN 2 // Pin where the PIR motion sensor is connected
#define LDRPIN A0 // Pin where the LDR is connected
#define LED1 3 // Pin where the first LED is connected
#define LED2 4 // Pin where the second LED is connected
#define SERVOPIN 9 // Pin where the servo is connected
#define RELAYPIN 7 // Pin where the relay is connected
#define BUZZERPIN 8 // Pin where the buzzer is connected
DHT dht(DHTPIN, DHTTYPE);
Servo myservo;
void setup() {
Serial.begin(9600);
dht.begin();
pinMode(PIRPIN, INPUT);
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
pinMode(RELAYPIN, OUTPUT);
pinMode(BUZZERPIN, OUTPUT);
myservo.attach(SERVOPIN);
myservo.write(0); // Initial position of the servo
}
void loop() {
// Read PIR motion sensor
int pirValue = digitalRead(PIRPIN);
// Read DHT11 sensor
float humidity = dht.readHumidity();
float temperature = dht.readTemperature();
// Read LDR value
int ldrValue = analogRead(LDRPIN);
// Check if motion is detected
if (pirValue == HIGH) {
digitalWrite(LED1, HIGH); // Turn on LED1
myservo.write(90); // Move servo to 90 degrees
digitalWrite(RELAYPIN, HIGH); // Turn on the relay
tone(BUZZERPIN, 1000); // Turn on the buzzer
} else {
digitalWrite(LED1, LOW); // Turn off LED1
myservo.write(0); // Move servo to 0 degrees
digitalWrite(RELAYPIN, LOW); // Turn off the relay
noTone(BUZZERPIN); // Turn off the buzzer
}
// Control LED2 based on LDR value
if (ldrValue < 500) {
digitalWrite(LED2, HIGH); // Turn on LED2 if it's dark
} else {
digitalWrite(LED2, LOW); // Turn off LED2 if it's light
}
// Print sensor values to Serial Monitor
Serial.print(F("Humidity: "));
Serial.print(humidity);
Serial.print(F("% Temperature: "));
Serial.print(temperature);
Serial.print(F("°C Light Level: "));
Serial.println(ldrValue);
// Wait a few seconds between measurements
delay(2000);
}