Skip to content

Latest commit

 

History

2 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 

Repository files navigation

Ultrasonic Sensor with Buzzer Alarm

This Arduino project uses an ultrasonic sensor (HC-SR04) to measure the distance to the nearest object. If the object is detected within a certain range, a piezo buzzer will sound a security alarm, alternating between two different tones. Additionally, an LED is activated when the object is within the predefined range.

Components Used

  • Arduino Uno
  • HC-SR04 Ultrasonic Sensor
  • Piezo Buzzer
  • LED
  • Jumper Wires
  • Breadboard

Circuit Connections

Ultrasonic Sensor (HC-SR04)

  • VCC: Connect to 5V on Arduino
  • GND: Connect to GND on Arduino
  • Trig Pin: Connect to Digital Pin 7
  • Echo Pin: Connect to Digital Pin 8

Piezo Buzzer

  • Positive: Connect to Digital Pin 3
  • Negative: Connect to GND on Arduino

LED

  • Anode (+): Connect to Digital Pin 2 (with a resistor)
  • Cathode (-): Connect to GND on Arduino

How It Works

  1. The ultrasonic sensor emits a sound wave from its Trig Pin, and the time it takes for the wave to return to the sensor is measured by the Echo Pin.
  2. The time duration of the returning pulse is used to calculate the distance to the nearest object.
  3. If an object is detected within a certain range (in this case, less than 200 microseconds), the LED lights up, and the piezo buzzer emits an alternating alarm sound, simulating a security alert.
  4. If no object is detected within that range, the LED remains off, and the buzzer is silent.

Code Breakdown

  • Pin Setup:

    • trigPin = 7 and echoPin = 8: Pins for the ultrasonic sensor.
    • buzzerPin = 3: Pin for the piezo buzzer.
    • pinMode(2, OUTPUT): Pin 2 is used to control an LED.
  • Distance Calculation:

    • The distance is calculated using the pulseIn() function, which measures the time it takes for the sound pulse to return to the sensor.
  • Alarm Triggering:

    • If the measured pulse duration is less than or equal to 200 microseconds, the buzzer and LED are triggered.
  • Buzzer Sound:

    • The buzzer alternates between two frequencies (1000 Hz and 600 Hz) with delays in between to create an alarm-like sound.

How to Use the Code

  1. Connect the components as per the circuit diagram described above.
  2. Upload the code to your Arduino board.
  3. Once uploaded, the sensor will continuously monitor the distance, and the buzzer will sound if an object is within range.
  4. The LED will also light up if the object is detected within the predefined range.

Adjustments

  • You can modify the if (duration <= 200) condition to change the range at which the alarm is triggered by adjusting the threshold based on your requirements.
  • The buzzer frequency and duration can also be changed to create different alarm effects.

License

This project is in the public domain and open for anyone to use and modify.

About

This Arduino project uses an HC-SR04 ultrasonic sensor to measure the distance to objects. When an object is detected within a specified range, a piezo buzzer sounds an alarm, and an LED lights up. The project is ideal for simple proximity-based alert systems like security alarms.

Topics

Resources

Stars

1 star

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages