How to Make Distance Meter Using HC-SR04 Ultrasonic Sensor and Arduino Uno
by electronicsinteresting in Circuits > Arduino
263 Views, 2 Favorites, 0 Comments
How to Make Distance Meter Using HC-SR04 Ultrasonic Sensor and Arduino Uno
In this project we’ll see how to make a distance meter using Ultrasonic sensor and Arduino Uno. It’s a very simple project that even a beginner would enjoy making. It has ultrasonic sensor which is the main reason how we can measure distance. The distance meter measures the distance between the ultrasonic sensor and the obstacle placed in front of it. Let’s have fun understanding and building this project!
Supplies
You can find the following components at Quartzcomponents.com
- Arduino Uno (with cable) x1
- HC-SR04 Ultrasonic Sensor x1
- Jumper wires (male to male) x4
- Breadboard x1
Working of Distance Meter Using HC-SR04 and Arduino Uno
The working of the project is simple. An obstacle is placed in front of ultrasonic sensor. Along with this, by using Arduino Uno, we’ll be able to measure the distance between the placed object and the ultrasonic sensor. If the object is moved closer or farther from the sensor, it is also measured and displayed on the serial monitor. The object has to be in-between 2 to 15cm from the sensor. This distance meter, made with ultrasonic sensor and Arduino Uno will be able to measure with 0.2mm of tolerance.
More About HC-SR04 Ultrasonic Sensor
HC-SR04 is a popular ultrasonic sensor which has a range of 2cm to 400cm. The sensor uses SONAR (or ultrasonic sound) waves to detect object. The transmitter part of the Ultrasonic sensor transmits pulses and the receiver part receives the reflected waves. The sensor has 4 pins as shown in the figure. VCC is usually connected to 5V, GND is the ground pin and Trig and Echo pins are used to transmit and receive the ultrasonic waves.
How HC-SR04 Ultrasonic Sensor Works?
When the reflected wave is received, by knowing the travel time of the wave and the speed of the sound, we can calculate the distance. We need to send a pulse of ultrasonic wave, in order to detect any object in its path. This pulse is of 10us (This means, trig pin should be made HIGH for 10us). Then the Echo pin waits to receive the reflected wave.
To calculate Distance, we know that Distance = (Speed x Time). But in our case, it’ll be Distance = (Speed x Time)/2 since the wave is travelling and being reflected back. We know that the speed of Sound is 340m/s or 34cm/s. Hence, the formula will be, Distance = (34cm x Time)/2. Using this formula, we can calculate the distance.
Circuit Connections for Distance Meter Using Arduino Uno and Ultrasonic Sensor
As shown in the circuit, Connect VCC pin of Ultrasonic sensor to 5V pin of Arduino Uno. Connect Trig pin of ultrasonic sensor to D10 of Arduino Uno. Echo pin of ultrasonic sensor to D9 of Arduino Uno. And finally GND pin of Ultrasonic sensor to GND pin of Arduino Uno.
Complete Arduino Code
// defines pins numbers
const int trig = 10;
const int echo = 9;
// defines variables
long durn;
int distn;
void setup() {
pinMode(trig, OUTPUT); // Sets the trigPin as an Output
pinMode(echo, INPUT); // Sets the echoPin as an Input
Serial.begin(9600); // Starts the serial communication
}
void loop() {
// Clears the trigPin
digitalWrite(trig, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trig, HIGH);
delayMicroseconds(10);
digitalWrite(trig, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
durn = pulseIn(echo, HIGH);
// Calculating the distance
distn = durn * 0.034 / 2;
// Prints the distance on the Serial Monitor
Serial.print("Distance: ");
Serial.println(distn);
}
Output of the Project
The distance meter measures the distance between ultrasonic sensor and the obstacle placed in front of it by sending out pulses of ultrasonic waves and measuring the duration of the reflected pulse and hence, calculating the distance.
Conclusion
Distance meter is a simple yet fun and interactive project. Its simplicity enables even beginners to learn this project. This project can also be integrated with 16x2 LCD or OLED display to display the distance measured and also by using a battery, we can make it portable. This gives learner a chance to explore and improve the project.