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

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
61NbRWVI2ML._AC_UL400_.jpg
61HZWsnVtjL._AC_UL400_.jpg
51rzdqdIvvL._SL1050_.jpg
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

  1. set of jumper wire .
  2. Arduino uno .
  3. led lights you can use any led light
  4.  HC-SR04 Ultrasonic Sensor Distance Module
  5. 1K ohm 1/4 Watt Resistor
  6. Electronic Spices Mini 5V Buzzer
  7. 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

check all material from the material list and confirm it . So that you can start

Circuit Diagram

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:

  1. Echo = pin 3
  2. Trig = pin 2 

LEDs:

  1. RedLED = pin 4
  2. YellowLED = pin 5
  3. 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

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

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

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

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
#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