Arduino Security System W/ PIR & Distance Sensor

by DP - 987636 in Circuits > Arduino

3134 Views, 3 Favorites, 0 Comments

Arduino Security System W/ PIR & Distance Sensor

IMG_20220120_104111.jpg
unnamed.jpg
Copy of Final Project - Comp Eng Sem 1 - DP 987636.png

Hi, my name is Dhyeya and in this Instructable I will be presenting and showing a step-by-step process of my project, the Arduino Security System. The project I built is an attempt to make more accurate and faultless security systems.

Description:

The following project is aimed towards making better security systems. The project will be using Distance or Ultrasonic Sensor and PIR Sensor to sense any sort of suspicious movements to turn on alarms in forms of LEDs and Buzzer and etc, depending on the user. By incorporating distance/ultrasonic sensors and infrared sensors, we can ensure that no type of movement can go undetected and simply if one sensor doesn’t detect it, the other will.

Purpose:

In today’s world, security systems have a slight tendency to make mistakes. We know that these mistakes can result in the risk of theft and burglary, which means that the user would have to pay dearly. With this project, I aim to make more accurate and alert security systems.

Note: This project was built in real life with real-life Arduino components and was also made in TinkerCAD.

Components Required

Copy of FINAL - Final Project - Comp Eng Sem 1 - DP 987636.png

To build this project, you will be needing the following components:

Note: Arduino jumper wires of all colours; mainly red, black, yellow and green; will be required and the user is expected to have them.

Wiring and Schematic (Part 1)

IMG_20220121_085240.jpg
IMG_20220120_104111.jpg
IMG_20220121_084827.jpg

As you can see, the wiring for this project is not difficult to perform and understand. The sensors are located on one, single side of the breadboard while the output components, along with the button are located on the other side.

The input components, the sensors and the button, have connections to the ground and power rail as one can see them, and a connection to the Arduino Pins. The PIR sensor did require a 3-pin female to male jumper wire which runs underneath the breadboard so the circuit it convinient to look at.

The output connections, on the other hand, only require connection to the ground rail and the Arduino Pins. They are connected to resistors of appropriate measures to control them.

However, these images provided do not give a proper schematic view which is more advanced, easier to follow and more neat. The next part will provide a schematic view.

Wiring and Schematic (Part 2)

final schematic image.png

Here is a schematic view to simplify and facilitate communication by visualizing the relationships between system objects and making them more obvious.

They provide diagrams in more a standardized and clear way.

I will now provide the wiring for each component, starting with the Sensors.

Distance Sensor:

  • VCC and GND pins are to be connected to the Power and Ground rails, respectively.
  • The echoPin is INPUT so it should be connected to Arduino Pin 4 via a yellow Jumper Wire
  • The trigPin is OUTPUT so it should be connected to Arduino Pin 2 via a green Jumper Wire

PIR Sensor:

  • A 3-pin female-to-male Jumper Wire will be required for this.
  • Since the 3-pin are in a colour order that can't be changed, we have to connected the VCC via the yellow wire. and the GND via the black wire to the Power and Ground rail, respectively.
  • The Signal or the INPUT will be connected to Arduino Pin 7 via the red wire.

How It Functions

in action.png

Since it is a security system, it is made to detect motion is two ways, using ultrasonic distance sensor and PIR sensor.

Ultrasonic Distance Sensor:

  • Detects any object in front of it and calculates its distance in inches. 15 inches has been set as a safe distance. Anything beyond it won't trigger any alarms and a Green LED will stay on.
  • Green LED will turn off if any oject or person is within 10 to 15 inches.
  • Any closer, below 10 inches, the Red LED will turn on alerting the user inside.
  • Between 5 to 10 inches, the Red LED will stay on and the user inside can check the distance on a serial monitor.
  • Below 5 inches, the Piezo Alarm will go off, alerting the user to take precautions.

PIR Sensor:

  • Detect basically any movement in its vicinity
  • Sensitivity can be controlled
  • Will set on a Piezo buzzer and a Red LED
  • The user can check the status on the serial moniter

Uses: The sensors can be connected on one's door or doorknob at nightimes.

Reset Button:

  • Smaller purpose
  • Can be used to reset the sensor values back to low after a situation is brought back to normal by the user.

OUTPUTs:

  • The LEDs have been coded to turn off or on depending on any movement or oject detected by the sensors.
  • Blinking Green and Red LEDs mean that both sensors have detect movement
  • The Piezo Buzzer has been coded to emit sounds depending the PIR sensor. If the Ultrasonic sensor detects movement within 5 inches, the Piezo buzzer will play a specific tune.

Note: The image shows the prototype in TinkerCAD functioning normally. The Piezo and the Red LED can be seen turned on, indicating some movement was detected in the simulation.

Code (Part 1)

The code for this project has been attached as a txt file. However, I will explain the code, in brief.

Variables

  • Components connected and their pins were declared; EX - 'const int trigPin = 2'
  • Values were declared; EX - 'int pirState = LOW'
  • Were all declared in an organized manner, first INPUTS and then OUTPUTS.

void setup()

  • Declared all the pins and components connected as either 'OUTPUT' or 'INPUT'
  • Made sure to add 'Serial.begin(9600)' This was to initialize serial communication at 9600 bits per second

void distance()

  • A function critical to the functioning of the project itself.
  • Added commands to determine how the distance would be measured, delays between signals, converting the received signals to inches and etc.
  • Added 'Serial.println(distanceInInches)' to print the distance in inches on the serial moniter.
  • Added commands on what OUTPUT components to activate upon detecting an object within a certain distance.

void reset()

  • Added command to reset all pins, sensors and sensor values to LOW upon execution
  • Would work when the button is pressed

void loop()

  • Added void distance() in it to make it one of the main parts of void loop()
  • Added commands to activate or de-activate OUTPUT components when the PIR Sensor detects movement
  • Declared 'pirState = digitalRead(pirPin)'
  • Delay set to (500), results would appear on the serial monitor every 500 milliseconds or 0.5 seconds

Code (Part 2a) - Variables

code_variables.png

This part of the code declares which components are connected to which Arduino UNO R3 pins. This part is critical as without it, the prototype wouldn't function at all.

One can also add values here which can help sensors start as LOW.

Ex - I added "int pirState = LOW"

This basically means that the PIR sensor starts as low and detects no motion. As time goes on, whenever it will detect any motion, it will transition to HIGH.

Code (Part 2b) - Setup

code_setup.png

The "void setup()" function is similar to setting up the variables but still different. This function is used to declared which pins will be OUTPUT or INPUT.

In our case, the prototype has 3 INPUT Pins (echoPin, pirPin, button) and 4 OUTPUT Pins (trigPin, redLED, greenLED, buzzerPin).

Depending on their (pins') status (whether they are OUTPUT or INPUT), the Arduino will either sends signal and allow current to pass through OR receive signal and current which can be used to turn on OUTPUT components.

Example:

  • INPUT: As the PIR Sensor detects movement and pirState is now HIGH, it will turn pirPIN HIGH as well. This is will send a signal or current to the Arduino which can detect it.
  • OUTPUT: As the Arduino receives a signal or current, it will sends a current or signal to an OUTPUT source, like the Red LED. This is all done according to the code.

Code (Part 2c) - Distance

code_distance2.png
code_distance1.png

This part of the code will allow:

  • The sensor to sends signals or waves
  • Detect them and convert them to Inches
  • Print it in the Serial Monitor
  • OUTPUT components to act according to the code

TrigPin:

  • Will start as LOW; Wait for 2 milliseconds and sends a signal as it turns HIGH
  • Will wait 10 milliseconds and go back to LOW

EchoPin:

  • Will detect these signals which are bounced back from the object nearby.
  • Will convert the time taken for the signals to return into inches using "distanceInInches = durationInDigit/74/2"

Serial Monitor:

  • Will print the distance in inches on the serial monitor

Commands:

  • In accordance to the distanceInInches, this part of the code will help the OUTPUT components function
  • Ex - Turning the Piezo Buzzer as HIGH if the distance is less than 5 inches.

Code (Part 2d) - Reset Button

reset code.png

The void reset() function is more of a convenience feature in this prototype.

Explanation:

Let's say there wasn't some suspicious movement near the user's property and the user was aware of this, the prototype will still detect this movement and set off the alarms. This can cause a major inconvenience but using this function the user can forcefully turn off the sensor for the desired time.

The "delay (20000);" part is customizable and the desired time limit can be set. In this case the time limit is 20 seconds; meaning the sensor will stay shut for 20 seconds.

The Green LED will also turn on, indicating the reset() function was performed.

Code (Part 2e) - Loop

new loop.png

Void Loop will perform the "distance()" function [See step 'Code (Part 2c) for more]

The '"reset()" function will also be performed [See step 'Code (Part 2d) for more]

The pirState = digitalRead(pirPin) will help the Arduino read pirState by reading pirPin as HIGH (1) or LOW(0).

PIR sensor:

  • Most of the PIR Sensor code is located in this part of the code
  • The first part will turn on the OUTPUT components (Red LED and Piezo Buzzer) if "pirState==1"
  • "Motion detected" will also be printed on the Serial Monitor"
  • The second part will keep the OUTPUT components; Red LED and Piezo Buzzer; off and keep the Green LED on
  • "Detecting..." will be printed on the Serial Monitor as the PIR Sensor is still attempting to detect motion

Common Mistakes

A Final Image of My Project

FINAL - Final Project - Comp Eng Sem 1 - DP 987636.png

This image is from TinkerCAD as it is more clear to look at.

Modifications

first wiring image.jpg

The project can be modified in various ways to fit the user's needs and wants. These modifications can be minor, like extending wires or using stronger sensors, or they could be larger and more complex modifications like:

  • The OUTPUT components can be modified or even replaced to build even more advanced alarm systems.
  • The system can be coded to function only during a given time limit, like at night time.

The user would also requires resistors, wires and breadboards or different sizes and strengths if they choose to modify or replace their OUTPUT or INPUT components.