HC-SR04 + Buzzer + Arduino: Parking Assist Sensor

by UnicornClockworks in Circuits > Sensors

11795 Views, 6 Favorites, 0 Comments

HC-SR04 + Buzzer + Arduino: Parking Assist Sensor

Screen Shot 2017-08-17 at 11.50.51 PM.png
HC-SR04 + Buzzer + Arduino: Parking Assist Sensor

Hello World!

This is Unicorn Clockworks back with another project. Here, we will make a parking assist sensor using an ultrasonic (HC-SR04) sensor and a buzzer.

As your car gets closer to the Ultrasonic sensor beyond the safety net, it will begin beeping warning you that you are getting too close! No more backing into the wall of your garage with this nifty invention!

Materials

Slide2.jpg
  • Arduino or any Arduino-compatible microcontroller
  • HC-SR04 Ultrasonic Sensor
  • Buzzer
  • Jumper Wires
  • [Optional] Breadboard -- you can use female to male jumper wires instead.

HC-SR04 (Ultrasonic Sensor) Connections

Slide3.jpg
Slide4.jpg
Slide5.jpg
Screen Shot 2017-08-17 at 6.37.24 PM.png
  • Connect the Red wire from the VCC pin of the ultrasonic sensor (pin 1) to the 3.3V pin on the Arduino
  • Connect the Green wire from the trigger pin of the ultrasonic sensor (pin 2) to pin 9 on the Arduino
  • Connect the Blue wire from the echo pin of the ultrasonic sensor (pin 3) to pin 10 on the Arduino
  • Connect the Black wire from the ground pin of the ultrasonic sensor (pin 4) to the GND pin on the Arduino

Buzzer Connections

Slide6.jpg
Slide7.jpg
Screen Shot 2017-08-17 at 11.33.57 PM.png
  • Connect the Purple wire from the buzzer pin ( any of them) to pin 6 on the Arduino
  • Connect the Grey wire from the remaining unconnected buzzer pin to the GND pin on the Arduino.

Code

Screen Shot 2017-08-17 at 11.28.38 PM.png
// defines pins numbers
const int trigPin = 9;
const int echoPin = 10;
const int buzzer = 6;
// defines variables
long duration;
int distance;
int safetyDistance;
void setup() {
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
pinMode(buzzer, OUTPUT);
safetyDistance = ultrasonic();
Serial.begin(9600); // Starts the serial communication
}
void loop() {
int dist = ultrasonic();
if (dist <= safetyDistance){
  tone(buzzer, 2500);
  delay(500);
  tone (buzzer, 2500);  
}
else{
  noTone (buzzer);
}
// Prints the distance on the Serial Monitor
Serial.print("Distance: ");
Serial.println(dist);
}
int ultrasonic(){
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculating the distance
distance= duration*0.034/2;
return distance;
}

It Works!

Slide8.jpg

Upload the code while blocking the ultrasonic sensor at a calibrated 'safety distance.' Move your hands towards it and watch it work!

That's it for now. Stay tuned for more!

Unicorn Clockworks Out!