#include // PIR Motion Sensor connected to Digital Pin D10 const int pirPin = D10; // Optional: LED indicator (D13 is built-in LED on XIAO) const int ledPin = LED_BUILTIN; // Variable to store previous PIR state (for motion detection) int previousState = LOW; void setup() { // Initialize Serial communication at 115200 baud Serial.begin(115200); delay(1000); // Configure PIR pin as INPUT pinMode(pirPin, INPUT); // Configure LED pin as OUTPUT pinMode(ledPin, OUTPUT); digitalWrite(ledPin, LOW); Serial.println("=== PIR (Passive Infrared) Motion Sensor Analysis ==="); Serial.println("Detecting motion via digital HIGH/LOW signal"); Serial.println("PIR pin: D10 (digital input)"); Serial.println("LED pin: D13 (built-in LED - indicates motion)"); Serial.println(""); Serial.println("Waiting for PIR warm-up (30-60 seconds)..."); delay(2000); Serial.println("Time(ms)\tPIR State\tMotion Detected"); Serial.println("=".repeat(50)); } void loop() { // Read digital value from PIR sensor // HIGH = motion detected // LOW = no motion int pirState = digitalRead(pirPin); // Get current timestamp unsigned long currentTime = millis(); // Print current state Serial.print(currentTime); Serial.print("\t"); Serial.print(pirState); Serial.print("\t\t"); if (pirState == HIGH) { Serial.println("YES - Motion Detected!"); digitalWrite(ledPin, HIGH); // Turn on LED } else { Serial.println("NO - No Motion"); digitalWrite(ledPin, LOW); // Turn off LED } // Detect state change (edge detection) if (pirState != previousState) { Serial.print(">>> STATE CHANGE at "); Serial.print(currentTime); Serial.print("ms: "); if (pirState == HIGH) { Serial.println("MOTION DETECTED (LOW → HIGH transition)"); } else { Serial.println("MOTION STOPPED (HIGH → LOW transition)"); } // Update previous state previousState = pirState; } // Delay 500ms between readings (2 readings per second) delay(500); } /* CIRCUIT CONFIGURATION: ====================== PIR Sensor Pin Configuration: - VCC (5V or 3.3V) → Power pin - GND → Ground pin - OUT (Signal) → D10 (digital input pin) - Time Delay Potentiometer (purple): Adjusts hold-on time (3-300 seconds) - Sensitivity Potentiometer (orange): Adjusts detection range (3-7 meters) - Trigger Mode Jumper (pink): Select single/repeated trigger XIAO Configuration: - D10 reads the digital output from PIR - Pull-up resistor typically NOT needed (PIR output is TTL-compatible) - LED on D13 provides visual feedback of motion detection EXPECTED BEHAVIOR: ================== - No motion: Output: LOW (0V), LED: OFF - Motion: Output: HIGH (5V), LED: ON - After timeout: Output: LOW, LED: OFF The DISCRETE HIGH/LOW output is characteristic of DIGITAL signals. No intermediate values—pure binary state change. KEY DIFFERENCES FROM ANALOG (LDR): ================================== 1. PIR: Binary (2 states) vs LDR: Continuous (4096 levels) 2. PIR: No proportional info vs LDR: Proportional to light 3. PIR: Event-based (motion) vs LDR: Measurement-based (intensity) TROUBLESHOOTING: ================ If PIR doesn't detect motion: 1. Wait 30-60 seconds for warm-up period 2. Adjust sensitivity potentiometer (clockwise = more sensitive) 3. Ensure movement is not too slow (PIR detects IR gradient changes) 4. Check voltage: PIR needs 5V (can damage if powered at 3.3V only) */