Components needed

  • Ultrasonic Sensor
  • Piezo(buzzer)
  • An LED
  • A resistor
  • Jumper cables
  • Breadboard
  • Arduino Board
  • A computer with the Arduino IDE installed

Completed Circuit

The image above is the completed circuit for the security system. We will build a simple security system using an ultrasonic sensor where if something is stolen, the piezo (buzzer) will buzz , and a red light will turn on.

The image above is an HC-SR04 ultrasonic distance sensor. It has four pins. The left most pin called Vcc, is for power, and the right most pin called Gnd is for the ground. The middle two pins are called trig (trigger) an echo. The HC-SR04 sensor works best between 2cm – 400 cm (1″ – 13ft) within a 30 degree cone, and is accurate to the nearest 0.3cm.

How an ultrasonic sensor works?

As you can see there are two round objects on the sensor. One is the transmitter and the other is the receiver. The transmitter (trigger pin) sends a high frequency sound signal. When that sound signal finds an object on its way, it will be reflected back, and will be received by the receiver (echo pin) on the sensor.

Using the time taken between the transmission and the reception of the high frequency sound signal, we can calculate the distance from the sensor to the object using the formula distance=speed multiplied by time. The sensor gives us the time taken for the high frequency sounds to travel and the speed of high frequency sounds in the air is already known.

We will not calculate the distance using the formula above manually. Instead we will use a library to do all that for us. There are many libraries available to use HC-SR04 ultrasonic sensor. We will use the library below in our program.

We have connected a buzzer in our circuit to make a noise when a distance to the object is more than 5 centimeter. Look at my previous blog on how to use a buzzer.

The Program

#include <HCSR04.h>

//Define pins ultrasonic(trig,echo)
UltraSonicDistanceSensor distanceSensor(A0, A1);

double distance;
int buzzerPin= 5;

void setup() {
  pinMode(buzzerPin, OUTPUT);

void loop()
  distance = distanceSensor.measureDistanceCm();

  if ( distance > 5 || distance== -1) {
    digitalWrite(8, HIGH);
    tone(buzzerPin, HIGH);
  } else{
    digitalWrite( 8, LOW);
  Serial.print("Object found at: ");
  //every 1sec.

The program is very simple. Using the library we have included, we are measuring the distance of the object in front of the ultrasonic sensor. If the distance is less than 5 cm, we consider the object is moved or “stolen” in our security system and, the buzzer and the LED light will go off. See video below for the demo.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s