Buzzer Sonar

A Sonar Radar that uses HC-06 to measure the distance of the object infront of it, and then beep faster as the object comes closer.   Component List:
  • 1 x Arduino Uno R3
  • Jumper Wires
  • 1 x Buzzer
  • 1 x HC-06
 
Wiring Guide:
  1. Connect the HC-SR04 Ultrasonic Sensor:
    • Connect the VCC pin of the sensor to the 5V pin on the Arduino (or breadboard).
    • Connect the GND pin of the sensor to the GND pin on the Arduino (or breadboard).
    • Connect the TRIG pin of the sensor to digital pin 9 on the Arduino.
    • Connect the ECHO pin of the sensor to digital pin 10 on the Arduino.
  2. Connect the Buzzer:
    • Connect the positive (longer) leg of the buzzer to digital pin 8 on the Arduino.
    • Connect the negative (shorter) leg of the buzzer to the GND pin on the Arduino (or breadboard).
    CODE:
#define TRIG_PIN 9
#define ECHO_PIN 10
#define BUZZER_PIN 8
void setup() {
  Serial.begin(9600);
  pinMode(TRIG_PIN, OUTPUT);
  pinMode(ECHO_PIN, INPUT);
  pinMode(BUZZER_PIN, OUTPUT);
}
void loop() {
  long duration, distance;


  // Clear the trigger pin
  digitalWrite(TRIG_PIN, LOW);
  delayMicroseconds(2);
  // Send a 10 microsecond pulse to the trigger pin
  digitalWrite(TRIG_PIN, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIG_PIN, LOW);
  // Measure the time taken for the pulse to return
  duration = pulseIn(ECHO_PIN, HIGH);
  // Calculate the distance in centimeters
  distance = (duration / 2) / 29.1;
  // Print the distance to the Serial Monitor
  Serial.print("Distance: ");
  Serial.print(distance);
  Serial.println(" cm");
  // Set a longer delay if distance is above 60
  int beepDelay = (distance > 60) ? 1000 : map(distance, 2, 60, 50, 500);


  // Set a shorter beep duration
  int beepDuration = 50;
  // Control the buzzer based on the distance
  tone(BUZZER_PIN, 1000, beepDuration); // Constant frequency for the example
  delay(beepDuration);


  // Stop the buzzer after the beep duration
  noTone(BUZZER_PIN);
  // Add an additional delay for distances above 60
  if (distance > 60) {
    delay(1000); // 1-second delay between beeps
  } else {
    delay(beepDelay); // Delay between beeps
  }
}
  Looking to build this project? Check out our custom, Bahraini Assembled, starter kit here! https://www.bitware.store/product/arduino-uno-r3-compatible-with-arduino-ide/

[bitware_arabic_portfolio_content]

Project Details

Level: Beginner (1/5)
Category: Monitoring System
Platform: Arduino
Creator: BitWare Store