PIR sensor example

In the following example we will demonstrate the HC-SR501 PIR sensor, this sensor has many nice features including the ability to be driven by 5v, low cost (£2 to £3), it has a decent range (7 metres) and a fairly small size. These sensors can take anything between 10-60 seconds to warm up, so you should either avoid motion at this point or in your code you would factor this in.

Connecting these to an Arduino is really simple, connect the VCC and GND up and then connect the output to a digital pin, in our example we connect to pin 8.

A passive infrared sensor (PIR sensor) is an electronic sensor that measures infrared (IR) light radiating from objects in its field of view. They are most often used in PIR-based motion detectors.




arduino and PIR
arduino and PIR




arduino and PIR schematic
arduino and PIR schematic


Code example

This basic example will display a different output depending on whether motion is detected.

[codesyntax lang=”cpp”]

int pirPin = 8;
int val;

void setup()

void loop()
val = digitalRead(pirPin);
//low = no motion, high = motion
if (val == LOW)
Serial.println(“No motion”);
Serial.println(“Motion detected – ALARM”);





HC-SR501 Human Sensor Module Pyroelectric on Amazon UK

HC-SR501 Human Sensor Module Pyroelectric on Amazon US

  • Facebook
  • Twitter
  • Linkedin
  • Pinterest
This div height required for enabling the sticky sidebar
Ad Clicks : Ad Views : Ad Clicks : Ad Views :