Ultrasonic Sensor HC-SR04

Ultrasonic sensor is a sensor that works on principle similar to radar or sonar. It generates high frequency sound and calculate the time interval between the sending of signal and the receiving of echo. Therefore, ultrasonic sensor can be used to measure distance. HC-SR04 ultrasonic sensor consists of a transmitter, a receiver and a control module.

Front view of HC-SR04
Back view of HC-SR04

The datasheet of HC-SR04 can be obtained here, HC-SR04.

The working principle of a ultrasonic sensor is as below:

1. Send at least 10us of HIGH signal to trigger pin.

2. Detect the pulse width of echo signal sent back.

3. Apply some calculation to get the distance.

I have sketch the working principle of ultrasonic sensor to give a better understanding.

Ultrasonic Sensor working principle sketch

The pulse width of echo is proportional to the distance travelled.

We know that the speed of sound travelling in air is 340 m/s.

When the transmitter send a signal out, the receiver receives back the signal sent.

Now that the distance travel by the signal is double of the distance of interest. So we divide it by two.

By using basic physics principle, velocity = distance/time.

Therefore, the distance travelled is the product of velocity and time.


The set up of ultrasonic sensor on breadboard.



There are library made available for ultrasonic sensor. You can get it from here.

If you would like to know more on how to use ultrasonic sensor with library, you can visit here.

However, I had tried out the ultrasonic sensor based on the working principle I had mentioned above, and it works perfectly. 😀

#include <LiquidCrystal.h>

#define trigger 2
#define echo 3

LiquidCrystal lcd(5,6,7,8,9,10);

float time=0,distance=0;

void setup()

void loop()

That’s it, hope that all the explanation can help you understand more about ultrasonic sensor. If you have any question, feel free to drop me a comment.

16 thoughts on “Ultrasonic Sensor HC-SR04

  1. Hi, thanks, that was a great article it helped me a lot.. I plan to write my own version of this post soon. Suggestion: use some syntax highlighter plugin to display code instead of using images..

      1. I’m sorry, I have not worked with wordpress.com. I use self hosted WordPress.org domain. There to add code to domain we have to add plugins like what was suggested by @vick. It seems wordpress.com domains have this feature inherently and could be used with a shortcode.

         your code here 

        to add code with line numbers. Here is a sample.

        int main()
        int a;
        unsigned char a;
        printf("Hello world!\n");
        return 0;
  2. I think you missed out some explaantion. Is it true that the pulse/ time that it returns is in the unit of microseconds ?

    Thanks 🙂

Leave a Reply

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

WordPress.com Logo

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

Google photo

You are commenting using your Google 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 )

Connecting to %s