Advertisement
microrobotics

HC-SR505 passive infrared (PIR) motion sensor

Mar 30th, 2023
3,288
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. The HC-SR505 is a passive infrared (PIR) motion sensor that detects motion in its field of view. Here's an example code in Arduino that reads the motion detection status from the sensor:
  3.  
  4. Note: This code assumes that the HC-SR505 PIR motion sensor is connected to pin 2 of the Arduino board. If you're using a different pin, you'll need to modify the SENSOR_PIN definition accordingly. Also, note that the HC-SR505 is a digital sensor that outputs a high signal when motion is detected and a low signal when no motion is detected, so it can be read directly using the digitalRead() function. Finally, note that the HC-SR505 has a limited detection range and may not detect motion outside of its field of view.
  5. */
  6.  
  7.  
  8. const int SENSOR_PIN = 2;  // the pin connected to the sensor
  9. int motion_detected = 0;  // 1 if motion detected, 0 otherwise
  10.  
  11. void setup() {
  12.   Serial.begin(9600);
  13.  
  14.   pinMode(SENSOR_PIN, INPUT);
  15. }
  16.  
  17. void loop() {
  18.   // read the motion detection status from the sensor
  19.   motion_detected = digitalRead(SENSOR_PIN);
  20.  
  21.   // print the motion detection status
  22.   if (motion_detected) {
  23.     Serial.println("Motion Detected");
  24.   } else {
  25.     Serial.println("No Motion Detected");
  26.   }
  27.  
  28.   // delay before reading from the sensor again
  29.   delay(1000);
  30. }
  31.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement