#include <Servo.h>
int pirPin = 2; // digital pin for PIR sensor
int servoPin = 3; // digital pin for servo motor
int ledPin = 4; // digital pin for LED
int buzzerPin = 5; // digital pin for Buzzer
Servo myservo; // create servo object to control a servo
void setup() {
pinMode(pirPin, INPUT);
pinMode(ledPin, OUTPUT);
pinMode(buzzerPin, OUTPUT);
myservo.attach(servoPin); // attaches the servo on pin 3 to the servo object
Serial.begin(9600);
}
void loop() {
int pirValue = digitalRead(pirPin);
if (pirValue == HIGH) { // condition for triggering servo, LED and buzzer
Serial.println("Motion detected!");
myservo.write(90); // move servo to 90 degrees to unlock the door
digitalWrite(ledPin, HIGH);
tone(buzzerPin, 1000);
delay(1000);
digitalWrite(ledPin, LOW);
noTone(buzzerPin);
delay(5000); // keep the door unlocked for 5 seconds
myservo.write(0); // move servo back to 0 degrees to lock the door
}
else {
Serial.println("No motion detected.");
myservo.write(0); // move servo to 0 degrees to lock the door
digitalWrite(ledPin, LOW);
noTone(buzzerPin);
}
}