Introduction
In this tutorial, we are going to Measure Distance with Arduino and Ultrasonic Sensor HC-SR04, In this article, we’ll explore the incredible capabilities of this sensor and its seamless integration with Arduino for precise distance measurements. From robotics to home automation, the HC-SR04 opens up a plethora of exciting applications. Let’s delve into this fascinating journey of measuring distance with Arduino and the versatile HC-SR04 Ultrasonic Sensor. Also, see the KY-050 Ultrasonic Distance Sensor Module.
Hardware Required
You will require the following Hardware Components for Measure Distance with Arduino and Ultrasonic Sensor HC-SR04
Components | # | Buy From Amazon |
---|---|---|
Arduino UNO | 1 | Buy Link |
Ultrasonic Sensor HC-SR04 | 1 | Buy Link |
9v DC Adapter (Optional) | 1 | Buy Link |
Jumper Wires | 4 | Buy Link |
Breadboard | 1 | Buy Link |
What is an Ultrasonic Sensor?
An ultrasonic sensor is a distance-measuring device. Its operation is based on the sending of a high-frequency pulse, not audible by the human being. This pulse bounces off nearby objects and is reflected back to the sensor, which has a suitable microphone for that frequency.
By measuring the time between pulses, and knowing the speed of sound, we can estimate the distance of the object against whose surface the ultrasonic pulse impacted.
Ultrasonic sensors are cheap sensors, and easy to use. The theoretical measurement range of the HC-SR04 sensor is 2cm to 400cm, with a resolution of 0.3cm. In practice, however, the actual measurement range is much more limited, around 20cm at 2 meters.
Ultrasonic sensors are low precision sensors . The orientation of the surface to be measured can cause the wave to be reflected, falsifying the measurement. In addition, they are not suitable in environments with a large number of objects, since the sound bounces off the surfaces, generating echoes and false measurements. They are also not suitable for outdoor and open-air operation.
Despite this low precision, which makes it impossible to accurately know the distance to an object, ultrasonic sensors are widely used . In robotics, it is common to mount one or several of these sensors, for example, to detect obstacles, determine the position of the robot, create environment maps, or solve mazes.
In applications where superior distance measurement accuracy is required, they are often accompanied by infrared distance meters and optical sensors.
How does an Ultrasonic Sensor work?
The sensor is simply based on measuring the time between sending and receiving a sound pulse. We know that the speed of sound is 343 m/s under conditions of temperature 20 ºC, 50% humidity, and atmospheric pressure at sea level. Transforming units results
In other words, sound takes 29.2 microseconds to travel one centimeter. Therefore, we can obtain the distance from the time between the emission and reception of the pulse by means of the following equation.
The reason we’re dividing time by two (in addition to the speed of sound in the appropriate units, which we calculated earlier) is that we’ve measured the time it takes for the pulse to go and come back, so the distance traveled by the pulse is double what we want to measure.
Pinout of Ultrasonic Sensor
Pinout Configuration of Ultrasonic Sensor
Pin Name | Description |
---|---|
Vcc | The Vcc pin powers the sensor, typically with +5V |
Trigger | Trigger pin is an Input pin. This pin has to be kept high for 10us to initialize measurement by sending US wave. |
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. |
Ground | This pin is connected to the Ground of the system. |
Circuit Diagram
The following circuit shows you the connection of the Measure Distance with Arduino and Ultrasonic Sensor HC-SR04 Please make the connection carefully
Circuit Connections
Arduino | Ultrasonic Sensor |
---|---|
+5V | VCC Pin |
GND | GND Pin |
D5 | Echo |
D6 | Trig |
Installing Arduino IDE Software
First, you will require to Download the updated version of Arduino IDE Software and Install it on your PC or laptop. if you Learn How to install the Arduino step-by-step guide then click on how to install Arduino Button given Blow
Code example
without bookstores
To activate the sensor we need to generate an electrical pulse on the Trigger pin of at least 10us. Previously, we will set the pin to Low for 4us to ensure a clean shot.
Later we use the “pulseIn” function to obtain the time required for the pulse to return to the sensor. Finally, we will convert time into distance using the corresponding equation.
Note that we try to always use integer arithmetic, avoiding using floating point numbers . This is because floating point operations slow down the processor a lot, and involve loading a large number of libraries into memory.
//For more Projects: www.arduinocircuit.com
const int EchoPin = 5;
const int TriggerPin = 6;
void setup() {
Serial.begin(9600);
pinMode(TriggerPin, OUTPUT);
pinMode(EchoPin, INPUT);
}
void loop() {
int cm = ping(TriggerPin, EchoPin);
Serial.print("Distance: ");
Serial.println(cm);
delay(1000);
}
int ping(int TriggerPin, int EchoPin) {
long duration, distanceCm;
digitalWrite(TriggerPin, LOW); // To generate a clean pulse, set to LOW for 4us
delayMicroseconds(4);
digitalWrite(TriggerPin, HIGH); // Generate a 10us trigger
delayMicroseconds(10);
digitalWrite(TriggerPin, LOW);
duration = pulseIn(EchoPin, HIGH); // Measure the time between pulses, in microseconds
distanceCm = duration * 10 / 292 / 2; // Convert to distance, in cm
return distanceCm;
}
With NewPing library
Another option is to use a library to facilitate the process, such as the NewPing library available in the library manager of the Arduino IDE.
The NewPing library provides additional functions, such as the option to perform a median filter to eliminate noise, or use the same pin as trigger and echo, which allows us to save many pins in case of having multiple ultrasonic sensors.
The library provides several examples to illustrate its use. The following example based on them shows the operation with a single pin for trigger and echo.
//For more Projects: www.arduinocircuit.com
#include <NewPing.h>
const int UltrasonicPin = 5;
const int MaxDistance = 200;
NewPing sonar(UltrasonicPin, UltrasonicPin, MaxDistance);
void setup() {
Serial.begin(9600);
}
void loop() {
delay(50); // Wait 50ms between pings (minimum 29ms)
Serial.print(sonar.ping_cm()); // Get the value in cm (0 = out of range)
Serial.println("cm");
}
Applications
- Obstacle Avoidance for Robots: One of the most common applications of the HC-SR04 sensor is in robotics, where it enables autonomous robots to detect obstacles in their path and navigate around them.
- Smart Parking Systems: In smart parking projects, the HC-SR04 sensor can be used to detect the presence of vehicles in parking spaces.
- Distance Measurements in Industrial Settings: The HC-SR04 sensor finds applications in industrial environments for distance measurements in conveyor belt systems, material handling, and object sorting processes.
- Proximity Sensing for Home Automation: In home automation projects, the HC-SR04 sensor can be used for proximity sensing, allowing smart devices to detect the presence of individuals or objects in their vicinity.