Introduction: HC-SR04 + Buzzer + Arduino: Parking Assist Sensor

About: I love to make projects based on microcontrollers including robots and IoT technologies. Absolutely, truly, madly, deeply in love with planes. I also dabble in the art of Origami and play chess in my spare ti…

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!

Step 1: Materials

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

Step 2: HC-SR04 (Ultrasonic Sensor) Connections

  • 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

Step 3: Buzzer Connections

  • 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.

Step 4: Code

// 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;
}

Step 5: It Works!

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!