#include <Servo.h>
int pirPin = 2; // PIR sensor input pin
int servoPin = 9; // Servo motor output pin
int pos = 0; // variable to store servo position
int pirState = LOW; // variable to store PIR sensor state
Servo myservo; // create servo object to control a servo
void setup() {
pinMode(pirPin, INPUT); // initialize PIR sensor pin as input
pinMode(servoPin, OUTPUT); // initialize servo motor pin as output
myservo.attach(servoPin); // attach the servo to the specified pin
myservo.write(pos); // initialize the servo to starting position
Serial.begin(9600); // initialize serial communication
}
void loop() {
pirState = digitalRead(pirPin); // read PIR sensor state
if (pirState == HIGH) { // if PIR sensor detects motion
Serial.println("Motion detected!");
for (pos = 0; pos <= 90; pos += 1) { // move servo to 90 degrees
myservo.write(pos); // set servo position
delay(15); // wait for servo to reach position
}
for (pos = 90; pos >= 0; pos -= 1) { // move servo back to starting position
myservo.write(pos); // set servo position
delay(15); // wait for servo to reach position
}
} else {
Serial.println("No motion detected.");
myservo.write(0); // move servo to starting position
}
delay(100); // wait for a short time before repeating loop
}
uno:A5.2
uno:A4.2
uno:AREF
uno:GND.1
uno:13
uno:12
uno:11
uno:10
uno:9
uno:8
uno:7
uno:6
uno:5
uno:4
uno:3
uno:2
uno:1
uno:0
uno:IOREF
uno:RESET
uno:3.3V
uno:5V
uno:GND.2
uno:GND.3
uno:VIN
uno:A0
uno:A1
uno:A2
uno:A3
uno:A4
uno:A5
servo1:GND
servo1:V+
servo1:PWM
pir1:VCC
pir1:OUT
pir1:GND