# HOW TO MEASURE DISTANCE USING ULTRASONIC HCSR04 & ARDUINO

In this blog, we are discussed how to measure distance using ultrasonic HCSR04 sensor and Arduino. Let’s take some information about the Ultrasonic HCSR04 sensor. To measure some distance we used it, but how it can work & measure the distance? That we will see in this blog.

So, Ultrasonic sensors are used in a variety of applications. Our ultrasonic sensors made with piezoelectric crystals, use high-frequency sound waves to resonate with the desired frequency and convert electric energy into acoustic energy, and vice versa.

In this what happened that Sound waves are transmitted to and reflected from the target(obstacle) back to the transducer. Targets can have any reflective form, even round. Certain variables, such as target surface angle, changes in temperature and humidity, and reflective surface roughness, can affect the operation of the sensors.

The picture is of Ultrasonic HCSR04 sensor which has 4 pins as below:

1. Vcc (power supply) pin
2. Trig (trigger) pin
3. Echo pin
4. GND pin

## The requirement for the project:

### Hardware components:

• Arduino UNO
• Ultrasonic HCSR04 sensor
• Jumper wires

### Hardware connections as follows:

• Step1: connect the Vcc pin of Ultrasonic HCSR04 to the 5V power supply of Arduino.
• Step2: connect Trig pin of Ultrasonic HCSR04 to the D4 digital pin of Arduino.
• Step3: connect the Echo pin of Ultrasonic HCSR04 to the D5 digital pin of Arduino.
• Step4: connect the GND pin of Ultrasonic HCSR04 to the GND pin of Arduino.

## How does it work?

• In Ultrasonic sensor, if the Trigger pin gets high then it will produce the sound wave.
• That sound wave travel some distance and if there is some object , it will hit.
• After hitting that object it will bounce back and received in the Echo pin.
• Here, the trigger pin is input which generate sound wave of 8 cycle for 10 microsecond when it is high . Echo pin act as output which is proportionly high till the sound wave is receive.
`For example: If we took one object at 10cm, to calculate it we used formula`
`Time= distance/speed`

As we know, speed of sound is 340 m/sec so it will convert to 0.034 cm/usec because we took 10 usec trigger.

If we want to calculate distance the formula became as

`Distance= time*0.034/2   `

(we divide it by 2 because when a sound wave produced it will hit to object and get a bounce back, we will get total time between this duration ).

### Now its time for coding of this project:

```const int trigPin = 4;    // pin no. 5 is D4 ESP8266
const int echoPin = 5;    // pin no. 7 is D5 ESP8266
// defines variables
long duration;
int dist;
void setup()
{
pinMode(trigPin, OUTPUT);    // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
Serial.begin(9600); // Starts the serial communication @9600
}
void loop()
{
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculating the distance
dist= duration*0.034/2;
// Prints the distance on the Serial Monitor
Serial.print("Distance: ");
Serial.println(dist);
}
```

## Steps for getting Program Output

After coding we will follow the below steps for getting the output of the program:

• STEP 1: Connect the Arduino to the USB port of the computer.
• STEP 2: Open two Arduino IDE window and Select Arduino UNO as Board and select the appropriate COM27 port.
• Board: Tools > Board > Arduino UNO.
• STEP 3: Port: Tools > Port in Arduino(COM27 port in your case port no. can be changed) .
• STEP 4: Compile and upload the above program to your Arduino UNO board via Arduino IDE. Readings from the sensor will be obtained in the serial monitor.
• The Output Screenshot of Serial Monitor:

The output will be obtained varied as far object moved towards the sensor or away from the sensor. If the sound wave hits the object at 10cm, so we will obtain output as a 10cm distance.

`Arduino Flame Sensor Interfacing to Build Fire Alarm System`
{"ft_lang_label":"__label__en","ft_lang_prob":0.81675196,"math_prob":0.84935606,"size":4181,"snap":"2023-14-2023-23","text_gpt3_token_len":997,"char_repetition_ratio":0.12880057,"word_repetition_ratio":0.013908206,"special_character_ratio":0.24276489,"punctuation_ratio":0.12265331,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.96385866,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-03-25T09:56:00Z\",\"WARC-Record-ID\":\"<urn:uuid:c925e66d-5f0c-40b5-95cc-94d6c38d9686>\",\"Content-Length\":\"91922\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:15ffd4d7-cc99-4810-80c4-559addfd167e>\",\"WARC-Concurrent-To\":\"<urn:uuid:d54d94cb-1bda-4534-bc0b-a0b636f48351>\",\"WARC-IP-Address\":\"172.67.189.201\",\"WARC-Target-URI\":\"https://pictorobo.in/how-measure-distance-using-ultrasonic-hcsr04-arduino/\",\"WARC-Payload-Digest\":\"sha1:H3GAWD6DNGXWGKXKQCONT2EKWGINV3FZ\",\"WARC-Block-Digest\":\"sha1:B5RVGFDSDAU24OKVV7E3FUCN74R36PNE\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-14/CC-MAIN-2023-14_segments_1679296945323.37_warc_CC-MAIN-20230325095252-20230325125252-00771.warc.gz\"}"}