Ultimate DIY Ultrasonic Security System: Safeguard Your Home Like a Pro With Arduino
by the_tech_atharva in Circuits > Arduino
363 Views, 0 Favorites, 0 Comments
Ultimate DIY Ultrasonic Security System: Safeguard Your Home Like a Pro With Arduino
![a112e5c9-2a6c-41d8-938f-33142bc13fc6.jpg](/proxy/?url=https://content.instructables.com/FA7/ZV2T/LIOLPZSF/FA7ZV2TLIOLPZSF.jpg&filename=a112e5c9-2a6c-41d8-938f-33142bc13fc6.jpg)
Simple tutorial on how to use Ultrasonic sensor with Arduino as a security device that will glow certain lights depending on distance between them and if the distance is too close it will beep the buzzer.
Supplies
![q.jpg](/proxy/?url=https://content.instructables.com/FWJ/GLFD/LIOLPZWW/FWJGLFDLIOLPZWW.jpg&filename=q.jpg)
![61NbRWVI2ML._AC_UL400_.jpg](/proxy/?url=https://content.instructables.com/F93/05RI/LIOLQ06E/F9305RILIOLQ06E.jpg&filename=61NbRWVI2ML._AC_UL400_.jpg)
![61HZWsnVtjL._AC_UL400_.jpg](/proxy/?url=https://content.instructables.com/FYG/FCQ7/LIOLQ06R/FYGFCQ7LIOLQ06R.jpg&filename=61HZWsnVtjL._AC_UL400_.jpg)
![51rzdqdIvvL._SL1050_.jpg](/proxy/?url=https://content.instructables.com/FCT/WM72/LIOLQ09E/FCTWM72LIOLQ09E.jpg&filename=51rzdqdIvvL._SL1050_.jpg)
![61UEA2NqdqL._SL1500_.jpg](/proxy/?url=https://content.instructables.com/FL5/QVZ0/LIOLQ09Q/FL5QVZ0LIOLQ09Q.jpg&filename=61UEA2NqdqL._SL1500_.jpg)
software
we will be using Arduino IDE for programing Arduino.
hardware stuff
I am also giving the links to the products used
- set of jumper wire .
- Arduino uno .
- led lights you can use any led light
- HC-SR04 Ultrasonic Sensor Distance Module
- 1K ohm 1/4 Watt Resistor
- Electronic Spices Mini 5V Buzzer
- Bread Board . I will suggest getting this i am using it for a while and its great.
Gathering Materials
![da24fa5f-d6ee-46ce-abe0-17901c495e1d.jpg](/proxy/?url=https://content.instructables.com/F0F/S52V/LIOLQ066/F0FS52VLIOLQ066.jpg&filename=da24fa5f-d6ee-46ce-abe0-17901c495e1d.jpg)
check all material from the material list and confirm it . So that you can start
Circuit Diagram
![310c903d-9991-4c05-b819-93fd751e3424.jpg](/proxy/?url=https://content.instructables.com/FPP/VJM6/LIOLQ0PK/FPPVJM6LIOLQ0PK.jpg&filename=310c903d-9991-4c05-b819-93fd751e3424.jpg)
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:
On Ultrasonic Sensor:
- Echo = pin 3
- Trig = pin 2
LEDs:
- RedLED = pin 4
- YellowLED = pin 5
- GreenLED = pin 6
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. i used colored ones to differentiate easily.
note : follow the circuit diagram correctly or else it will not work
Assembly - Breadboard
![7683bfe5-9811-4156-b452-a59805963921.jpg](/proxy/?url=https://content.instructables.com/FRD/D8WN/LIOLQ124/FRDD8WNLIOLQ124.jpg&filename=7683bfe5-9811-4156-b452-a59805963921.jpg)
Great, now we’re ready to connect the Arduino to the breadboard. Remember, the wire from the 5V pin goes to the positive channel, and the wire from the GND pin goes to the negative channel. It’s important to get this right, so double-check your connections
Assembly - Ultrasonic Sensor
![a226be87-5f40-4714-9d16-ad2fd31de4b1.jpg](/proxy/?url=https://content.instructables.com/FN6/AV9E/LIOLQ137/FN6AV9ELIOLQ137.jpg&filename=a226be87-5f40-4714-9d16-ad2fd31de4b1.jpg)
Awesome, now let’s hook up the ultrasonic sensor. It’s best to put it on the far right side of the breadboard, facing outwards. Look at the setup picture to see how to connect the pins. The GND pin goes to the negative channel, the Trig pin goes to pin 2 on the Arduino, the Echo pin goes to pin 3 on the Arduino, and the VCC pin goes to the positive channel. Don’t worry if you get confused, just check the picture again
Assembly - LEDs
![d0158918-c152-4161-9ac5-5be74bfc6789.jpg](/proxy/?url=https://content.instructables.com/F9L/STN1/LIOLQ150/F9LSTN1LIOLQ150.jpg&filename=d0158918-c152-4161-9ac5-5be74bfc6789.jpg)
Nice, now we’re going to add the LED’s to the breadboard and Arduino. It’s not hard, just follow the setup picture and do the same thing for each LED. We’ll start with the Green LED. You need to connect the long leg (the anode) to pin 6 on the Arduino with a green wire, and the short leg (the cathode) to the negative channel on the breadboard, using a 1k ohm resistor. Then do the same for the Yellow and Red LED, but connect the yellow one to pin 5 and the red one to pin 4. When you’re done, it should look like the picture above. Resistors are good for protecting your LED’s, so try to use them if you can.
Assembly - Buzzer
![7077bfe2-d446-41e5-9d94-2cca285f1c66.jpg](/proxy/?url=https://content.instructables.com/FAA/U78T/LIOLQ17Q/FAAU78TLIOLQ17Q.jpg&filename=7077bfe2-d446-41e5-9d94-2cca285f1c66.jpg)
Yay, we’re almost done! The last thing we need to do is connect the buzzer to the breadboard and the Arduino. It’s super easy, just follow these steps. Connect the long leg of the buzzer to pin 7 on the Arduino with a green wire, and connect the short leg of the buzzer to the negative channel on the breadboard with a 1k ohm resistor. Please use a resistor for the buzzer, it will make it quieter and last longer
Code
![WhatsApp Image 2023-06-11 at 18.50.34.jpg](/proxy/?url=https://content.instructables.com/F4F/SN8U/LIOLQ254/F4FSN8ULIOLQ254.jpg&filename=WhatsApp Image 2023-06-11 at 18.50.34.jpg)
#define trigPin 2
#define echoPin 3
#define LEDlampRed 4
#define LEDlampYellow 5
#define LEDlampGreen 6
#define soundbuzzer 7
int sound = 500;
int distance = 0;
int distanceincm = 0;
int distanceinsm = 0;
void setup() {
Serial.begin(9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(LEDlampRed, OUTPUT);
pinMode(LEDlampYellow, OUTPUT);
pinMode(LEDlampGreen, OUTPUT);
pinMode(soundbuzzer, OUTPUT);
}
void loop() {
long durationindigit;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
durationindigit = pulseIn(echoPin, HIGH);
distanceincm = (durationindigit / 5) / 29.1;
if (distanceincm < 50) {
digitalWrite(LEDlampGreen, HIGH);
} else {
digitalWrite(LEDlampGreen, LOW);
}
if (distanceincm < 20) {
digitalWrite(LEDlampYellow, HIGH);
} else {
digitalWrite(LEDlampYellow, LOW);
}
if (distanceincm < 5) {
digitalWrite(LEDlampRed, HIGH);
sound = 1000;
} else {
digitalWrite(LEDlampRed, LOW);
}
if (distanceincm > 5 || distanceinsm <= 0) {
Serial.println("Outside the permissible range of distances");
noTone(soundbuzzer);
} else {
Serial.print(distanceincm);
Serial.println(" cm");
tone(soundbuzzer, sound);
}
delay(300);
}
Working
- as you get close to ultrasonic
- the arduino will turn on respective LED to indicated that something is closing
- as soon as it will come close to the specified threshold it will start beeping