Introduction
Do you have a project that you want to do but don’t know where to start? This tutorial is for you! In this article, we will be discussing ultrasonic HC-SR04 and buzzers and how they work. After reading this, you will be able to know how to control them with Arduino and create your very own projects! So Let’s Get Started!
Hardware Components
You will require the following hardware to make Detecting Obstacles with Buzzer.
Components | Value | Qty |
---|---|---|
Arduino UNO | – | 1 |
Ultrasonic Sensor | HC-SR04 | 1 |
Buzzer | – | 1 |
Breadboard | Mini | 1 |
Jumper Wires | – | 1 |
Ultrasonic Sensor HC-SR04
HC-SR04 is an ultrasonic distance sensor. This economical sensor provides a sensing range of 2 to 400cm of non-contact measurement functionality with an accuracy that can reach up to 3mm. Each HC-SR04 module includes an ultrasonic transmitter, a receiver, and a control circuit.
There are only 4 pins that you need to know about the HC-SR04 Sensor: VCC (Power), GND (Ground), Trig (Trigger), and Echo (Receive), and. You will find this sensor very simple and easy to set up and use for your next DIY projects & tutorials.
HC-SR04 Ultrasonic Features
- Operating voltage: +5V
- Theoretical Measuring Distance: 2cm to 450cm
- Practical Measuring Distance: 2cm to 80cm
- Accuracy: 3mm
- Measuring angle covered: <15°
- Operating Current: <15mA
- Operating Frequency: 40Hz
HC-SR04 Ultrasonic Pinout
Pin Number | Pin Name | Description |
1 | Vcc | The Vcc pin powers the sensor, typically with +5V |
2 | Trigger | The trigger pin is an Input pin. This pin has to be kept high for 10us to initialize measurement by sending a US wave. |
3 | Echo | The echo pin is an Output pin. This pin goes high for a period of time which will be equal to the time taken for the US wave to return back to the sensor. |
4 | Ground | This pin is connected to the Ground of the system. |
Detecting Obstacles with Buzzer
Working for this project is very simple when the distance is between 0 to 0.5-meter buzzer is ON when the distance sensed by HC-SR04 is greater than this range buzzer will go OFF.
Installing Arduino IDE
First, you need to install Arduino IDE Software from its official website Arduino. Here is a simple step-by-step guide on “How to install Arduino IDE“.
Code
Now copy the following code and upload it to Arduino IDE Software.
// Define pins for ultrasonic and buzzer
int const trigPin = 10;
int const echoPin = 9;
int const buzzPin = 2;
void setup()
{
pinMode(trigPin, OUTPUT); // trig pin will have pulses output
pinMode(echoPin, INPUT); // echo pin should be input to get pulse width
pinMode(buzzPin, OUTPUT); // buzz pin is output to control buzzering
}
void loop()
{
// Duration will be the input pulse width and distance will be the distance to the obstacle in centimeters
int duration, distance;
// Output pulse with 1ms width on trigPin
digitalWrite(trigPin, HIGH);
delay(1);
digitalWrite(trigPin, LOW);
// Measure the pulse input in echo pin
duration = pulseIn(echoPin, HIGH);
// Distance is half the duration devided by 29.1 (from datasheet)
distance = (duration/2) / 29.1;
// if distance less than 0.5 meter and more than 0 (0 or less means over range)
if (distance <= 50 && distance >= 0) {
// Buzz
digitalWrite(buzzPin, HIGH);
} else {
// Don't buzz
digitalWrite(buzzPin, LOW);
}
// Waiting 60 ms won't hurt any one
delay(60);
}
Schematic
Make connections according to the circuit diagram given below.
Wiring / Connections
Arduino | Ultrasonic Sensor | Buzzer |
---|---|---|
5V | VCC | |
GND | GND | Negative(-) |
PIN 9 | Echo | |
PIN 10 | Trig | |
D2 | – | Positive(+) |
Finally, let’s power up Arduino IDE and test this “Detecting Obstacles with Buzzer” circuit. We hope you found this tutorial useful. If you feel any difficulty in making it feel free to ask anything in the comment section.