## 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.  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!

### 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 in 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.

### Components required

You can find the following components at Quartzcomponents.com

### 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.

### Code Explanation

In Arduino IDE, before “void setup()” we define the pins. Here, since we have Trig and Echo pins, we define those two pins as constant and assign it to pin number 10 and 9.

const int trig = 10;
const int echo = 9;

We define two variables, Duration (durn) and Distance (distn) as long and int type of data respectively.

long durn;
int distn;
void setup() {
pinMode(trig, OUTPUT);
pinMode(echo, INPUT);
Serial.begin(9600);
}

In “void setup()” we set trig pin as output since this pin sends ultrasonic waves to detect obstacle and Echo pin as input since this receives the reflected ultrasonic waves. We start the serial communication to display in serial monitor by using the function “Serial.begin” and 9600 is the baud rate set.

void loop() {
digitalWrite(trig, LOW);
delayMicroseconds(2);

This part of the code is written to clear the trig pin and wait for 2 microseconds.

digitalWrite(trig, HIGH);
delayMicroseconds(10);
digitalWrite(trig, LOW);

This part of the code will set the trig pin on HIGH for 10 microseconds and changes the state back to LOW. This will cause the sensor to send a pulse of ultrasonic wave. When this wave hits any obstacle, it reflects back. This is then used to do further calculations.

durn = pulseIn(echo, HIGH);

Here, duration is calculated by measuring received pulse length. This is done by reading the echo pin and returning the travel time of the wave in microseconds.

distn = durn * 0.034 / 2;

Distance is calculated by multiplying the duration time by the speed of sound wave and dividing by 2 because the wave would have travelled, hit the object, and travelled back to be received by echo pin.

Serial.print("Distance: ");
Serial.println(distn);
}

This part is used to print the distance on the Serial monitor. A simple inbuilt “Serial.println” function is used. This displays the distance value in the serial monitor.

### 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.

### Complete 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);

### 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.

For more such interesting projects, check out quartzcomponents.com