HC-SR501 passive infrared sensor with Arduino

Got myself a HC-SR501 passive infrared sensor from ebay for less than 5€.
As information about this sensor is hard to find but usage is pretty easy I thought I share my findings.

HC-SR501
HC-SR501: Left poti is to adjust sensitivity. I have set it to medium. Right poti is to adjust the delay. I set it to minimum which means it triggers about every 5 seconds.
HC-SR501
On the bottom are the connections for 5V (left), signal (middle) and GND (right). I tried around with the jumper on the top right, but couldn’t really tell a different behaviour.

I thought the signal pin delivers an analog value according to the distance so I added a LED to pin 11 for the output. This was the initial setup:

#define pir A0
#define led 11

void setup() {
  pinMode(pir, INPUT);
  pinMode(led, OUTPUT);
  Serial.begin(9600);

}

void loop() {
  int i = analogRead(pir);
  int l = map(i,0,1023,0,255);

  Serial.print(i);
  Serial.print(" / ");
  Serial.println(l);
  analogWrite(led, l);

  delay(100);
}

I realized that the value I was reading at the sensor pin was always around 680 not matter what.
I then went on to change analogRead to digitalRead:

#define pir A0
#define led 11

void setup() {
  pinMode(pir, INPUT);
  pinMode(led, OUTPUT);
}

void loop() {
  digitalWrite(led, digitalRead(pir));
  delay(100);
}

And it worked too… but I think going forward I will use analogRead and will react to motion once the value delivered by the PIR is higher than 500.

29 thoughts on “HC-SR501 passive infrared sensor with Arduino”

  1. i am stuck using this sensor for one of my projects, and i will be using a 5V DC input, and as im new to coding in C would you be able to tell me what the H and L pins are required for? and according to your post, the o/p can be either analog or digital? :s

  2. I have got one and thanks to this post I was able to power it because there no pin markings on my module. I have measured the current. At 5 V It uses about 40 uA in static mode and about 150 uA when there is motion. Signal pin was disconnected.

  3. Hi, i’m a novice with this.

    How can I know to limit the distance or range of measure? thanks

    1. Man, please look at the caption of my first picture. It’s the left poti that you have to play around with.

  4. Hi, thanks for the tutorial, it works!

    I’m a newbie of these and I have a question about the code. Is there anyway to make it output “1″ when the sensor senses an object (instead of just lighting up the LED). This sensor is used in a sensor network and we need to gather the information it gets (the LED output can only be read by people but not PC).

    Please help! Thanks a lot!!!

    1. Hi Jane
      what do you mean by “output 1″? lighting up an LED is nothing else than writing a “1″ to an output pin.

      1. I mean to have it output “1″ in the monitor in the arduino software, so that if I sees a “1″ on the screen, I know there is an object. The code you provided is very concise so that I have no idea where to add a println statement. Could you please help?

        1. Jane, just make sure you add
          Serial.begin(9600);
          to the setup() function.

          and then the loop() should look like

          void loop() {
          int val = digitalRead(inPin);
          if (val == HIGH) {
          Serial.println(“1″);
          } else {
          Serial.println(“0″);
          }
          }

          NOTE: This is untested! You probably need to adapt this.

  5. hi, my project using this sensor model. but i use PIC16f as it microcontroller.. do u have sample coding for this pir sensor using pic16f?

  6. Actually the PIR outputs a digital signal no matter how much movement there is. Its either a 1 (movement detected) or 0 (no movement). So, analog is of no extra use here.
    P.S: My PIR came without markings of 5V or GND or Trigger pin. Thanks to your pics I was able to figure it out.

  7. Thanks so much for this! I finally got around to trying these things out, and this made it very simple to get it started! I’ve got a really unnecessary but cool project planned, using 2 PIR sensors.
    Thanks again!

  8. Hi
    You set the poti to minimum which means it triggers about every 5 seconds, and it really too slow for my needs.
    Is it possible to trigger the sensor every 1 sec or even less?
    Did you try?
    thanks
    Ben

    1. Sorry, I guess it’s the minimum for this sensor. Not sure about other sensors out there.

  9. Thank you so much!

    I’ve been thinking that my sensor is broken. I always got xxx number from it and never ZERO.
    Sooo…

    The problem was that instead pin = 0 i should use pin = A0.

    I think it listened the wrong port!

    Thanks again! :)

    1. miranda, those are analog values from the sensor.

      the code in the loop function is being executed for ever. this means – and because of the delay(100) statement – that you will get 10 readings from the PIR sensor per second.

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>