// Simple Arduino Motion Detector with LED and Servo
// Include the Servo library
#include <Servo.h>
// Define pins
int myPIR = 6; // PIR motion sensor pin
int myLED = 2; // LED pin
int servoPin = 3; // Servo pin (using the previous buzzer pin)
// Create servo object
Servo myServo;
// Variable to store motion state
int motionState = 0;
void setup() {
// Start serial communication
Serial.begin(9600);
// Setup pin modes
pinMode(myPIR, INPUT);
pinMode(myLED, OUTPUT);
// Attach servo to pin
myServo.attach(servoPin);
// Initialize servo to 0 degrees
myServo.write(0);
// Initial message
Serial.println("Motion detector with servo ready!");
}
void loop() {
// Read PIR sensor
motionState = digitalRead(myPIR);
if (motionState == HIGH) {
// Motion detected
digitalWrite(myLED, HIGH); // Turn on LED
myServo.write(90); // Move servo to 90 degrees
Serial.println("Light on");
}
else {
// No motion
digitalWrite(myLED, LOW); // Turn off LED
myServo.write(0); // Move servo to 0 degrees
Serial.println("Light off");
}
delay(500); // Short delay
}