This article is a complete guide to using the Ultrasonic Sensor HC-SR04 with Arduino to measure distances. We'll highlight its key features, how it works, and provide a practical example that you can use for your own projects. The schematic diagram and wiring will be provided along with example code.
Content
Understanding the Concept
The ultrasonic sensor takes inspiration from bats' echolocation ability. By emitting high-frequency sound waves (that cannot be heard by human) and analyzing the echoes that bounce back, the sensor accurately determines the distance to objects.
This technology finds application in measuring distances and motion detection, inspired from the nature to create innovative and useful technologies. There are other animals uses the ultrasonic in a form of Sonar which is slightly different from the Ultrasonic sensor. To know the difference between them check the Ultrasonic and Sonar Comparison article.
Ultrasonic Sensor
A common ultrasonic sensor is composed of two ultrasonic transducers. It converts electrical energy into sound waves which are then transmitted to the target object by one of the transducers(Transmitter).
When an ultrasonic sensor emits a short burst of high-frequency sound waves, it travels through the air until it encounters an object. Upon hitting the object's surface, the sound waves reflect back towards the sensor, where they are detected.
HC-SR04 is a common ultrasonic module that could measure distance between 2 - 400 cm.
Specifications
Measuring Distance
Calculating the distance measured by an ultrasonic sensor is based on the time taken by ultrasonic waves to travel from the sensor to the object and back.
The key principle behind the distance formula is the speed of sound in the medium (air, in this case). The formula takes into account the time taken for the sound wave to travel the round trip and the speed of sound to calculate the distance between the sensor and the object. Here are the breakdown of distance measurement steps:
Send a sound pulse out at a speed of sound which is approximately 340 meters per second or 1120 feet per second, through the air.
Wait for the echo: The sound wave travels to an object and bounces back as an echo.
Measure the time: The sensor measures the time it takes for the pulse to travel to the object and back. This is usually measured in microseconds (millionths of a second).
Calculate the distance: To calculate the distance, you can use the following formula:
Where:
Time: is the time it took for the pulse to travel to the object and back (in microseconds).
Speed: is the speed of sound in air which is approximately:
340 meters per second (m/s) or
1120 feet per second (ft/s) or
29 microseconds per centimeter ((1/340) * 10000)) (u/cm)
Dividing by 2 is necessary because the sound wave has to travel to the object and back, so the distance is half of the total distance travelled.
For example, if the sensor measures a time of 1440 microseconds, the distance would be:
Distance = (1440 * 0.00034) / 2
Distance = 0.248 meters
or if we refer to the speed by (29 u/cm) we could calculate the distance as follows:
Distance = Time / [Sound Speed (u/cm) * 2] = 1440 / (29*2) = 24.8 cm
Required Components
Breadboard
Jumper Wires
9V Battery (optional)
Wiring
Arduino UNO | HC SR04 Ultrasonic Sensor |
5V | VCC |
GPIO 02 | TRIG |
GPIO 03 | Echo |
GND | GND |
Ultrasonic Sensor to Arduino UNO Wiring
At this point, the hardware setup is complete and it’s time to start writing code for Arduino board.
Related Video
Summary
In conclusion, an ultrasonic sensor connected to an Arduino can be used to accurately measure distances. By understanding how the sensor works, connecting it properly to the Arduino board, and using the right algorithms, users can take reliable measurements with a high degree of accuracy.
Now that you know how to use an ultrasonic sensor with Arduino for accurate measurements, why not try it out yourself? Give it a go and see what you can create!
Arduino Code
int trigPin = 2; // Trigger
int echoPin = 3; // Echo
float duration, distancecm, distanceinches;
void setup() {
//Serial Port begin
Serial.begin (9600);
//Define inputs and outputs
pinMode(trigPin, OUTPUT); //triggering a sound to bound off an obect
pinMode(echoPin, INPUT); //receiving the sound bounce (echo) from trigger
}
void loop() {
// Standard good measure to include this code ensure a clean pulse signal from sensor
digitalWrite(trigPin, LOW);
delay(5);
digitalWrite(trigPin, HIGH);
delay(10);
digitalWrite(trigPin, LOW);
//setting receiving pin to INPUT again
pinMode(echoPin, INPUT);
//duration (in microseconds) it takes for the sound wave to be sent from sensor to object and return
duration = pulseIn(echoPin, HIGH);
// Convert the travel time of sound into distance, distance = time x speed of sound
distancecm = (duration/2) / 29.1; // or multiply (duration/2) by 0.0343 cm/microsecond
distanceinches = (duration/2) / 74; // or multiply (duration/2) by 0.0135 inch/microsecond
//printing the output in readable form
Serial.print("Distance: ");
Serial.print(distancecm);
Serial.print("cm,");
Serial.print(distanceinches);
Serial.print("in");
Serial.println();
delay(250);
}
Comments