Categories

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.

 

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.

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);
}

Links

HC-SR501 Human Sensor Module Pyroelectric on Amazon UK

HC-SR501 Human Sensor Module Pyroelectric on Amazon US

Share

Comments are closed.