Intruder Sensor with Ultrasonic Sensor

 In this Arduino project, ultrasonic sensor HC-SR04 is used to detect intruders. Two LEDs and a servo motor is also connected to Arduino.  The two led give visual information about intruder and the servo motor control can be used as an actuator to control different appliance such as locking or unlocking doors or other things.

How the intruder detector works?

The ultrasonic sensor works by sending series of brust of ultrasonic sound pulses which is controlled by the trigger pin. When the pulses strikes an object, the sound wave are reflected and the ultrasonic sensor can detects and sends this information over the echo pin. Here we HC-SR04 ultrasonic sensor to detect the presence of intruder.

When the intruder is within the range of the sensor, Arduino is set up to turn on LED and to start servo motor.

Video demonstration

The following video demonstrates how the ultrasonic intruder detector works with Arduino Uno.

On breadboard

The wiring connection of the Arduino with HC-SR04, servo motor and the two led with the help of breadboard is shown below.

The connection is explained in the circuit schematic section.

Circuit Schematic

The following shows the electrical wiring diagram between HC-SR04 ultrasonic, Arduino, Servo Motor and the two LEDs.

 The Ultrasonic sensor HC-SR04 has four pins- trigger, echo, Vcc and ground. The trigger pin is used to send sound wave and the echo pin is used to get the received signal if there is any object within a defined range. Here the the trigger pin is connected to pin 11 and echo pin is connected to pin 12 of Ardino Uno. The ground and Vcc pins are connected to ground and the +5V supply pin of the Arduino.

The servo motor has three pins- signal, Vcc and ground. The signal pin of the servo motor is connected to pin 10 of the Arduino Uno.

Two leds yellow and red are used. The yellow(red) led is connected to the pin 8 and the green led is connected to pin 9 of Arduino via 220Ohm resistors.

Source Code

The following is the Arduino program for the intruder detection system.


#include <NewPing.h> // NewPing library:
#include <Servo.h> // Servo library

#define echoPin 12 // Echo pin connected to Arduino 12
#define trigPin 11 // Trig pin connected to Arduino 11
#define MAX_DISTANCE 400 // Maximum distance we want to ping for in centimeters

NewPing sonar(trigPin, echoPin, MAX_DISTANCE); // NewPing Library setting
const int greenLed = 9, yellowLed = 8; // Set green LED to pin 9, yellow to pin 8
const int servoPin = 10;
int pos = 10;
Servo myservo;

void setup() {
   Serial.begin (115200);
   pinMode(echoPin, INPUT);
   pinMode(trigPin, OUTPUT);
   pinMode(greenLed, OUTPUT);
   pinMode(yellowLed, OUTPUT);
   myservo.attach(servoPin); // Servo attached to pin 9

void loop() {
   int duration, distance, pos = 0, i;
   digitalWrite(trigPin, LOW);
   digitalWrite(trigPin, HIGH); // Trig pin sends a ping
   digitalWrite(trigPin, LOW);
   duration = pulseIn(echoPin, HIGH); // Echo receives the ping
   distance = (duration / 2) / 29.1;
   Serial.println(" cm");
   // If sensor detects object within 10 cm
   if (distance <= 10) {
      digitalWrite(greenLed, LOW); // Turn off green LED
      digitalWrite(yellowLed, HIGH); // Turn on yellow LED
      myservo.write(180); // Move servo arm 180 degrees
   // Otherwise
   else {
      digitalWrite(yellowLed, LOW); // Turn off yellow LED
      digitalWrite(greenLed, HIGH); // Turn on green LED

You should install the NewPing library from the Arduino IDE library manager. Or you can download the library and put in the same folder as the arduino sketch file. 

There are different ways to make intruder detection or alarm system. One way to make intruder detection is to use PIR system which is explained in the tutorial PIR system with Arduino. Another way to make intruder system is to create alarm system with a LASER which is explained in the tutorial Laser Diode & LDR based alarm system using Arduino.

The following shows simulation of HC-SR04 ultrasonic sensor with Arduino in Proteus electronics design software.


Post a Comment

Previous Post Next Post