Arduino Basics: PIR Sensor (Part 2)

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.


Parts Required

Fritzing Sketch

PIR Tutorial 2_bb

Arduino Sketch

/*Simple PIR sketch: Written by ScottC, 19th Dec 2013

void setup(){

void loop(){

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

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


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


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


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:


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.


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

  2. Thank you for explanation.

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

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

  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?

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

  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 ?

    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.

  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

    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.

  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

    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.

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

  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?

    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 ?

  9. Hi
    I made the same as connection you have described, but it doesnt work. I have these cheap items from china :

    Could it be bad item or where is problem?

    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?

  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...

  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?

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

    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.

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

    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?

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

  15. i have two 105

  16. this was excellent help thank you


Feel free to leave a comment about this tutorial below.
Any questions about your particular project should be asked on my Arduino Tutorials Discord Server.

Comments are moderated due to large amount of spam.