Arduino Lesson 17: Using a PIR

Overview

PIR sensors allow you to sense motion, almost always used to detect whether a human has moved in or out of the sensors range. They are small, inexpensive, low-power, easy to use and don’t wear out. For that reason they are commonly found in appliances and gadgets used in homes or businesses. They are often referred to as PIR, “Passive Infrared”, “Pyroelectric”, or “IR motion” sensors.

FXYEQAUFYIZHAJN.LARGE

Parts

To build the projects described in this lesson, you will need the following parts.

αρχείο λήψης

1 PIR Motion Sensor

learn_arduino_breadboard_half_web.jpg

Half-size Breadboard

1

learn_arduino_uno_r3_web.jpg

Arduino Uno R3

1

learn_arduino_jumpers_web.jpg

Jumper wire pack

1

 

How PIRs Work?

The PIR sensor itself has two slots in it, each slot is made of a special material that is sensitive to IR.
The lens used here is not really doing much and so we see that the two slots can ‘see’ out past some distance (basically the sensitivity of the sensor).
When the sensor is idle, both slots detect the same amount of IR, the ambient amount radiated from the room or walls or outdoors. When a warm body like a human or animal passes by, it first intercepts one half of the PIR sensor, which causes a positive differential change between the two halves.
When the warm body leaves the sensing area, the reverse happens, whereby the sensor generates a negative differential change. These change pulses are what is detected.
proximity_pirdiagram

The Breadboard Layout

Most PIR modules have a 3-pin connection at the side or bottom.

learn_arduino_breadboard

Connecting PIR sensors to a microcontroller is really simple. The PIR acts as a digital output so all you need to do is listen for the pin to flip high (detected) or low (not detected).

Power the PIR with 5V and connect ground to ground. Then connect the output to a digital pin. In this example we’ll use pin 7.

To connect your PIR sensor to your arduino follow the instructions by the image above.

VCC Pin to +5V of Arduino and GND Pin to GND (Ground) of your Arduino Board. Then connect the signal pin to the digital 7 pin of Arduino.

 

Arduino Code

The code is very simple, and is basically just keeps track of whether the input to pin 7 is high or low. It also tracks the state of the pin, so that it prints out a message when motion has started and stopped.

 

  1. /*
  2. * PIR sensor tester
  3. */
  4. int ledPin = 13; // choose the pin for the LED
  5. int inputPin = 7; // 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. pinMode(ledPin, OUTPUT); // declare LED as output
  10. pinMode(inputPin, INPUT); // declare sensor as input
  11. Serial.begin(9600);
  12. }
  13. void loop(){
  14. val = digitalRead(inputPin); // read input value
  15. if (val == HIGH) { // check if the input is HIGH
  16. digitalWrite(ledPin, HIGH); // turn LED ON
  17. if (pirState == LOW) {
  18. // we have just turned on
  19. Serial.println(“Motion detected!”);
  20. // We only want to print on the output change, not state
  21. pirState = HIGH;
  22. }
  23. } else {
  24. digitalWrite(ledPin, LOW); // turn LED OFF
  25. if (pirState == HIGH){
  26. // we have just turned of
  27. Serial.println(“Motion ended!”);
  28. // We only want to print on the output change, not state
  29. pirState = LOW;
  30. }
  31. }
  32. }
Με ετικέτα: , , , ,

Leave a Reply

Your email address will not be published. Required fields are marked *

*