#include <Servo.h>
// Create a Servo object
Servo servoMotor;
// Define PIR sensor input pin
int pirSensorPin = 3; // You can change this pin if needed
void setup() {
// Attach the servo to pin 9
servoMotor.attach(9); // You can change the pin to which the servo is connected
// Initialize PIR sensor as an input
pinMode(pirSensorPin, INPUT);
// Initialize serial communication for debugging
Serial.begin(9600);
}
void loop() {
// Read the PIR sensor value (HIGH if motion is detected, LOW otherwise)
int pirValue = digitalRead(pirSensorPin);
if (pirValue == HIGH) {
// Motion detected, move the servo to a specific angle
servoMotor.write(90); // 90 degrees (adjust as needed)
Serial.println("Motion detected!");
delay(5000); // Delay to prevent continuous triggering
} else {
// No motion, move the servo to another angle (e.g., 0 degrees)
servoMotor.write(0);
Serial.println("No motion.");
}
}