Introduction: Add Motion Detection to Your Arduino Home Security Project Using HC-SR505

Hi folks. We've been working on a home automation/security project where we've used the HC-SR505 PIR motion sensor instead of the usual HC-SR501 sensor that we usually use. We have noticed that the HC-SR505 doesn't have much coverage on Instructables, so, we at TechDepot Egypt decided to prepare this instructable to cover the HC-SR505 operation and required code.

You will need for this instructable the following:

- 1 x Any Arduino board such UNO, which we will be using for this instructable, still, any Arduino board would do.

- 1 x USB programming cable that matches your Arduino port

- 1 x HC-SR505, PIR motion sensor

- 3 x Dupont Male-Female wire connectors, they would look like these

For those of you who are new to Arduino and DIY electronics projects, please stay with us for an intro about the PIR motion sensors. For the old Android veterans who just want to take a look on the HC-SR505 please step forward.

PIR stands for Passive InfraRed, it is also known as Pyroelectric Infrared, which when followed by "Sensor" would mean a device used to sense the infrared (IR) radiated by warm bodies. OMG, is my body radiating Infrared!!!... Actually, YES, you do, and also any warm body does.

Away of the deep technical mumbo jumbo (if you need the mumbo jumbo, please check here), a PIR sensor consists of two parts. The actual sensor which is shown on the left side in the second picture above., and a plastic cover which consists of a number of small lenses used to focus and emphasise the IR radiation, in addition to the electronic components needed to drive the sensor on the back of the electronic board.

in the HC-SR505 (first image), a mini sensor is fitted on the electronic board and covered with a mini plastic cover.

Step 1: So, What Is the HC-SR505?

The HC-SR505 is the little brother of the HC-SR501 which means it sure lacks some of the HC-SR501 functionality such as:

1- Single/Repeated triggering is not adjustable. The HC-SR505 triggering is repeatedly by default

2- The HC-SR501 range is around 7 meters while the HC-SR505 is around 3 - 4 meters

3- The delay between repeated triggering is not adjustable

Still, the HC-SR505 enjoy some features where it outbeats the older brother such as:

1- Size

2- Very low current consumption which is around 60 uAmps

3- As reliable as the older brother

Step 2: HC-SR505 Connection

The connection is so easy and straight forward with the help of the marks printed on the sensor board, +, S, -.

The S here is the signal (Out), which is a digital signal, so when there is a motion within range the signal is HIGH at 3.3 volts, and when the motion is over it goes LOW at 0 or around 0 volts.

So, simply use the 3 Dupont cables to connect the (+) pin for the sensor to the Arduino 5V, the (-) to the Arduino GND, and let's connect the (Out) in the sensor to digital pin 8 in Arduino (D8).

Finally, connect the USB cable between your Arduino and computer, open the Arduino IDE, choose your Arduino board type and the port you used for connection from the Tools in the menu bar. You are now good to go Code diving :)

Step 3: Understand the Concept Behind the Code!

Ok, since the main target of Instructables is Learning, so, let's see how that code would go.

It is a good practice in Arduino programming to identify the pins you are going to use. The most common ways to do so is by either using:

#define pirPin 8 // Note that you DO NOT end this by semicolon (;)


const int pirPin = 8;

I prefer the second one as the type is identified and if you tend to use the pirPin with a wrong variable type it will give you an error while using the #define it will do an action that is simply similar to "Find and Replace" so, before compiling it will go through the code and replace each "pirPin" it finds with the digit 8.

Also, it is a good practice whenever you use a sensor is to have a "Delay" between the occurrences of polling the sensor for Output to give the sensor the time to recover between readings and also, as the Arduino is operating at a very high frequency, so, polling the motion sensor every few microseconds (1/million of second) or milliseconds (1/1000 of a second) is not needed, nothing will change that fast :). But if we use the normal delay command in Arduino this means you are ordering the Arduino to idle for that period. Example:

if (digitalRead(pirPin) == HIGH) { // Check if the sensor Out pin is HIGH, i.e. a movement is detected

// Do some code, like ring an alarm or send alert SMS to your mobile... etc


delay (5000); // delay for 5000 milliseconds (5 seconds as millisecond is 1/1000 of a second)

During the delay command, Arduino is not doing anything and in most security systems you would have several other sensors, not just motion sensor. You may have a Gas leakage sensor, or a flame sensor, and definitely, if you receive a signal from one sensor, you would need Arduino to continue checking other sensors while this one recovers.

Accordingly, we are going to use the timer technique instead of the normal delay. How is that?

First we will assign 3 variables, 2 to hold time and one boolean. Variables that should hold time are of the type Long, and since time is measured from the second Arduino turned on, so, there is no place for negative values. Accordingly:

unsigned long motionDelay = 5000; // This is the delay needed between polling. 5 seconds here.

unsigned long motionTimer; // This will act as a holder for the time at which we polled the sensor

boolean inMotion = false; // This is flag that goes up (true) during the period we don't need to poll the sensor

Now the logic behinde the process is as follows (this is a description, not code, do not copy and paste):

if(Sensor Out Reading is HIGH (there is motion happening) AND NOT inMotion) do the following:

1- Do the code when motion happens, may be activate a relay ti switch on an alarm or send SMS alert to your mobile....etc

2- Read the current time and store in motionTimer

3- Raise the inMotion flag

Else if (if the time now - motionTimer is eaqual or even more than the delay set in the motionDelay variable) do the following:

1- Do the code required when motion is not being read (if required)

2- Lower the inMotion flag

If you feel comfortable with this step, move to the full code in next step.

Step 4: The Final Code Is Here!

Ok, let's put the last step in action and do what we've discussed:

const int pirPin = 8; // This is the D8 pin that we are going to use

unsigned long motionDelay = 5000; // Motion Delay Timer
unsigned long motionTimer; // Motion Trigger Timer

boolean inMotion = false; // Motion sensor need to be read or not flag

void setup() {
// put your setup code here, to run once:

pinMode(pirPin, INPUT); // Prepare the pin as Input pin

Serial.begin(19200); // Prepare the Serial Monitor


void loop() {

// put your main code here, to run repeatedly:

if (digitalRead(pirPin) == HIGH && !inMotion)


Serial.println("Motion Detected");

motionTimer = millis();

inMotion = true;

} else if (millis() - motionTimer >= motionDelay) {

inMotion = false;



Step 5: Wrap Up

Hope this instructable is useful and try to apply the timer concept to other sensors you add. Also, feel free to check my other instructable regarding connecting a GSM to an Arduino Mega.

Thanks for following :)


AshishR25 made it!(author)2017-05-07

Are connections same

TechDepot+Egypt made it!(author)2017-05-07

For connection please checkout this image:

AshishR25 made it!(author)2017-05-07


TechDepot+Egypt made it!(author)2017-05-07

Hi AshishR25, it is the same, no change. Use this same code.

AshishR25 made it!(author)2017-05-07

Can u send me programme by using PIR HC-sr501

TechDepot+Egypt made it!(author)2017-05-07

Hi AshishR25, it is the same, no change. Use this same code.

aguarobot made it!(author)2017-04-16

Thanks for the info.

TechDepot+Egypt made it!(author)2017-04-16

You are most welcome :)

About This Instructable




Bio: TechDepot Egypt is an online retail store that sells the components to bring your electronics projects ideas to life. We are not just components sellers ... More »
More by TechDepot Egypt:Add motion detection to your Arduino home security project using HC-SR505Receive alerts and messages from your Arduino MegaVery Simple HC-SR04 Connection to Arduino Example
Add instructable to: