Introduction: Arduino Security System

This Arduino circuit will create a security system using an ultra sonic sensor to monitor the specific area. The system will have an ultra sonic sensor attached to a servo, moving across continuously while measuring the distance between the sensor and the objects in the area. If the sensor detects an objects that is too close, it will trigger a buzzer as well as an LED to notify the presence of the object. This security system is a simple and affective tool for basic perimeter monitoring.

Step 1: Materials and Cost

For this project, you need the following materials:


4x resistors: Three 330 Ω resistors ($0.87 each), One 10K resistor ($0.12 each)

1x Buzzer ($0.52)

2x LEDs ($0.28 each)

1x Pushbutton ($0.08 each)

1x Servo Motor ($4.95)

1x Arduino Uno ($19.95)

1x Ultra Sonic Sensor ($4.99)

1x Breadboard ($4.16)

Wires ($13.99)

Step 2: Wiring

Buzzer = Pin 6

Red LED = Pin 7

Green LED = Pin 8

Pushbutton = Pin 4

Sensor Trigger Pin = Pin 9

Sensor Echo Pin = Pin 10

Servo Motor = Pin 5

Step 3: Upload the Code

In this code, the servo motor will continuously rotate with the ultra sonic sensor attached to it. When the sensor is able to detect something that is too close, it will turn on the red LED and the buzzer, and make then continue to go off until the object that was detected has moved away. When the ultra sonic sensor is unable to detect anything, the green LED will stay on, only turning off when the sensor is able to detect an object. The button on this circuit automatically turns on the buzzer and the LED, having both turn on and off 3 times before stay on, when the button is pressed, the servo will stop moving as well as the ultra sonic sensor not controlling the buzzer and the LEDs.



#include <Servo.h>


Servo myServo;

const int servoPin = 5;

int pos = 0;


const int trigPin = 9;

const int echoPin = 10;

const int buzzer = 6;

const int ledPin = 7;

const int ledPin2 = 8;

const int buttonPin = 4;

int buttonState = 0;


// defines variables

long duration;

int distance;

int safetyDistance;


void setup() {

pinMode(trigPin, OUTPUT);

pinMode(echoPin, INPUT);

pinMode(buzzer, OUTPUT);

pinMode(ledPin, OUTPUT);

pinMode(ledPin2, OUTPUT);

pinMode(buttonPin, INPUT);


Serial.begin(9600); // Starts the serial communication

myServo.attach(servoPin);

myServo.write(pos);

}


void loop() {

for (pos = 0; pos <= 90; pos++) {

myServo.write(pos);

delay(15);

}

for (pos = 90; pos >= 0; pos--) {

myServo.write(pos);

delay(15);

buttonState = digitalRead(buttonPin); // Read the state of the button


if (buttonState == HIGH) {

// Flash LED 3 times when button is pressed

for (int i = 0; i < 3; i++) {

digitalWrite(ledPin, HIGH); // Turn on green LED

digitalWrite(buzzer, LOW); // Turn off buzzer

digitalWrite(ledPin2, LOW); // Turn off red LED

delay(500);

digitalWrite(ledPin, LOW); // Turn off green LED

digitalWrite(buzzer, HIGH); // Turn on buzzer

digitalWrite(ledPin2, LOW); // Turn off red LED

delay(500);

}


// Wait while the button is still pressed

while (digitalRead(buttonPin) == HIGH) {

digitalWrite(ledPin, HIGH); // Turn off green LED

digitalWrite(buzzer, LOW); // Turn on buzzer

digitalWrite(ledPin2, LOW); // Turn off red LED }

}

} else {

// If button is not pressed, measure the distance and control the buzzer and LEDs

// Clears the trigPin

digitalWrite(trigPin, LOW);

delayMicroseconds(2);


// Sets the trigPin on HIGH state for 10 microseconds

digitalWrite(trigPin, HIGH);

delayMicroseconds(10);

digitalWrite(trigPin, LOW);


// Read the echoPin, returns the sound wave travel time in microseconds

duration = pulseIn(echoPin, HIGH);


// Calculating the distance

distance = duration * 0.034 / 2;


safetyDistance = distance;

if (safetyDistance <= 20) {

// If distance is <= 20 cm

digitalWrite(buzzer, LOW); // Turn off buzzer

digitalWrite(ledPin, HIGH); // Turn on green LED

digitalWrite(ledPin2, LOW); // Turn off red LED

} else {

// If distance is greater than 20 cm

digitalWrite(buzzer, HIGH); // Turn on buzzer

digitalWrite(ledPin, LOW); // Turn off green LED

digitalWrite(ledPin2, HIGH); // Turn on red LED

}


// Prints the distance on the Serial Monitor

Serial.print("Distance: ");

Serial.println(distance);

}

}}