A
Arduino•4mo ago
! wotZ

Motion Controlled Servo

https://wokwi.com/projects/433217542086664193 #include <Servo.h> // Include the Servo library const int pirPin = 6; // Digital pin connected to the PIR sensor const int buzzerPin = 8; // Digital pin connected to the passive buzzer const int servoPin = 3; // Digital pin connected to the servo motor Servo myServo; // Create a Servo object void setup() { 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 == 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) delay(500); // Keep buzzer on for 0.5 seconds digitalWrite(buzzerPin, LOW); // Turn off the buzzer myServo.write(90); // Rotate servo to 90 degrees Serial.println("Action completed. Waiting for next motion..."); delay(500); // Add a small delay after an event to avoid rapid re-triggering } 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 } }
Motion Controlled Servo - Wokwi ESP32, STM32, Arduino Simulator
Run IoT and embedded projects in your browser: ESP32, STM32, Arduino, Pi Pico, and more. No installation required!
3 Replies
AnonEngineering
AnonEngineering•4mo ago
Forgot Serial.begin() in setup 🙂
! wotZ
! wotZOP•4mo ago
it had error i didnt tried to fix it so i deleted it xd
AnonEngineering
AnonEngineering•4mo ago
with a few more lines you can do "State Change Detection" https://wokwi.com/projects/433222484144176129

Did you find this page helpful?