Introduction
This project demonstrates how to use an Arduino microcontroller along with an ultrasonic sensor and a buzzer to measure distance. The system emits a warning sound via the buzzer when an object comes within a set distance threshold. This is a simple yet effective way to build an object detection system, useful for applications like parking sensors, obstacle detection, and robotic navigation.
Components Required:
Arduino Uno (or any compatible Arduino board)
Ultrasonic Sensor (HC-SR04)
Buzzer
Jumper wires
Breadboard (optional for circuit assembly)
LEDs (optional, for visual feedback)
Power source (5V supply or Arduino’s USB connection)
Working Principle
Ultrasonic Sensor: The ultrasonic sensor, typically the HC-SR04, sends out sound waves and listens for their echo after they bounce off objects. The time taken for the echo to return helps calculate the distance to the object.
Arduino Processing: The Arduino controls the ultrasonic sensor to trigger the transmission of sound waves and measures the time taken for the echo to return. Using the time value, the Arduino calculates the distance to the object.
Buzzer Activation: When the distance falls below a pre-defined threshold (for example, 10 cm), the Arduino triggers the buzzer to produce a sound, alerting the user to the presence of an object.
Circuit Diagram
HC-SR04 Ultrasonic Sensor Connections:
VCC pin to 5V on Arduino
GND pin to GND on Arduino
TRIG pin to digital pin 9 on Arduino
ECHO pin to digital pin 10 on Arduino
Buzzer Connections:
One terminal of the buzzer to digital pin 8
The other terminal to GND
// Define pins for the ultrasonic sensor and the buzzer
const int trigPin = 9;
const int echoPin = 10;
const int buzzerPin = 8;
// Set the distance threshold
const int threshold = 10; // 10 cm
void setup() {
// Initialize serial communication
Serial.begin(9600);
// Set the sensor pins as output and input
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(buzzerPin, OUTPUT);
}
void loop() {
// Send a pulse to the ultrasonic sensor
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Read the pulse width from the echo pin
long duration = pulseIn(echoPin, HIGH);
// Calculate the distance based on the pulse duration
int distance = duration * 0.0344 / 2; // Speed of sound is 0.0344 cm/us
// Output the distance to the Serial Monitor
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
// Trigger the buzzer if the distance is below the threshold
if (distance < threshold) {
digitalWrite(buzzerPin, HIGH); // Turn on the buzzer
} else {
digitalWrite(buzzerPin, LOW); // Turn off the buzzer
}
delay(500); // Wait for half a second before taking another reading
}
Explanation of Code:
Pin Definitions: The code defines pins for the ultrasonic sensor (TRIG and ECHO) and the buzzer (Buzzer Pin).
Sensor Triggering: The digitalWrite() functions are used to trigger the ultrasonic sensor to send sound waves.
Distance Calculation: The distance is calculated by measuring the pulse duration using pulseIn(), which measures how long the ECHO pin is HIGH. The time is converted into distance.
Buzzer Activation: The buzzer is turned ON if the calculated distance is below the defined threshold value (10 cm in this case).
Applications:
Obstacle Detection: The project can be used in robotics for detecting objects in the robot's path.
Parking Sensors: It can be adapted for use in vehicles to warn drivers when they are too close to an object while parking.
Security Systems: Can be used to detect intruders or objects approaching a sensitive area.
Conclusion
This project demonstrates how easy it is to integrate sensors with an Arduino to create interactive and useful systems. By incorporating an ultrasonic sensor and a buzzer, this simple project can be expanded into a variety of applications that require distance measurement and alerting. The use of Arduino makes it a cost-effective and highly customizable solution for numerous electronic and embedded system applications.
Future Enhancements:
Multiple Buzzer Alerts: Add different sound patterns based on the distance.
Display Output: Integrate an LCD or OLED display to show real-time distance measurements.
Wireless Alert System: Use a wireless module like Bluetooth or Wi-Fi to send distance alerts to a mobile phone or computer.
Integration with Servo Motors: Use the system to control a motor or robotic arm based on object detection.