This circuit is designed to detect motion using a PIR motion sensor and respond by turning on a red LED, activating a relay, and rotating a servo motor. When motion is detected, the red LED turns on for 10 seconds. After this period, the relay is activated, and the servo motor rotates 90 degrees for 3 seconds before returning to its original position.
PIR Motion Sensor (Wokwi Compatible)
LED: Two Pin (red)
Servo (Wokwi Compatible)
Arduino UNO
RELAY-G5D
LED: Two Pin (green)
/*
* This Arduino Sketch controls a circuit with a PIR motion sensor, a red LED,
* a relay, and a servo motor. When motion is detected by the PIR sensor, the
* red LED turns on. After 10 seconds, the relay is activated, and the servo
* motor rotates 90 degrees for 3 seconds before returning to its original
* position.
*/
#include <Servo.h>
// Define pin connections
const int pirPin = 13; // PIR sensor output connected to D13
const int redLedCathode = 12; // Red LED cathode connected to D12
const int redLedAnode = 11; // Red LED anode connected to D11
const int relayPin = 9; // Relay coil connected to D9
const int servoPin = 10; // Servo PWM connected to D10
Servo myServo;
void setup() {
// Initialize pin modes
pinMode(pirPin, INPUT);
pinMode(redLedCathode, OUTPUT);
pinMode(redLedAnode, OUTPUT);
pinMode(relayPin, OUTPUT);
myServo.attach(servoPin);
// Ensure initial states
digitalWrite(redLedCathode, LOW);
digitalWrite(redLedAnode, LOW);
digitalWrite(relayPin, LOW);
myServo.write(0);
}
void loop() {
if (digitalRead(pirPin) == HIGH) { // Motion detected
digitalWrite(redLedCathode, HIGH); // Turn on red LED
delay(10000); // Wait for 10 seconds
digitalWrite(relayPin, HIGH); // Activate relay
myServo.write(90); // Rotate servo to 90 degrees
delay(3000); // Wait for 3 seconds
myServo.write(0); // Rotate servo back to 0 degrees
delay(1000); // Wait for 1 second
digitalWrite(relayPin, LOW); // Deactivate relay
digitalWrite(redLedCathode, LOW); // Turn off red LED
}
}
This code initializes the necessary pins and sets their modes. In the loop
function, it continuously checks for motion detected by the PIR sensor. When motion is detected, it turns on the red LED, waits for 10 seconds, activates the relay, rotates the servo motor to 90 degrees, waits for 3 seconds, and then returns the servo motor to its original position before turning off the relay and the red LED.