Armable Breach Detection System

by 683944 in Circuits > Arduino

154 Views, 1 Favorites, 0 Comments

Armable Breach Detection System

IMG-2371.jpg

Hello! My name is Rafay Rabbani and in this Instructable, I will be showcasing my final engineering project. For this project, I have created a threat detection system. In this threat detection system, various LEDs and a sound buzzer will turn on depending on the distance of an object from an ultrasonic sensor. It also has a slide switch, which depending on its position will either arm or unarm the system. When armed the distance indicating LEDs and piezo will be active, when unarmed, the threat detection system will be inactive.

Supplies

To create this project, the materials you will need are…

Arduino Uno

1 x Breadboard

Assorted Wires

4 x 330Ω Resistor

1 x NPN Transistor

1 x Ultrasonic Distance Sensor

1 x Slide Switch

1 x Red LED

1 x Yellow LED

1 x Green LED

1 x Piezo Buzzer

Wiring

Engineering FInal Project.png
IMG-2372.jpg
Schematics.PNG
IMG-2373.jpg

To aid in wiring the components together I have attached the TInkercad diagram, schematic and breadboard model above.


1. Connecting the Power Source

Numerous components will require both power and ground so start by connecting the Arduino’s 5v VCC and GND to the breadboard. You will also need to bridge the power and ground connections to the other side of the board so that the current can flow through.


2. Wiring Up the LEDs

Position the 3 LEDs on the breadboard in order going from left to right red, yellow, and green. Attach a 330Ω resistor to the cathode side of the LED and wire it to ground, repeat for all 3 LEDs. Next, wire the anode side of the LED to the Arduino pinouts, depending on the colour of the LED the pinout will be different. Red is 11, yellow is 10 and green is 9, ensure you wire them up correctly otherwise, you won’t achieve the desired result.


3. Affixing the Piezo

Wiring up the Piezo will be a little more difficult than the LEDs, but don’t worry I’ll walk you through it. Start by positioning the sound buzzer on the breadboard and wiring it to power (should be labelled with a +). Then attach an NPN transistor so that the emitter side is connected to the buzzer’s ground. Next wire the collector side of the transistor with ground and the base with pinout 4 on the Arduino board.


4. Connecting the Ultrasonic Distance Sensor

Place the Ultrasonic Distance Sensor on the edge of the breadboard so that it is facing out. Wire up the pin labelled VCC with the breadboards VCC and the pin labelled GND with ground. A tip to keep the wiring clean is to bring the VCC and ground from the opposite side of the board and bridge it to their respective pins (look at the attached images to see what I mean). Next wire the pin labelled Echo to pinout 5 and Trig to pinout 6 on the Arduino board. You’re Ultrasonic Distance Sensor should now be fully wired up.


5. Securing the Slide Switch

Start by positioning the slide switch in a place where it can be easily accessible. Next wire up the leftmost pin to VCC and the rightmost pin to ground using a 330Ω resistor, Finally wire up the center “Common” pin with pinout 2 on the Arduino board.  


Coding: Part 1 (Variables)

Arduino Variables.PNG
Void Measue().PNG

Prior to coding you first need to initialize all of your variables. By defining the variables, you will be able to control the pinouts on the Arduino board.  All of the variables for the LEDs and Ultrasonic Distance Sensor are PWM pins so we can control their output digitally. Whereas, the switch pin and Piezo are connected to regular Arduino pins.

Before we move on to the next step don’t forget to define the variable for both the duration of the ping and distance ion centimetres from the Ultrasonic Distance Sensor. Then import the function measure_distance (this will make more sense later).

Coding: Part 2 (Setup Function)

VOid SETOPIO.PNG

Before setting your variables initialize the serial monitor so that you can print stuff to it

Now that all of your variables have been defined next, you need to set them as either input or output. In order to do that you need to type the following:

pinMode((Variable name),(Input or Output)) 


Replace variable name with whatever variable you are doing then assign it as either an input or output. For example, the Piezo or buzzer is set as an output because it is receiving a pulse from the Arduino board. All 3 of the LED variables and the trigger pin for the Ultrasonic Distance Sensor need to be set as output as they are also gonna receive a pulse from the Arduino board. 


The echo pin for the Ultrasonic Distance Sensor will be set as input because it will send a signal to the Arduino board rather than receiving one. Then add a 100ms delay in order to accurately configure the i/o pins.

Now that all the variables have been assigned and the setup function is complete we can now move on to the loop function.


Coding: Part 3 (Loop Function)

Void Loop().PNG

The loop function is where the majority of the code will be and can get confusing sometimes so just stay with me. 

I first read the slide switches' current state using the command switchState = digitalRead(switchPin). Then I again imported the measure_distance function which I will explain later. 


Now we get to our first if statement, if there is an object within 20cm of the ultrasonic sensor and the switch’s state is high, meaning that the system is armed the red LED will light up, the Piezo will begin producing sound and “BREACH DETECTED” will be printed to the serial monitor. 

if((distance<20) && (switchState == HIGH)){

 digitalWrite(RED,HIGH);

 digitalWrite(YELLOW,LOW);

 digitalWrite(GREEN,LOW);

 tone(buzzer, sound);

 Serial.println("BREACH DETECTED");


Next is the else if statement, if there is an object more than 20 cm from the ultrasonic sensor but still within 50cm and the switch’s state is high the yellow LED will light up and “DANGER” will be printed to the serial monitor. 

else if((distance<=50)&&(distance>20) && (switchState == HIGH)){

 digitalWrite(RED,LOW);

 digitalWrite(YELLOW,HIGH);

 digitalWrite(GREEN,LOW);

 noTone(buzzer);

 Serial.println("DANGER");

 }


And now the last else if statement, if there is an object more than 50 cm from the ultrasonic sensor and the switch’s state is still high the green LED will light up and “SAFE” will be printed to the serial monitor.

else if((distance>50)&& (switchState == HIGH)){

 digitalWrite(RED,LOW);

 digitalWrite(YELLOW,LOW);

 digitalWrite(GREEN,HIGH);

 noTone(buzzer);

 Serial.println("SAFE");


To end off the lf statement and thereby the loop function I added an else statement which will print “SYSTEM UNARMED” to the serial monitor as it will only be printed if the slide switch’s state is low therefore unarming the system.


Coding: Part 4 (Measure Distance Function)

measure.PNG

Now that the loop function has been complete we have finally reached the long-anticipated measure_distance function.


Before we begin we have to type void measure_distance() to let the system know we are creating a function.

void measure_distance()


The ultrasonic distance sensor is triggered by a high pulse of 10 microseconds, in order to ensure a clean high pulse we first have to give a short low pulse and end off with another short low pulse.  

digitalWrite(trigger,LOW);

delayMicroseconds(2);

digitalWrite(trigger,HIGH);

delayMicroseconds(10);

digitalWrite(trigger,LOW);

delayMicroseconds(2);


Now we have to read the signal from the ultrasonic sensor. Duration is the time (in microseconds) from the sending of the ping to the receiving of its echo off an object. 

duration=pulseIn(echo,HIGH);


This next part can get a bit confusing so just bear with me. The speed of sound is 340 metres per second or 29 microseconds per centimetre, so to get the length in cm we divide the duration by 29. Since the ping has to travel both back and forth, to find the distance of the object (in cm) we have to talk half of the distance travelled.

distance=duration/29/2;


With the completion of the measure_distance function, the code is finally complete and can now be put into use. I know it got confusing at times, but I hope you were all able to follow along.


Demonstration

Congratulations on completing the project! Below I have attached a video on how the full circuit should function enjoy!