Click here to Skip to main content
11,932,103 members (60,054 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


9 bookmarked

Using PIR Motion Detector with Arduino

, 12 Jan 2014 CPOL
Rate this:
Please Sign up or sign in to vote.
A library for PIR motion sensor


I think cooking-hacks sold me their last PIR motion sensor of this model, they made it 'retired' Smile | :) Whatever, I like writing libraries, and also wrote for this one.

In the datasheet, 12v Vcc is advised, but also good with 5v. Therefore, you can connect it directly to your Arduino board without any need for an external power source.

One last point, use Arduino built-in pullup resistor for the digital input pin if you don't have an external resistor.

Using the Code

As always, place the library files in the libraries folder of the Arduino IDE. Then, you have a go.

// include library
#include <PIRMotion.h>
// connected to digital2, use internal pullup
PIRMotion pirm(2,1);

Specify callback function for motion event in the setup.


Implement the callback function.

void pirmCallback(void) {

And don't forget to monitor the sensor in the main loop.

void loop() {

You have motion sensing capability now.

I have included one more function to the library to be able to add a time lag after a motion. For example, one may want to turn the lights on when somebody comes in, hold the lights on for 5 minutes, then start to monitor again to see if anybody wanders.

pirm.setLagAfterMotion(5*60); // stops monitoring for 5 minutes


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Vedat Ozan Oner
Turkey Turkey
No Biography provided

You may also be interested in...

Comments and Discussions

-- There are no messages in this forum --
| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.151126.1 | Last Updated 12 Jan 2014
Article Copyright 2014 by Vedat Ozan Oner
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid