Arduino with PIR sensor - ElectroEngineers

Destination to project makers.

Monday, 21 August 2017

Arduino with PIR sensor

PIR sensor is the motion detection sensor that detects variation in the IR energy of the environment. It works from 3 to 7 metres of lenght based on the calibration. It has got good accuracy and the sensor is very cheap. It can be used for alive human ditection, automatic door opens and for security alarms.
PIR sensor is a digital sensor which has three pins. vcc, ground and Dout. It can be directly interfaced with arduino. Data out can be connected to any of the digital pins.

Components required

  1. Arduino
  2. PIR sensor
  3. Jumper wires
  4. Led 

Step 1 Pin connections
           Vcc - 5v
          Dout- any digital pin
          Pin 13 - Led

Step 2. Dump the below code to arduino

The code is tested for its functionality

Whenever motion is detected by PIR, the LED will turn ON. The glowing interval depends on the calibration of PIR sensor. Before trying, check out the calibration of the sensor.
  1. /*
  2. * PIR sensor tester
  3. */
  4. int ledPin = 13; // choose the pin for the LED
  5. int inputPin = 9; // choose the input pin (for PIR sensor)
  6. int pirState = LOW; // we start, assuming no motion detected
  7. int val = 0; // variable for reading the pin status

  8. void setup() 
  9. {
  10. pinMode(ledPin, OUTPUT); // declare LED as output
  11. pinMode(inputPin, INPUT); // declare sensor as input
  12. Serial.begin(9600);
  13. }
  14. void loop()
  15. {
  16. val = digitalRead(inputPin); // read input value
  17. if (val == HIGH) // check if the input is HIGH
  18. {                   
  19.     digitalWrite(ledPin, HIGH);    // turn LED ON
  20. if (pirState == LOW) 
  21.     {
  22.                                 // we have just turned on
  23. Serial.println("Motion detected!");
  24. // We only want to print on the output change, not state
  25. pirState = HIGH;
  26. }
  27. else
  28. {
  29. digitalWrite(ledPin, LOW); // turn LED OFF
  30. if (pirState == HIGH)
  31.    {
  32. // we have just turned of
  33. Serial.println("Motion ended!");
  34. // We only want to print on the output change, not state
  35. pirState = LOW;
  36. }
  37. }
  38. }

No comments:

Post a Comment

Followers