This Arduino program integrates with PIR (Passive Infrared) motion sensors...

September 3, 2025 at 02:12 PM

#include <Servo.h> // === PIR sensor pins === #define PIR_NORTH 2 #define PIR_EAST 3 #define PIR_SOUTH 4 #define PIR_WEST 5 // === Servo objects === Servo camA; Servo camB; // === Servo pins === #define CAM_A_SERVO 9 #define CAM_B_SERVO 10 // === Camera Angles (adjust to setup) === int angleNorth = 0; int angleEast = 90; int angleSouth = 180; int angleWest = 270; // for 360° servo mapping, or remap to standard servo 0–180° // === Default angle === int defaultAngleA = 90; int defaultAngleB = 90; // === Timing === unsigned long lastDetection = 0; unsigned long detectionTimeout = 5000; // 5 seconds void setup() { // PIR setup pinMode(PIR_NORTH, INPUT); pinMode(PIR_EAST, INPUT); pinMode(PIR_SOUTH, INPUT); pinMode(PIR_WEST, INPUT); // Servo setup camA.attach(CAM_A_SERVO); camB.attach(CAM_B_SERVO); // Move cameras to default position camA.write(defaultAngleA); camB.write(defaultAngleB); Serial.begin(9600); } void loop() { bool motionNorth = digitalRead(PIR_NORTH); bool motionEast = digitalRead(PIR_EAST); bool motionSouth = digitalRead(PIR_SOUTH); bool motionWest = digitalRead(PIR_WEST); if (motionNorth) { Serial.println("Motion North!"); camA.write(angleNorth); lastDetection = millis(); } else if (motionEast) { Serial.println("Motion East!"); camB.write(angleEast); lastDetection = millis(); } else if (motionSouth) { Serial.println("Motion South!"); camA.write(angleSouth); lastDetection = millis(); } else if (motionWest) { Serial.println("Motion West!"); camB.write(angleWest); lastDetection = millis(); } // Return cameras to default if no detection if (millis() - lastDetection > detectionTimeout) { camA.write(defaultAngleA); camB.write(defaultAngleB); } }

This Arduino program integrates with PIR (Passive Infrared) motion sensors and two servo motors to monitor and respond to motion detection in four directions: North, East, South, and West. Here's what the code does:

  1. Hardware Setup:

    • PIR Sensors: Four PIR sensors are connected to digital pins 2, 3, 4, and 5 corresponding to North, East, South, and West directions.
    • Servos: Two servo motors (camA and camB) are controlled via pins 9 and 10. These servos are responsible for moving towards detected motion.
  2. Predefined Angles:

    • The servos are set to move to specific angles depending on the motion direction (angleNorth, angleEast, angleSouth, angleWest).
    • Default positions for the servos are specified (defaultAngleA and defaultAngleB, both set to 90 degrees).
  3. Motion Detection and Servo Response:

    • In the loop function, the program continuously checks for motion signals from the PIR sensors.
    • When motion is detected in a specific direction:
      • It prints a message to the Serial monitor about the motion direction.
      • The corresponding servo (camA or camB) is moved to the predefined angle for that direction.
      • The timestamp of detection is updated using millis().
  4. Timeout for Reset:

    • If no motion is detected for a specified timeout (detectionTimeout, set to 5 seconds), the servos return to their default positions.
  5. Behavior:

    • Upon motion detection, a servo will point towards the respective direction.
    • If no further motion occurs after the timeout duration, the cameras will reset to face their default positions.

Summary:

The code is a motion-tracking system using PIR sensors and servos. It detects motion from four directions, rotates servos to point towards the detected direction, and resets to a default position if no motion is detected for 5 seconds. It can be used for applications such as security cameras or motion-tracking systems.

Generate your own explanations
Download our vscode extension
Read other generated explanations

Built by @thebuilderjr
Sponsored by beam analytics
Read our terms and privacy policy
Forked from openai-quickstart-node