Using Distance Sensor in Arduino (HC-SR04)

There is a component which can be used to measure distances by emitting ultrasonic wave to an object and calculating the time it takes for the echo to come back (from the time the sensor emitted the ultrasonic wave. Generally, the echo goes back to the sensor at two times the distance (First distance is from the sensor to the object, second distance is from the object back to the sensor).

The speed of sound is calculated at 343 meters / second. If you recall your Physics class in intermediate school (or even college), you know that:

velocity (v) = distance (d) / time (t) or simply, v = d/t

By using the equation above (v = d/t), we can re-arrange the equation to solve for the distance:

distance (d) = velocity (v) * time (t) or simply, d = vt

Since it takes about twice the time the echo go back to the sensor, we need to divide the resulting time to 2. Here is the formula to do this:

distance (d) = velocity (v) * time (t) / 2 or simply, d = v(t/2)

By the way, below is the actual mathematical computations I did to calculate the distance of an object from the sensor:



I placed an object about 10 cm from the distance sensor. I compiled my code and uploaded the sketch to my Arduino board. I opened the serial monitor to see what values are being returned to the sensor. For the test that I did, the sensor returned 630 (in microseconds). Since we know that the speed (velocity) of sound is 343 meters / second, we can now calculate for the distance:

d = ?
t = 630 microseconds
v = 343 meters / second

Re-arranging the formula v = d / t to solve for d, we have d = vt. So we have the following:

d = (343 meters / second) x (630 microsecond)

We need to convert microseconds to seconds, by dividing it with 1000000. We get 0.00063 seconds.

d = (343 meters / second) x (0.00063 second)

We cancel out seconds, and we are left with meters. Now, do the multiplication:

d = 0.21609 meters.

Now, this distance also covered the time it took the echo to reach the sensor. Only half of this distance is the actual distance of the object from the sensor. Now, we divide the distance by 2 to get the actual distance from the sensor to the object:

d = 0.21609 meters / 2 = 0.108145 meters or 10.81 cm

You will see that the equation above is put to use. Enough of the class, and let’s get started!

Prepare these components. Please see image below:

Distance Sensor (the one I am using is the HC-SR04 model)
Arduino UNO R3 (or any Arduino Board)
Optional Protoshield of UNO with mini breadboard
Some jumper wires (at least 4 of these)

Distance Sensor - 1

STEP 1. Attach the Distance Sensor (HC-SR04) to the mini breadboard as shown below:
Distance Sensor - 2

STEP 2. The sensor has 4 pins: GND (Ground), Echo, Trig (Trigger) and VCC (+5v).Although the colors of the jumper wires do not matter, I will still use colors to determine which jumper wire goes with the sensor pins and Arduino pins. Attach the black jumper wire to the GND pin of the sensor, attached the yellow jumper wire to the ECHO pin of the sensor, attach the blue jumper wire to the TRIG pin of the sensor, and the red wire to the VCC pin of the sensor. Please refer to the image below:
Distance Sensor - 3

STEP 3. Attach the other ends of the jumper wires to the Arduino pins. The other end of the black jumper wire to the Arduino GND, the other end of yellow jumper wire to digital pin 12 of Arduino board, the other end of the blue jumper wire to digital pin 13 of the Arduino board and the other end of the red jumper wire to 5V of the Arduino board. Refer to the image below:
Distance Sensor - 4

The schematic diagram I made with Fritzing for the whole setup is shown below:
HC-SR04 Distance Sensor

Now, the code part. Remember the “class” on velocity = distance / time? You will be able to see that in the code. Please see the screenshot of the code from the Arduino IDE:

In our sample code above,

v = soundSpeed (343 meters per second)
t = duration (since it takes twice the time for the echo to go back to the sensor, we divide it by two)
d = vt or soundSpeed * (duration / 2)

Now, we’re done! Compile and upload the sketch to Arduino board, and open the Serial monitor. Move objects to and fro in front of the sensor. You should see varying values. Code above shows the conversion of distance from meters to centimeter.

I hope this tutorial helps! Thank!


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s