This project involves using an Arduino to measure distances using an ultrasonic sensor and controlling a buzzer and LED based on the measured distance.
- Arduino board
- Ultrasonic Distance Sensor (HC-SR04)
- Buzzer
- LED
- Connect the Trigger (Trig) pin of the ultrasonic sensor to digital pin 2 on the Arduino.
- Connect the Echo pin of the ultrasonic sensor to digital pin 3 on the Arduino.
- Connect the positive (anode) leg of the buzzer to digital pin 8 on the Arduino.
- Connect the positive (anode) leg of the LED to digital pin 13 on the Arduino.
- Connect the ground (GND) of the ultrasonic sensor, buzzer, and LED to the ground (GND) of the Arduino.
- Connect the VCC of the ultrasonic sensor to the 5V pin on the Arduino.
The provided Arduino code performs the following actions:
- Initializes pins and serial communication.
- Measures the distance using the ultrasonic sensor and calculates the distance in centimeters.
- Controls the buzzer and LED based on the measured distance:
- If distance <= 30 cm, the buzzer beeps rapidly and the LED remains on for 3 seconds.
- If 30 cm < distance < 50 cm, the buzzer beeps slowly and the LED remains on for 2 seconds.
- If 50 cm < distance < 70 cm, the buzzer beeps briefly and the LED remains on for 1 second.
- If none of the above conditions are met, the buzzer and LED are turned off.
- Upload the provided code to your Arduino board using the Arduino IDE.
- Make sure your components are correctly wired as per the wiring instructions.
- Open the serial monitor in the Arduino IDE to view the measured distances.
Remember to adjust the threshold distances and timing as needed for your project's requirements.
This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License. If you are have problem with this repository, you can contact me od Discord: Sponton#4170