#include <Servo.h> // Required library for servo control
const int PIR_PIN = 8; // PIR sensor signal pin
const int SERVO_PIN = 2; // Servo motor signal pin
Servo myServo; // Create servo object
void setup() {
Serial.begin(9600);
pinMode(PIR_PIN, INPUT); // Set PIR as input
myServo.attach(SERVO_PIN); // Connect servo to pin 9
myServo.write(0); // Initial position
delay(5000); // Warm-up time for PIR sensor (approx. 10-60s)
}
void loop() {
int motionDetected = digitalRead(PIR_PIN); // Read sensor state
if (motionDetected == HIGH) {
Serial.println("gate closed ");
myServo.write(90);
delay(1000);
myServo.write(0);
delay(1000);// Rotate to 90 degrees
}
else {
Serial.println("No motion.");
myServo.write(0); // Return to 0 degrees
}
delay(2000); // Small delay for stability
}