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


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.


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.


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); 
  digitalWrite(trigPin, HIGH);
  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 {
  if (distance < 5) {
    digitalWrite(RedLED, HIGH);
    sound = 1000;
  else {
  if (distance > 5 || distance <= 0){
    Serial.println("Out of range");
  else {
    Serial.println(" cm");
    tone(buzzer, sound);

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!

<p>hi,</p><p>This is the problem occured when i upload to the uno</p>
<p>Hi,</p><p>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).</p><p>Evantually it worked fine</p><p>Thank you for a very nice project</p>
Pleasee send the code to <br>phinahasphilip2000@gmail.com<br>Plssss it is our first mission<br>Hope u will send plss,,,,
<p>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?</p>
<p>sorry ,</p><p>I didn't see your reply,</p><p>I'll send it in the next days since I'm on vacation right now</p>
<p>Hi,</p><p>I'm gonna try this feature in the next few days, thank you for that.</p><p>Where can I find the software to creat such a schemes like you did?</p><p>Where</p>
<p>thank uu</p>
<p>I was think if some one could help me with the code if i want it to work exactly opposite to this. For eg. the buzzer doesnot make sound if any object is near but buzz is it moves away. </p>
<p>is this code working properly!!!!!</p><p>do we need to install library for ultrsonic sensor??</p>
<p>there is a problem in the code the formula calculating the the distance is wrong i think <br><br>use this code i made </p><p>the connections made where same as shown</p><p>const int trigPin = 6;</p><p>const int echoPin = 5;</p><p>#define GreenLED 11</p><p>#define YellowLED 10</p><p>#define RedLED 9</p><p>#define buzzer 3</p><p>int sound = 500;</p><p>void setup()</p><p>{</p><p> // initialize serial communication:</p><p> Serial.begin(9600);</p><p> pinMode(GreenLED, OUTPUT);</p><p> pinMode(YellowLED, OUTPUT);</p><p> pinMode(RedLED, OUTPUT);</p><p> pinMode(buzzer, OUTPUT);</p><p>}</p><p>long distanceOverTime(long first,long second)</p><p>{</p><p> return ((first-second)/.1)*.0223693629;//taking cm/s to mph</p><p>}</p><p>long holder;//store the cm from last time through loop.</p><p>long temp;//used to store the speed value after changes</p><p>int counter;</p><p>void loop()</p><p>{</p><p> // establish variables for duration of the ping, </p><p> // and the distance result in inches and centimeters:</p><p> long duration, inches, cm;</p><p> int tens;</p><p> int ones;</p><p> long Speed;</p><p> // The sensor is triggered by a HIGH pulse of 10 or more microseconds.</p><p> // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:</p><p> pinMode(trigPin, OUTPUT);</p><p> digitalWrite(trigPin, LOW);</p><p> delayMicroseconds(2);</p><p> digitalWrite(trigPin, HIGH);</p><p> delayMicroseconds(10);</p><p> digitalWrite(trigPin, LOW);</p><p> // Read the signal from the sensor: a HIGH pulse whose</p><p> // duration is the time (in microseconds) from the sending</p><p> // of the ping to the reception of its echo off of an object.</p><p> pinMode(echoPin, INPUT);</p><p> duration = pulseIn(echoPin, HIGH);</p><p> // convert the time into a distance</p><p> inches = microsecondsToInches(duration);</p><p> cm = microsecondsToCentimeters(duration);</p><p> if (cm&lt;= 50) {</p><p> digitalWrite(GreenLED, HIGH);</p><p>}</p><p> else {</p><p> digitalWrite(GreenLED, LOW);</p><p> }</p><p> if (cm&lt;= 30) {</p><p> digitalWrite(YellowLED, HIGH);</p><p>}</p><p> else {</p><p> digitalWrite(YellowLED,LOW);</p><p> }</p><p> if (cm&lt;= 15) {</p><p> digitalWrite(RedLED, HIGH);</p><p> sound = 1000;</p><p> tone(buzzer, sound);</p><p>}</p><p> else {</p><p> digitalWrite(RedLED,LOW);</p><p> noTone(buzzer);</p><p> }</p><p> /* if (cm&lt; 5 || cm&lt;= 0){</p><p> Serial.println(&quot;Out of range&quot;);</p><p> noTone(buzzer);</p><p> }</p><p> else {</p><p> Serial.print(cm);</p><p> Serial.println(&quot; cm&quot;);</p><p> tone(buzzer, sound);</p><p>}*/</p><p> delay(300);</p><p> Serial.print(cm);</p><p> Serial.print(&quot; cm &quot;);</p><p> Serial.println();</p><p> delay(100);</p><p>}</p><p>long microsecondsToInches(long microseconds)</p><p>{</p><p> // According to Parallax's datasheet for the PING))), there are</p><p> // 73.746 microseconds per inch (i.e. sound travels at 1130 feet per</p><p> // second). This gives the distance travelled by the ping, outbound</p><p> // and return, so we divide by 2 to get the distance of the obstacle.</p><p> // See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf</p><p> return microseconds / 74 / 2;</p><p>}</p><p>long microsecondsToCentimeters(long microseconds)</p><p>{</p><p> // The speed of sound is 340 m/s or 29 microseconds per centimeter.</p><p> // The ping travels out and back, so to find the distance of the</p><p> // object we take half of the distance travelled.</p><p> return microseconds / 29 / 2;</p><p>}</p>
<p>Sorry for the post below I figured out the problem. just had to delete &lt;br&gt; and hit return.</p><p>All good now and the ibl is really well done congrats!</p>
Very good
<p>Great arduino project.</p>
<p>I kike it</p>

About This Instructable




More by Arduino Project By Bess :Personal Security System using Arduino  
Add instructable to: