Advertisement
makispaiktis

ArduinoBasics - 6. PIR sensor, buzzer and LED

Mar 21st, 2021 (edited)
897
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /// A passive infrared sensor (PIR sensor) is an electronic sensor
  2. // that measures infrared (IR) light radiating from objects in its field of view.
  3. // They are most often used in PIR-based motion detectors. PIR sensors are commonly
  4. // used in security alarms and automatic lighting applications.
  5. // PIR sensors detect general movement, but do not give information on who or what moved.
  6.  
  7. int ledPin = 9;
  8. int buzzerPin = 10;
  9. int sensorPin = 11;
  10. bool isSensorDetecting;
  11. // bool state;
  12.  
  13. void setup()
  14. {
  15.   pinMode(sensorPin, INPUT);
  16.   pinMode(ledPin, OUTPUT);
  17.   pinMode(buzzerPin, OUTPUT);
  18. }
  19.  
  20. void loop()
  21. {
  22.   isSensorDetecting = digitalRead(sensorPin);
  23.   // The sensor can understand i an object exists or no (0 or 1)
  24.   if(isSensorDetecting == true){
  25.     digitalWrite(ledPin, HIGH);
  26.     tone(buzzerPin, 1000);
  27.     delay(500);
  28.     // I will make the reverse thing in order to have sth like alarm
  29.     digitalWrite(ledPin, LOW);
  30.     noTone(buzzerPin);
  31.     delay(500);
  32.   }
  33.   else{
  34.     digitalWrite(ledPin, LOW);
  35.   }
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement