Advertisement
YourMain12

Basic Motion Sensor

Jul 18th, 2023
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.52 KB | None | 0 0
  1. // Things you need: arduino board, PIR motion sensor module (HC-SR501)
  2.  
  3. const int motionSensorPin = 2;
  4. const int outputPin = 13;
  5.  
  6. void setup() {
  7.   pinMode(outputPin, OUTPUT);
  8.   pinMode(motionSensorPin, INPUT);
  9.   Serial.begin(9600);
  10. }
  11.  
  12. void loop() {
  13.   int motionState = digitalRead(motionSensorPin);
  14.  
  15.   if (motionState == HIGH) {
  16.     digitalWrite(outputPin, HIGH);
  17.     Serial.println("Motion detected!");
  18.   } else {
  19.     digitalWrite(outputPin, LOW);
  20.     Serial.println("No motion detected.");
  21.   }
  22.  
  23.   delay(100);
  24. }
  25.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement