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!
gidump
Published

Beeping_Proximity_Sensor

This ultrasonic sensor detects objects close to it, the closer they get the faster it beeps.

BeginnerProtip708
Beeping_Proximity_Sensor

Things used in this project

Hardware components

Arduino Mega 2560
Arduino Mega 2560
You can use any controller board.
×1
Ultrasonic Sensor - HC-SR04 (Generic)
Ultrasonic Sensor - HC-SR04 (Generic)
×1
Buzzer
Buzzer
Goes beeeep.
×1
Resistor 220 ohm
Resistor 220 ohm
Otherwise the buzzer is too loud.
×1
Jumper wires (generic)
Jumper wires (generic)
×1
USB-A to B Cable
USB-A to B Cable
Or whatever is right to connect your board and computer
×1
Breadboard (generic)
Breadboard (generic)
×1

Story

Read more

Schematics

Circuit

Code

This code is probably very bad, I have no idea, if it is bad I don't want to know.

C/C++
const int trigPin = 9;
const int echoPin = 10;
int buzzpin=7;

long duration;
int distance;

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

void loop() 
{
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  
  digitalWrite(trigPin, LOW);
  
  duration = pulseIn(echoPin, HIGH);
  distance= duration*0.034/2;
  
 
  Serial.println(distance);
  
  delay(100);

  
 if(distance <= 40 && distance >30)
 {
 digitalWrite(buzzpin, HIGH);
   delay(2000);
   digitalWrite(buzzpin, LOW);
   delay(2000);
 }
 if(distance <= 30 && distance>20)
 {
 digitalWrite(buzzpin, HIGH);
   delay(500);
   digitalWrite(buzzpin, LOW);
   delay(500);
 }
  if(distance <= 20 && distance> 10)
 {
 digitalWrite(buzzpin, HIGH);
   delay(50);
   digitalWrite(buzzpin, LOW);
   delay(50);
 }
 if(distance <= 10)
 {
 digitalWrite(buzzpin, HIGH);
 }
 if(distance>40)
 {
 digitalWrite(buzzpin, LOW);
 }
}

Credits

gidump
0 projects • 0 followers

Comments