Arduino

# Measure Distance using Ultrasonic Sensor

Story Highlights
• Working principle
• Measure The Distance

Ultrasonic Sensor HC-SR04 type of Proximity sensors, one of the most common sensors used in various projects this sensor works by ultrasonic waves it is easy to use, many applications, easy to program and cheap. We will use this sensor with Arduino to build projects.

### Working principle

The ultrasonic sensor is used in many applications, for example, it can be used as a radar in drones, The operation of the Ultrasonic Sensor wave sensor is to measure the distance the sensor has a part to send high-frequency sound waves called by Trigger. When the wave hits something reflected back to sensor The wave receives a part called by Echo.

Inside Ultrasonic Sensor measure the distance related to sonic speed and time between the emission Trigger and reception Echo. When these waves bounce, the time is taken to return to the sensor and the distance is calculated using the following simple relationship:

Speed=distance/time

Also, distance=speed*time

where the speed is constant, the speed of sound 340 meters per second at 25 degrees Celsius and varies from material to another depending on the carrier such as liquids, gases, and solid objects. we will focus on the transmission of ultrasound in a vacuum. But here we calculate the signal in centimeters and the speed of sound will be 0.0340 centimeters/microsecond but we notice in the picture that the sensor will send and receive a signal and that allows the distance value to multiply and we need to divide it by 2 to become the equation in the final form:

Distance = time * speed/2

The maximum distance in this sensor varies depending on the model and manufacturer in this tutorial we will use HC-SR04. for more details, you can download Datasheet. The following figure shows how it works:

In order to work the sensor must take into the maximum distance and the body within the range of the sensor, where the object is out of range the sensor output will be zero.

This sensor has 4 pins as shown in the following table:

### Measure The Distance

Let’s start using the Ultrasonic Sensor with the Arduino to measure the distance, in this experiment we will need to any Arduino board and components in the following table and the latest version of Arduino Software (IDE). if you use older versions you need to download an Ultrasonic library

#### Schematic Diagram

In this project, we will connect the ultrasonic sensor to the digital input and output 12 and 13 we connect GND and 5V from the Arduino board, Where we will take distances from the sensor and display them on the serial monitor.

Connections of Ultrasonic HC-SR04 Sensor with Arduino UNO R3 As follows:

#### Code

Upload this code to Arduino Uno board or any board you own:

```/* www.tocircuit.com
Prepared by Fawzi Al-azraq
*/

//First Defines pins numbers for trig & echo
int trigPin = 12;
int echoPin = 13;

// Now defines variables
long dur;  //duration
int  dis;  //distance

void setup()// here we defines output and input
{
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
Serial.begin(9600);
}

void loop() {

digitalWrite(trigPin, LOW);
delayMicroseconds(3);

digitalWrite(trigPin, HIGH);
delayMicroseconds(5);
digitalWrite(trigPin, LOW);

dur = pulseIn(echoPin, HIGH);

// Calculating the distance
dis= dur*0.034/2;

// Prints the distance on the Serial Monitor
Serial.print("Distance is: ");
Serial.println(dis);
}```

Now after uploading the code to the Arduino board open serial monitor and watch the distance of stuff. You can use this distance to read directly or combine it with other commands.

1. now says:

Hello there, I discovered your site by the use of
Google while searching for a comparable topic, your website came up, it
seems great. I have bookmarked it in my google bookmarks.

Hello there, just became aware of your weblog
via Google, and located that it is really informative.

I am going to watch out for brussels. I will appreciate if you
proceed this in future. A lot of other people
can be benefited out of your writing. Cheers!

2. fleck review says:

Your style is very unique compared to other folks
I’ve read stuff from. Many thanks for posting when you have the opportunity, Guess I’ll just bookmark this site.