Personal Security System Using Arduino

45K25755

Intro: Personal Security System Using Arduino

Here is just a simple guide on how to create your own mini "security system" using an Arduino. This is just a fun project, please don't actually rely on this device to keep your house secure! This design uses an Arduino, a HC-SRO4 Ultrasonic Sensor, a Buzzer, and some LED's. Ultimately from this tutorial, I hope you learn how to use a buzzer and LED's to display how far away an object is from the ultrasonic sensor.

STEP 1: Assemble Materials

Materials required are:

(1x) Arduino Uno

(1x) Breadboard

(1x) HC-SRO4 Ultrasonic Sensor

(1x) Buzzer

(1x) Green LED

(1x) Yellow LED

(1x) Red LED

(4x) 220 ohm Resistors

(10x) Jumper wires

STEP 2: Setup

The photo above shows the setup of the project.

The circuit should be setup as follows:

Connect a red wire from the 5V pin on the Arduino to the positive channel of the breadboard.

Connect a black wire from the GND pin on the arduino to the negative channel of the breadboard

Buzzer = pin 3

On Ultrasonic Sensor:

Echo = pin 6

Trig = pin 7

LEDs:

RedLED = pin 9

YellowLED = pin 10

GreenLED = pin 11

The green wires connected to the LEDs should be connected in line to the positive side of the LED, while the negative side of the LED should be connected to the negative channel of the breadboard using a 220 ohm resistor.

STEP 3: Assembly - Breadboard

Firstly, let's connect the 5V and GND pin on the Arduino to the breadboard. As I mentioned before, be sure that the wire attached to the 5V pin is connected to the positive channel of the breadboard, and the wire attached to the GND pin is connected to the negative channel of the breadboard.

STEP 4: Assembly - Ultrasonic Sensor

Time to connect the HC-SRO4 ultrasonic sensor! A great tip is to place the ultrasonic sensor as far right to the breadboard as possible and make sure that it is facing out. Referring back to the setup picture, you should connect the GND pin on the ultrasonic sensor to the negative channel on the breadboard. Next connect the Trig pin on the sensor to pin 6 on the Arduino and connect the Echo pin on the sensor to pin 7 on the Arduino. Lastly, connect the VCC pin on the ultrasonic sensor to the positive channel on the breadboard. Refer to the picture above if anything gets confusing.

STEP 5: Assembly - LEDs

The next step is to connect the LED's to the breadboard and Arduino. If you need to, I highly recommend that you refer back to the setup picture (Step 2), attaching the LEDs is pretty easy, there's a lot of repetition. Let's first attach the Green LED. So the way to do this, is to connect the anode (the longer leg) to pin 11 on the Arduino with a green wire, and to connect the cathode (the shorter leg) to the negative channel on the breadboard, using a 220 ohm resistor. Then repeat that step for the Yellow and then the Red LED, make sure to connect the anode (the longer leg) of the yellow LED to pin 10 on the Arduino and then connect the anode of the red LED to pin 9. Once you have done that, your setup should look similar to the picture above.

*NOTE*

Resistors are not absolutely necessary, however they are highly recommended to be used.

STEP 6: Assembly - Buzzer

The last part of the setup for this, is connecting the buzzer to the breadboard and the Arduino. This is one of the easiest parts of the whole setup. All that is required to do is to connect the longer leg of the buzzer to pin 3 of the Arduino using a green wire and then connect the shorter leg of the buzzer to the negative channel of the breadboard using a 220 ohm resistor.

*NOTE*

It is HIGHLY recommended to use a resistor in connecting the shorter leg of the buzzer to the negative channel of the breadboard. This greatly reduces the volume of the buzzer and prevent it from dying to quickly.

STEP 7: Code

Now that you have finished the setup, its time to code the Arduino. All you have to do, is to open the Arduino program on your computer and then copy and paste the code from below. Feel free to change the distances at which the ultrasonic sensor detects an object from and the volume of the buzzer!

#define trigPin 6<br>#define echoPin 7
#define GreenLED 11
#define YellowLED 10
#define RedLED 9
#define buzzer 3
int sound = 500;
void setup() {
  Serial.begin (9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(GreenLED, OUTPUT);
  pinMode(YellowLED, OUTPUT);
  pinMode(RedLED, OUTPUT);
  pinMode(buzzer, OUTPUT);
 
}
void loop() {
  long duration, distance;
  digitalWrite(trigPin, LOW); 
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration/5) / 29.1;
 
  if (distance < 50) {
      digitalWrite(GreenLED, HIGH);
}
  else {
      digitalWrite(GreenLED, LOW);
  }
  
  if (distance < 20) {
    digitalWrite(YellowLED, HIGH);
}
  else {
    digitalWrite(YellowLED,LOW);
  }
  if (distance < 5) {
    digitalWrite(RedLED, HIGH);
    sound = 1000;
}
  else {
    digitalWrite(RedLED,LOW);
  }
 
  if (distance > 5 || distance <= 0){
    Serial.println("Out of range");
    noTone(buzzer);
  }
  else {
    Serial.print(distance);
    Serial.println(" cm");
    tone(buzzer, sound);
   
  }
  delay(300);
}

Once you've done that, and you've plugged in your Arduino to your computer, run the code and you're finished. If you've followed all the directions properly, the closer your hand or any object gets to the HC-SRO4 ultrasonic sensor, the LEDs should progressively light up until and you're so close that the buzzer will go off.

STEP 8: The Working Arduino!

This is a really fun project, I had a lot of fun creating it. I just want to give a shoutout to my Computer Science teacher Mr. Smith for being so very helpful and being amazing!

29 Comments

does the same concept work with the MEGA 2560??

Hi, i am currently building this. I plugged in the arduino and the 3 LED lights lit up but the ultrasonic sensor doesn't go off. First it said that there was a stray # so i deleted the <br> and pressed enter to move the second define down a line and it worked. This was last week. But today, It is not working.

there is a problem in the code the formula calculating the the distance is wrong i think

use this code i made

the connections made where same as shown

const int trigPin = 6;

const int echoPin = 5;

#define GreenLED 11

#define YellowLED 10

#define RedLED 9

#define buzzer 3

int sound = 500;

void setup()

{

// initialize serial communication:

Serial.begin(9600);

pinMode(GreenLED, OUTPUT);

pinMode(YellowLED, OUTPUT);

pinMode(RedLED, OUTPUT);

pinMode(buzzer, OUTPUT);

}

long distanceOverTime(long first,long second)

{

return ((first-second)/.1)*.0223693629;//taking cm/s to mph

}

long holder;//store the cm from last time through loop.

long temp;//used to store the speed value after changes

int counter;

void loop()

{

// establish variables for duration of the ping,

// and the distance result in inches and centimeters:

long duration, inches, cm;

int tens;

int ones;

long Speed;

// The sensor is triggered by a HIGH pulse of 10 or more microseconds.

// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:

pinMode(trigPin, OUTPUT);

digitalWrite(trigPin, LOW);

delayMicroseconds(2);

digitalWrite(trigPin, HIGH);

delayMicroseconds(10);

digitalWrite(trigPin, LOW);

// Read the signal from the sensor: a HIGH pulse whose

// duration is the time (in microseconds) from the sending

// of the ping to the reception of its echo off of an object.

pinMode(echoPin, INPUT);

duration = pulseIn(echoPin, HIGH);

// convert the time into a distance

inches = microsecondsToInches(duration);

cm = microsecondsToCentimeters(duration);

if (cm<= 50) {

digitalWrite(GreenLED, HIGH);

}

else {

digitalWrite(GreenLED, LOW);

}

if (cm<= 30) {

digitalWrite(YellowLED, HIGH);

}

else {

digitalWrite(YellowLED,LOW);

}

if (cm<= 15) {

digitalWrite(RedLED, HIGH);

sound = 1000;

tone(buzzer, sound);

}

else {

digitalWrite(RedLED,LOW);

noTone(buzzer);

}

/* if (cm< 5 || cm<= 0){

Serial.println("Out of range");

noTone(buzzer);

}

else {

Serial.print(cm);

Serial.println(" cm");

tone(buzzer, sound);

}*/

delay(300);

Serial.print(cm);

Serial.print(" cm ");

Serial.println();

delay(100);

}

long microsecondsToInches(long microseconds)

{

// According to Parallax's datasheet for the PING))), there are

// 73.746 microseconds per inch (i.e. sound travels at 1130 feet per

// second). This gives the distance travelled by the ping, outbound

// and return, so we divide by 2 to get the distance of the obstacle.

// See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf

return microseconds / 74 / 2;

}

long microsecondsToCentimeters(long microseconds)

{

// The speed of sound is 340 m/s or 29 microseconds per centimeter.

// The ping travels out and back, so to find the distance of the

// object we take half of the distance travelled.

return microseconds / 29 / 2;

}

Hello, I have a problem , when I used your code it never stopped buzzing.Do you have any idea why?. I really need yours or anybodies help.Thanks.

Hello, I followed the instructions for the wiring, but the buzzer will not turn off, and the LEDs aren't turning on, I used a different code however:

const int trigPin = 6;

const int echoPin = 5;

#define GreenLED 11

#define YellowLED 10

#define RedLED 9

#define buzzer 3

int sound = 10000;

void setup()

{

// initialize serial communication:

Serial.begin(9600);

pinMode(GreenLED, OUTPUT);

pinMode(YellowLED, OUTPUT);

pinMode(RedLED, OUTPUT);

pinMode(buzzer, OUTPUT);

}

long distanceOverTime(long first,long second)

{

return ((first-second)/.1)*.0223693629;//taking cm/s to mph

}

long holder;//store the cm from last time through loop.

long temp;//used to store the speed value after changes

int counter;

void loop()

{

// establish variables for duration of the ping,

// and the distance result in inches and centimeters:

long duration, inches, cm;

int tens;

int ones;

long Speed;

// The sensor is triggered by a HIGH pulse of 10 or more microseconds.

// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:

pinMode(trigPin, OUTPUT);

digitalWrite(trigPin, LOW);

delayMicroseconds(2);

digitalWrite(trigPin, HIGH);

delayMicroseconds(10);

digitalWrite(trigPin, LOW);

// Read the signal from the sensor: a HIGH pulse whose

// duration is the time (in microseconds) from the sending

// of the ping to the reception of its echo off of an object.

pinMode(echoPin, INPUT);

duration = pulseIn(echoPin, HIGH);

// convert the time into a distance

inches = microsecondsToInches(duration);

cm = microsecondsToCentimeters(duration);

if (cm<= 50) {

digitalWrite(GreenLED, HIGH);

}

else {

digitalWrite(GreenLED, LOW);

}

if (cm<= 30) {

digitalWrite(YellowLED, HIGH);

}

else {

digitalWrite(YellowLED,LOW);

}

if (cm<= 15) {

digitalWrite(RedLED, HIGH);

sound = 10000;

tone(buzzer, sound);

}

else {

digitalWrite(RedLED,LOW);

noTone(buzzer);

}

/* if (cm< 5 || cm<= 0){

Serial.println("Out of range");

noTone(buzzer);

}

else {

Serial.print(cm);

Serial.println(" cm");

tone(buzzer, sound);

}*/

delay(300);

Serial.print(cm);

Serial.print(" cm ");

Serial.println();

delay(100);

}

long microsecondsToInches(long microseconds)

{

// According to Parallax's datasheet for the PING))), there are

// 73.746 microseconds per inch (i.e. sound travels at 1130 feet per

// second). This gives the distance travelled by the ping, outbound

// and return, so we divide by 2 to get the distance of the obstacle.

// See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf

return microseconds / 74 / 2;

}

long microsecondsToCentimeters(long microseconds)

{

// The speed of sound is 340 m/s or 29 microseconds per centimeter.

// The ping travels out and back, so to find the distance of the

// object we take half of the distance travelled.

return microseconds / 29 / 2;

}

Does it should be connected to the computer all the time

nope, it can be powered with a 5 or 9 volt adapter with a pin that is compatible with the second port available on your arduino. however, the program should be already uploaded from your pc :)

Hi,

This project didn't work when I used the program you kindly wrote, I had to change a few things in it (which was very difficult for a fresh beginner like me).

Evantually it worked fine

Thank you for a very nice project

Pleasee send the code to
phinahasphilip2000@gmail.com
Plssss it is our first mission
Hope u will send plss,,,,

sorry ,

I didn't see your reply,

I'll send it in the next days since I'm on vacation right now

Hi! I like the project very much and I want to make it. But, I have an ultrasonic sensor of three pins instead of four. I need help to solve the problem. Could you help me?

hi nice project for beginners.

I wonder why my buzzer didn't ring

The coding above u wrote has a mistake in line 1

So the original was

#define trigPin 6<br>#define echoPin 7

The needed code is

#define trigPin 6#define echoPin 7

(just remove the <br>)

and make sure ur COM is 3 or 5 according to your number.

To change it watch this short vid below

https://www.youtube.com/watch?v=hou4okcCX7E

The given code wasn't working for me, so I edited it. Here is what I came up with:

//This sketch tells you if you get too close using a HC-SR04 sonar,
//red, yellow, and green LEDs and a piezo buzzer.

#include <NewPing.h> //include HC-SR04 sensor library

#define TRIGGER_PIN 6 //set up trigger pin on HC-SR04
#define ECHO_PIN 7 //set up echo pin on HC-SR04
#define MAX_DISTANCE 200 //set up max distance on HC-SR04
#define GreenLED 11 //set up the green LED on pin 11
#define YellowLED 10 //set up the yellow LED on pin 10
#define RedLED 9 //set up the red LED on pin 9

NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // set up sonar

int buzzer = 3; //set up piezo buzzer

void setup() //Let's get started!
{
Serial.begin (9600); //Let the serial monitor know it will be getting info
pinMode(GreenLED, OUTPUT); //Let ardino know that LED will be outputting
pinMode(YellowLED, OUTPUT); //Let ardino know that LED will be outputting
pinMode(RedLED, OUTPUT); //Let ardino know that LED will be outputting
pinMode(buzzer, OUTPUT); //Let ardino know that buzzer will be outputting
}

void loop() //Now for the fun stuff!
{
long distance=sonar.ping_cm(); //set up variable "distance" with sonar info

if (distance > 50) //If the distance is more than 50 cm
{
digitalWrite(GreenLED, HIGH); //turn on the green light
Serial.print(distance); //write the distance to the monitor
Serial.println(); //blank line for readability
digitalWrite(RedLED,LOW); // make sure the red LED is off
digitalWrite(YellowLED,LOW); // make sure the yellow LED is off
analogWrite(buzzer, LOW); // make sure the buzzer is off
}

else if (distance > 20 && distance < 50) //If the distance is more than 20 cm
{ // and less than 50 cm
digitalWrite(YellowLED, HIGH); //turn on the yellow light
Serial.print(distance); //write the distance to the monitor
Serial.println(); //blank line for readability
digitalWrite(GreenLED, LOW); // make sure the green LED is off
digitalWrite(RedLED,LOW);// make sure the red LED is off
analogWrite(buzzer, LOW);// make sure the buzzer is off
}

else if (distance > 5 && distance < 20) //If the distance is more than 5 cm
{ // and less than 20 cm
digitalWrite(RedLED, HIGH); //turn on the red light
Serial.print(distance); //write the distance to the monitor
Serial.println(); //blank line for readability
digitalWrite(GreenLED, LOW); // make sure the green LED is off
digitalWrite(YellowLED,LOW); // make sure the yellow LED is off
analogWrite(buzzer, HIGH); // turn on the buzzer
}

delay(300); //wait 300ms between readings so it doesn't get crazy
}

Im just starting to experiment with Arduino and your program writeup is so helpful! I love that you explained what each line did, It makes it so much easier to understand that the Arduino is doing! Do you have any tutorial or other program examples? I would love to learn from them!

I haven't made any tutorials. I just go through the code line by line, yes it takes time but I end up totally understanding the code afterwards-give it a shot

More Comments