Ultrasonic Sensor With Buzzer
When I came onto instructables as a beginner I couldn't find any Ultrasonic Sensor with Buzzer that code that actually worked, So I learned how to create my own code and this was one of my first projects and I wanted to share it with other people who are also beginners and make them be able to learn.
Supplies
- 1 Ultrasonic sensor
- 1 Buzzer
- 6 Jumper wires
- 1 BreadBoard
- Arduino Uno
- An ultrasonic sensor measures distance by emitting a sound wave and calculating how long it takes for the echo to return after bouncing off an object.
- A buzzer is an electronic device that produces a sound when an electric current passes through it, often used for alerts, notifications, or alarms. It can generate a variety of tones or beeps depending on its design and how it's driven.
- A jumper wire is a short, flexible wire used to make connections between different points on a circuit board or breadboard, allowing for easy modification and testing of electronic circuits.
- A breadboard is a tool used for building and testing electronic circuits without soldering. It consists of a grid of holes arranged in rows and columns. The holes are connected internally in specific patterns to allow components to be placed and connected easily. The breadboard typically has bus strips at the top and bottom for power distribution and terminal strips in the center for connecting components. You insert components and use jumper wires to create connections, enabling you to prototype and test circuits quickly. It’s ideal for experimentation and learning, but not suited for permanent or high-frequency circuits.
Connections
HC-SR04 or Ultrasonic sensor
- VCC = VIN
- TRIG = PIN 12
- ECHO = PIN 13
- GND = (Power) GND
Buzzer
- Postive (+) = PIN 8
- Negative ( - ) = (Power) GND
And that's it! See? Not so hard.
Code
#define trigPin 12
#define echoPin 13
#define Buzzer 8
void setup() {
Serial.begin(9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(Buzzer, OUTPUT);
}
void loop() {
int duration, distance;
// Send a pulse to the trigger pin
digitalWrite(trigPin, HIGH);
delayMicroseconds(10); // short pulse
digitalWrite(trigPin, LOW);
// Measure the duration of the echo pulse
duration = pulseIn(echoPin, HIGH);
// Calculate the distance
distance = (duration / 2) / 29.1;
if (distance <= 80) {
// Object detected within 80 cm
Serial.println("Object detected");
tone(Buzzer, 1000); // Emit a 1000 Hz tone
} else {
// No object detected
Serial.println("No object detected");
noTone(Buzzer); // Turn off the buzzer
}
delay(300); // Small delay before the next measurement
Finished Point
Thank you for trying my code. If your have any questions please don't be afraid to email me at mrelectro025@gmail.com