Introduction
The world of electronics has brought a lot of innovations that make our daily lives more comfortable and convenient. One such innovation is the HC-SR04 ultrasonic distance sensor. This sensor can measure distances between two objects, and it is widely used in robotics, automation, and security systems.
In this article, we will explore how to interface an “HC-SR04 Ultrasonic Distance Sensor” with Arduino. This tutorial will guide you through the process of connecting the sensor to an Arduino board, writing a code to control the sensor, and reading the distance measurement data. So let’s get started!
What is HC-SR04 Ultrasonic Distance Sensor?
The HC-SR04 ultrasonic distance sensor is a popular electronic component that can measure distances between two objects. It uses ultrasonic waves to determine the distance by sending out a high-frequency sound wave and measuring the time it takes for the wave to bounce back from the object. The sensor consists of an ultrasonic transmitter, a receiver, and a control circuit. It operates on a 5V DC power supply and can measure distances up to 4 meters with an accuracy of 3mm.
Hardware Components
To interface an HC-SR04 Ultrasonic Distance Sensor with Arduino, you’ll need the following hardware components to get started:
Components | Value | Qty |
---|---|---|
Arduino UNO | – | 1 |
USB Cable Type A to B | – | 1 |
DC Power for Arduino | – | 1 |
Ultrasonic Distance Sensor | HC-SR04 | 1 |
Breadboard | – | 1 |
Jumper Wires | – | 1 |
HC-SR04 Ultrasonic Distance Sensor Pinout
Pin Number | Pin Name | Pin Description |
---|---|---|
1 | Vcc | The Vcc pin powers the sensor, typically with +5V |
2 | Trigger | 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. |
3 | Echo | 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. |
HC-SR04 Ultrasonic Distance Sensor Circuit
Make connections according to the circuit diagram given below.
Wiring / Connections
Arduino | HC-SR04 |
---|---|
5V | VCC |
GND | GND |
D2 | TRIG |
D3 | ECHO |
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“.
Installing Libraries
Before you start uploading a code, download and unzip the following libraries at /Program Files(x86)/Arduino/Libraries (default), in order to use the sensor with the Arduino board. Here is a simple step-by-step guide on “How to Add Libraries in Arduino IDE“.
Code
Now copy the following code and upload it to Arduino IDE Software.
// Define Trig and Echo pin:
#define trigPin 2
#define echoPin 3
// Define variables:
long duration;
int distance;
void setup() {
// Define inputs and outputs:
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
//Begin Serial communication at a baudrate of 9600:
Serial.begin(9600);
}
void loop() {
// Clear the trigPin by setting it LOW:
digitalWrite(trigPin, LOW);
delayMicroseconds(5);
// Trigger the sensor by setting the trigPin high for 10 microseconds:
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Read the echoPin, pulseIn() returns the duration (length of the pulse) in microseconds:
duration = pulseIn(echoPin, HIGH);
// Calculate the distance:
distance = duration * 0.034 / 2;
// Print the distance on the Serial Monitor (Ctrl+Shift+M):
Serial.print("Distance = ");
Serial.print(distance);
Serial.println(" cm");
delay(50);
}
Code Explanation
This Arduino code is a simple program that interfaces the HC-SR04 ultrasonic distance sensor with the Arduino board. It defines the Trig and Echo pins used to interface with the sensor. The program initializes the sensor by setting the input and output pins using the pinMode() function. It also sets up the Serial communication to print the distance measurement data to the Serial Monitor.
In the loop function, the code sends a signal to the Trig pin to trigger the sensor and then reads the response on the Echo pin using the pulseIn() function. The duration of the pulse is then used to calculate the distance of the object from the sensor. The distance is printed on the Serial Monitor using the Serial.print() function. The delay(50) function is used to introduce a small delay between each measurement.
Overall, this code provides a simple example of how to interface the HC-SR04 ultrasonic distance sensor with the Arduino board and read the distance measurement data.
Application
Here are some applications of the HC-SR04 Ultrasonic Sensor:
- Distance measurement: The HC-SR04 is commonly used for measuring distance. It sends an ultrasonic pulse and measures the time it takes for the pulse to bounce back, which can be used to calculate the distance to an object.
- Obstacle avoidance: By measuring the distance to objects in their surroundings, the HC-SR04 can be used to help robots or drones navigate and avoid obstacles.
- Object detection: The sensor can be used to detect the presence of objects, for example in security systems or industrial automation.
- Liquid level measurement: The HC-SR04 can be used to measure the level of liquids in tanks or containers.
- Parking assistance: The sensor can be used to assist drivers in parking by providing feedback on the distance to obstacles behind or in front of the vehicle.
- Motion detection: By measuring the distance to objects that are moving, the sensor can be used to detect motion in its surroundings. This can be useful in security systems or automation applications.
Conclusion
Interfacing an HC-SR04 ultrasonic distance sensor with Arduino is an easy and straightforward process that can be done by anyone with a basic understanding of electronics and programming. This tutorial has provided you with the necessary information to get started with this project. Remember, this sensor has a wide range of applications, and it can be used in different projects to measure distances