Home LearningBasics PIR sensor example

PIR sensor example

by shedboy71

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.

 

Layout

 

arduino and PIR

arduino and PIR

 

Schematic

 

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()
{
Serial.begin(9600);
}

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

delay(1000);
}

[/codesyntax]

 

Links

HC-SR501 Human Sensor Module Pyroelectric on Amazon UK

HC-SR501 Human Sensor Module Pyroelectric on Amazon US

Share

You may also like