HC-SR04 Tutorial for Arduino

The HC-SR04 is an excellent low-cost ultrasonic sensor that works really well with Arduino micro-controllers. The quality of these sensors does vary depending on where you buy them from, so we recommend sticking with reputable suppliers. We have some for sale over in our store.

hcsr04

These sensors are very easy to use. First connect the VCC and GND pins to the Arduino’s +5V and GND pins. Next connect the Trigger and Echo pins to two digital pins on the Arduino, perhaps pins 2 and 3 for example.

Setting the Trigger Pin to HIGH for 10 microseconds will activate a measurement reading and then the pulseIn() function can be called on the Echo Pin to determine the distance. The length of the incoming pulse is proportional to the distance measured.

Here is some simple example code to demonstrate the usage:

pinMode(triggerPin, OUTPUT);
pinMode(echoPin, INPUT);
digitalWrite(triggerPin, HIGH);
delayMicroseconds(10);
digitalWrite(triggerPin, LOW);
int distance = pulseIn(echoPin, HIGH) / 2;

This code returns the raw measurement of the time for the sound to reach the object. Sound travels at approximately 340 meters per second at sea level. Therefore, the length of time it takes sound to travel 1 meter is 1/340 which is 0.0029 seconds. To calculate the length of time for sound to travel 1 centimeter, we divide this by 100 to get 0.000029 seconds, or 29 microseconds.

So to convert the reading from the sensor to centimeters, we divide by 29 (or to convert to inches, divide by 74, since 1 inch = 2.54 cm).

For example:

int distance_cm = distance / 29;
int distance_inches = distance / 74;

For accurate readings, it is recommended to leave some time in between measurements, as the sound waves could be echoing around for a while. A good rule of thumb is to sleep for 50 milliseconds between readings but you may want to adjust this based on your requirements.

Another good tip is to specify a timeout when calling pulseIn() to avoid waiting a long time for a signal that never arrives. If you are only interested in measuring distances up to 100 cm for instance then you could specify a timeout of 5,800 microseconds (100 * 29 * 2).

int distance = pulseIn(echoPin, HIGH, 5800) / 2;

As good as these sensors are, they cannot defy the laws of physics and results will vary greatly depending on the object being measured and the angle between the sensor and the object.

Some smooth objects (like glass) at a 45 degree angle from the sensor may not return an accurate signal, and even worse could bounce the signal of some other object, giving a false reading. Objects made from less smooth materials will usually return some signal.

For the best results, it is recommended to have multiple sensors at different angles around the robot. We sell packs of these sensors at a low cost in our store, making it very affordable to have multiple sensors.

Also, it is good to have a backup plan if obstacle avoidance fails. It is always a good idea to be able to detect if the robot has hit a wall, perhaps using physical sensors, such as microswitches.

[product sku=”HCSR04″]

7 thoughts on “HC-SR04 Tutorial for Arduino

    1. andygrove Post author

      Hi Raj,

      pulseIn(echoPin, HIGH) will return the time it takes for the sound to reach the object and be returned, which is 2 x the distance, so we divide by 2.

      Then to convert the timing into a familiar metric we divide by 29 to get centimeters, or we can divide by 74 to get inches.

      I hope that helps. I’ll see if I can explain this better in the article.

      Thanks for your feedback!

      Reply
      1. Nick

        Thanks this is very helpful. Can you tell me why we use 29 for centimeters and 74 for inches? I get that it is because there are 2.54 centimeters in an inch, but where does the 29 come from originally? Thank you for the tutorial.

        Reply
  1. Angel

    Hello sir, I would like to ask some tips. I’m 18 year old Computer Engineering student and I am planning to make a Flood Monitoring System that transmits data over the internet to our server. I have Arduino Uno, 3 pcs Ultrasonic Sensor and Ethernet Shield. Do you think the things that I have bought is enough? Sorry sir. I’m just new with this. Thank you so much :)

    Reply
  2. Valter Gonçalves

    why reason divide the distance variable by 29, or to inches, divide by 74.
    which calculations for these values?
    Thanks for the good project.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *