#include <Servo.h>
#define PIR_PIN 6 // Pin connected to the PIR sensor
#define SERVO_PIN 3 // Pin connected to the servo motor
Servo servo; // Create a servo object to control the servo motor
int angle = 0; // Initial angle for the servo
void setup() {
pinMode(PIR_PIN, INPUT); // Set the PIR pin as input
servo.attach(SERVO_PIN); // Attach the servo to its pin
servo.write(angle); // Set the initial position of the servo
Serial.begin(9600); // Start serial communication for debugging
}
void loop() {
int motion = digitalRead(PIR_PIN); // Read the PIR sensor
if (motion == HIGH) { // If motion is detected
Serial.println("Motion detected!");
servo.write(180); // Rotate the servo to 180 degrees
delay(1000); // Wait for the servo to reach its position
while(digitalRead(PIR_PIN) == HIGH) {} // Wait until motion stops
servo.write(0); // Rotate the servo back to 0 degrees
}
}