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.

39 thoughts on “HC-SR501 passive infrared sensor with Arduino

  1. Pingback: Spycam with Arduino / Digispark | Hacking – DIY and Coding

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

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

    • 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!!!

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

      • 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?

        • 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

  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! 🙂

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

  10. Hi. I’m having a problem with my HC-SR501 Sensor. I am getting 3.3V on the signal pin all the time as soon as I power it up. It doesn’t change during the power up sequence or ever after. What could be wrong?

    • I just tried a backup unit and it works fine so I guess the one unit is defective? Guess you get what you pay for 🙂

      • Hi Glen.
        So, Is it possible to power HC-SR501 with 3.3v and it works?.
        It seems that a power supply of 5v are indicated in all datasheet i have found.

  11. Hello and thanks for the concept code. I am so going to scare the hell out of my neighbours cats when they enter my shed to do their needs. Just need a Nano, some tweeter, a led strobe…

  12. Hey Roman,

    I want to bridge the button of the remote control GoPro to make a video when something moves, and stop when no more moves.

    The Digispark / Arduino should control an optocoupler to “press” the remote control button.

    How can I get a impulse (200 ms) from the Digispark / Arduino when motion is detected?
    And again a pulse (200 ms) when no movement is detected to stop the recording?

    Thank you very much! 🙂

  13. Pingback: DuinoLab Starter Kit #14 : PIR Sensor (Motion Detector) – DuinoLab.com

  14. Hi everyone, help me please, I bought this sensor and the 5V relay, but sensor TRUE is the 3.3V and relay not working, I tried connect the 2n2222a transistor (E – relay, B – sensor 3.3V, C – 5V), but the output of the E is same 3.3V, what I do wrong, I need the 5V output on the TRUE

Leave a Reply

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