Detect motion using PIR sensor 🏃‍♂️

Are you always concerned about your safety or do you own valuables?

It is possible to have a guard at the entrance to your house to keep it safe, but this will cost you a lot of money!


The best solution to this problem is to use a motion sensor called a PIR sensor. This sensor can easily detect when someone moves in front of it.


In this tutorial, we will use the PIR sensor to send a message to your computer when someone enters your house.


Overview


You might be wondering how this sensor works; you might believe it has a camera, but in fact it relies on the heat produced by our bodies. This explains why this sensor might respond to your pet.


There are various types of motion sensors, , each of which varies according to the ring and sensitivity. PIR sensors are commonly used in security systems since they may trigger an alert when someone enters a room.


Let's find out more together!

Getting the items


For this project you will need the following components, you can buy them from our store. 

Sale Off
Voltaat Arduino Uno R3 (Voltaat Version)
45 QAR
Sale Off
Voltaat PIR Motion Sensor
17 QAR
Sale Off
Voltaat Jumper Wires - Male to Female (40 Pack)
10 QAR

Wiring it up


To set up the PIR motion sensor in the correct way, follow the instructions below. The image demonstrates how to connect the wires between the PIR motion sensor and the Arduino. Once the PIR motion sensor and the Arduino are connected to each other, connect the Arduino to your computer using the USB cable.



Connections from the Arduino to the PIR sensor:
• Arduino GND pin → PIR sensor GND pin (- pin)
• Arduino pin 2 → PIR sensor out pin
• Arduino VCC pin → PIR sensor VCC pin (+ pin)

The sensor has two potentiometers, one to control the sensitivity and the other to control the timing of the sensor.

Coding


The function of this code is to obtain the reading from the PIR sensor via the Arduino input pin. If it is high, it means there is someone in your house. In this case, the Arduino will send a message to the computer warning that someone has entered your home.

 

This code is straightforward and does not require any libraries.

 

You can use it directly or change it to suit as many applications as you want.


/*
Voltaat learn (https://www.voltaat.com/pages/voltaat-learn)
Link for full tutorial: https://bit.ly/3D3uxqn

Tutorial:PIR sensor alerts you when someone enters your house

The function of this code is to obtain the reading from the PIR sensor via the Arduino input pin.
If it is high, this means there is someone in your house.
In this case the Arduino will send a message to the computer warning that someone has entered your home.

Connections from the Arduino to the PIR sensor:
• Arduino GND pin → PIR sensor GND pin (- pin)
• Arduino pin 2 → PIR sensor out pin
• Arduino VCC pin → PIR sensor VCC pin (+ pin)

*/


//Define the variable pirSensor to digital Pin 2
int pirSensor =2;

//Define the variable pirSensorRead and initialize its value to 0
int pirSensorRead =0;

//Commands inside void setup run once
void setup(){

//Start the serial monitor at 9600 baud rate (9600 bits per second)
Serial.begin(9600);

//pirSensor is defined as an input
pinMode(pirSensor,INPUT);

}

// Commands inside void loop run forever
void loop()
{
// Read the value of digital output from rainSensor and assign it in the variable pirSensorRead
pirSensorRead =digitalRead(pirSensor);


if(pirSensorRead ==HIGH)
{
//Print to serial monitor
Serial.println("Security Alarm, There is someone in your house!");
//Delay for 5 seconds
delay(5000);
}

}

Testing it out




Now you must have correctly wired the PIR motion sensor to the Arduino as we explained in the wiring section, as well as uploaded the code to your Arduino board.

Then, install the sensor in a suitable place at the entrance of your house. You can test it by passing in front of it or moving your hand near it.

You may now access the serial monitor on your Arduino IDE by clicking on the magnifying glass icon at the top right corner.



The serial monitor is a great way that enables you to establish communication between your computer and the Arduino by sending and receiving different commands and information using the USB cable.


Now, as we see in the following image, the serial monitor displays the security alert because the sensor was triggered, which means there is someone at your house!


You should also make sure you have chosen the right baud rate (9600) as specified in the code.


Resources 


Arduino Code

Fritzing Wiring file

Related Tutorials


A servo motor is just a simple DC motor with certain modifications. When you look at it, you'll notice that it has several gears and an electronic circuit.


The ultrasonic sensor is a device that can measure distance by sending out sound waves and calculating how long it takes for them to bounce back.


In this tutorial, we will use the PIR sensor to send a message to your computer when someone enters your house.