/*
Arduino | project-showcase
Motion sensor
EmiR60GALA June 8, 2025 — 3:29PM
https://wokwi.com/projects/433217542086664193
*/
#include <Servo.h> // Include the Servo library
const int pirPin = 9; // Digital pin connected to the PIR sensor
const int buzzerPin = 8; // Digital pin connected to the passive buzzer
const int servoPin = 7; // Digital pin connected to the servo motor
int oldPirState = 0;
Servo myServo; // Create a Servo object
void setup() {
Serial.begin(9600);
pinMode(pirPin, INPUT); // Set PIR sensor pin as input
pinMode(buzzerPin, OUTPUT); // Set buzzer pin as output
myServo.attach(servoPin); // Attach the servo motor to the specified pin
myServo.write(0); // Initialize servo to 0 degrees
}
void loop() {
int pirState = digitalRead(pirPin); // Read the state of the PIR sensor
if (pirState != oldPirState) {
oldPirState = pirState;
if (pirState == HIGH) { // If motion is detected
Serial.println("Motion detected!");
//digitalWrite(buzzerPin, HIGH); // Turn on the buzzer (makes sound if it's a passive buzzer and you're controlling it with HIGH/LOW)
tone(buzzerPin, 440);
myServo.write(90); // Rotate servo to 90 degrees
Serial.println("Action completed. Waiting for next motion...");
} else {
// No motion detected, do nothing or you can add code here for no motion state
Serial.println("No motion"); // Uncomment this line if you want to see "No motion" continuously
//digitalWrite(buzzerPin, LOW); // Ensure buzzer is off when no motion
noTone(buzzerPin);
myServo.write(0);
}
}
}