20 December 2013

PIR Sensor (Part 2)

PIR Border Title
In this tutorial we will connect our HC-SR501 PIR (movement) Sensor to an Arduino UNO. The PIR sensor will be powered by the Arduino and when movement is detected, the PIR sensor will send a signal to Digital Pin 2. The Arduino will respond to this signal by illuminating the LED attached to Pin 13.

PIR Sensor (Part 1) : Showed that this sensor can be used in isolation (without an Arduino). However, I will still demonstrate how you can attach this sensor to the Arduino so that we can move forward to more advanced objectives and concepts.


Video









Parts Required






Fritzing Sketch



PIR Tutorial 2_bb




Arduino Sketch




 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
/*Simple PIR sketch: Written by ScottC, 19th Dec 2013

 http://arduinobasics.blogspot.com/
 
 ----------------------------------------------------*/

void setup(){
 pinMode(13,OUTPUT);
 pinMode(2,INPUT);
}

void loop(){
 digitalWrite(13,digitalRead(2));
}




The sketch above reads the signal coming in from the PIR sensor on Pin 2, and if it reads HIGH, it light up the LED attached to Pin 13. If it reads LOW, it will turn the LED off. This is all controlled by line 13 in the Arduino Sketch above.

The following table helps to identify the purpose of the potentiometers on the PIR sensor. Most people say they use trial and error. I will attempt to reduce the mystery of these components on the PIR board.


104 (Left) – Max


104Left
LED on = 20 sec
LED off = 3 sec

When you move the 104 labelled potentiometer all the way to the left (max position), the LED will remain on for 20 seconds after movement is detected. The 20 seconds is independent of the other potentiometer (105) setting. When the LED turns off, it will remain off for 3 seconds before the sensor will trigger again from any further movement.




104 (Right) – Min



104Right

LED on = 1 sec
LED off = 3 sec

When you move the 104 labelled potentiometer all the way to the right (min position), the LED will remain on for 1 second after movement is detected. When the LED turns off, it will remain off for 3 seconds before the sensor will trigger again from any further movement.




105 (Left) – Max



105Left

Most sensitive – Detects movement from over 10 steps away.

The 105 labelled potentiometer controls the sensitivity of the PIR sensor. When in the left position, the PIR sensor is most sensitive and small amounts of movement will trigger the sensor. It detected my movement (ie a single step to the left of right) from over 10 steps away from the sensor. I was very impressed.




105 (Right) – Min



105Right

Least sensitive: Need significant movement for sensor to trigger. Detects movement from about 4 steps away.

When the 105 labelled potentiometer is twisted to the right, the PIR sensor becomes a lot less sensitive. I needed to take much bigger steps to the left or right to trigger the sensor (about 3 times the size compared to the left position). It also had a lot more trouble detecting movement occurring further away. It only really started to detect my movement when I was about 4 steps away from it. 

My preferred combination was 104-Right (min) + 105-Left (max), which meant that the sensor would remain on for only a short period of time, and detect any subtle movements in the room. This combination is displayed below:


IMG_0697

I have not tested to see how it performs over a very long period with this setting, and whether it would suffer from false positive readings, but that could easily be fixed by turning the 105 labelled potentiometer a bit more to the right.

28 comments:

  1. This comment has been removed by a blog administrator.

    ReplyDelete
    Replies
    1. May plot some sensors soon using
      MegunoLink . Looks interesting.

      Delete
  2. Thank you for explanation.

    ReplyDelete
  3. i have problem, this range not to far, just 10CM, the sensitivity is max. please help me.

    ReplyDelete
    Replies
    1. Are you using the same PIR sensor that I am using?

      Delete
  4. Hi. I just seem to get a regular output of so many Motion detects to no motion detected. Its like its in a loop of 5 high outputs to a bout 20 lows that just repeats? Do you think my sensor is faulty?

    ReplyDelete
    Replies
    1. Sounds very similar to a faulty sensor that I had.

      Delete
  5. Hi !
    I want to connect my PIR sensor to light up a 12v LED lamp whenever a person is detected in a room.
    Is it safe to connect the lamp directly to my arduino ?

    ReplyDelete
    Replies
    1. No - not safe.
      You will need to power the LED lamp with an alternate power supply. The Arduino could control the LED lamp switch using a Relay or MOSFET.... or you may be able to get away with a transistor. It just depends on how much power the LED lamp consumes... You would need to find out which option is best for your project.

      Delete
  6. Hi !
    I'm working in arduino project which will keep an eye on my little dog while I'm not home and send me an alert if he makes an unusual movement. Can I do that with a PIR sensor based on comparing the frequencies it sends ?
    Thank you

    ReplyDelete
    Replies
    1. The sensor that I used, "triggered" when movement was detected. You can adjust the sensitivity of the sensor (as discussed in the tutorial).... but there is nothing really "smart" about this module... ie. it cannot tell whether the movement is "normal" or "unusual". But I guess you can build some smarts within your Arduino program, but you are limited to movement or no movement. By using a number of these sensors, you may be able to identify if the movement is "unusual" - but that is a very vague statement, because I don't really understand what you qualify as unusual.

      Delete
  7. I mean by unusual, any movement that gives a warning signal, for example : being attacked by something : snake maybe since the dog's house is outdoor or any thing else

    ReplyDelete
    Replies
    1. It won't really indicate the amount of movement - only that it moved.

      I am not sure if it would detect a snake - because it is cold blooded??

      If it were me trying to do what you want to do - I would use some sort of web-cam setup.

      Delete
    2. Ok, that's a good idea, with a web-cam I think I could do that.
      Thank you !

      Delete
  8. My arduino uno goes off when I touch the wire attached to pin 2.This is with no PIR attached to the uno. Now it is going off when I get near the uno itself. Any ideas?

    ReplyDelete
    Replies
    1. Floating pin. You need to attach the PIR sensor (while the Arduino is not powered).

      Although I should check, when you say "goes off" - do you mean the LED turns on ? Or do you mean the Arduino turns off ?

      Delete
  9. Hi
    I made the same as connection you have described, but it doesnt work. I have these cheap items from china :
    http://www.ebay.com/itm/251733271073?_trksid=p2060353.m2749.l2649&ssPageName=STRK%3AMEBIDX%3AIT
    http://www.ebay.com/itm/151919077902?_trksid=p2060353.m2749.l2649&ssPageName=STRK%3AMEBIDX%3AIT

    Could it be bad item or where is problem?
    Thanks

    ReplyDelete
    Replies
    1. I dont know why, but after reconnection from usb it started work... I hope it will work now.
      Is possible to change 3 sec LED off some way?

      Delete
  10. I just tried part 1 and it works correctly... when I connect arduino and load sketch it doesnt work and its only blinking led...

    ReplyDelete
  11. When the LED turns off, how to make the sensor remain off only for 1 seconds before the sensor trigger again from any further movement?

    ReplyDelete
  12. do you know how to use this sensor with other sensor? for example ear-clip heart rate sensor

    ReplyDelete
    Replies
    1. Yes - you would have to read them on a separate digital pin from each other. So you would leave one attached to Digital pin 2, and have one of the others attached to another digital pin like D4.
      Then take a reading as per normal.

      Delete
  13. hi scott. nice tutorial.
    btw, how to keep the LED on as long as there's motion detected?

    ReplyDelete
    Replies
    1. Set the 104 potentiometer to the far right. With the code above, this will turn the LED on for 1 second and off for 3 seconds. So the detection cycle (when there is constant movement) is about once every 4 seconds.

      So instead of illuminating the LED based solely on the PIR sensor's output, you would illuminate the LED based on a timer within the Arduino code.

      Poll digital pin 2, and every time you detect movement, you reset a count-down timer.
      The count-down timer must be set to a timeframe greater than 4 seconds. And if you detect movement before the time runs out, you reset the timer (to 6 seconds or whatever you want)and keep the LED on until the timer runs out. When the timer runs down to zero seconds you turn off the LED... but it will never reach zero if motion is detected. Does that make sense?

      Delete
  14. sir iam new to arduino board any step by step basic file.

    ReplyDelete

Feel free to leave a comment about this tutorial below.
Any questions about your particular project should be asked in the ArduinoBasics forum.

Comments are moderated due to large amount of spam.