Hackster is hosting Hackster Holidays, Ep. 7: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Friday!Stream Hackster Holidays, Ep. 7 on Friday!
Saaket Poray
Published © GPL3+

Proximity Detector Alarm

Hear a "beep" if someone gets too close!

BeginnerShowcase (no instructions)1 hour5,109
Proximity Detector Alarm

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
×1
HC-SR04 Ultrasonic Sensor
×1
LED (generic)
LED (generic)
×1
Speaker 0.5 w
Any speaker that is 0.5 W or low power is fine. I just used a speaker I found from another toy.
×1
Jumper wires (generic)
Jumper wires (generic)
×9
Breadboard (generic)
Breadboard (generic)
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Schematics

Diagram

Schematic

Github

Code

proximitydetectoralarm

Arduino
#define trigPin 13
#define echoPin 12
#define speaker 11
#define led2 10

void setup() {
  Serial.begin (9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(speaker, OUTPUT);
  pinMode(led2, OUTPUT);
}

void loop() {
  long duration, distance;
  digitalWrite(trigPin, LOW);  
  delayMicroseconds(2); 
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10); 
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration/2) / 29.1;
  if (distance < 192) {  // set your distance
  tone(speaker,7); 
  digitalWrite(led2,HIGH);
}
 
  delay(50); //change the rate at which the speaker pings
}

Credits

Saaket Poray
6 projects • 14 followers

Comments