Home » Arduino » How to measure distances with the HC-SR04 ultrasonic with Arduino sensor?

Login with your Social Account

How to measure distances with the HC-SR04 ultrasonic with Arduino sensor?

  • 1
  • 20

Discover how to measure distances with the HC-SR04 ultrasonic with Arduino sensor. Examples of connection and programming (both graphic and textual).

Here, we describe the steps needed to measure distances with the HC-SR04 ultrasonic with Arduino sensor. Several programming examples, including examples of graphical programming will be explained.

How does it work?

The HC-SR04 ultrasonic sensor emits a train of ultrasonic waves with a membrane and receives the echo of these waves, when they bounce off objects, with another membrane. The operating principle is to the time of flight (TOF), which corresponds to the time that requires the ultrasonic wave to travel by air since it is generated until it is received. The distance travelled by the wave is indeed twice the distance of the ultrasound sensor to the object (is the distance to go and to return back).

The sound speed in air is approximately 343.59 m/s, this means each 29uS the ultrasound wave travels a centimeter (58uS/cm to the object if we consider wave between the departure and the return time).

The HC-SR04 sensor has (in addition to power supply VCC PIN to + 5V and 0V / GND mass) two pins controlling the trigger (TRIG) and the echo received by wave (ECHO). To generate a trigger, the TRIG should be active during at least 10uS (set to + 5V) and then set to 0V again. Then we must wait until the ECHO signal is active and measure the elapsed time between the trigger and the echo.

To calculate the distance of the object in centimeters, we divide time into uS by 58.

Properties of the sensor:

  • Ultrasonic wave has an angle detection in which theoretically is sensitive and therefore all waves coming at an angle within the main lobe will be received is approximately 15 degrees.
  • The maximum sensor distance is 400cm.
  • Sensors can be affected by the phenomenon of cross-talking: a sensor wave is received by another sensor or a same sensor can get his own prior shot wave if timeouts between trigger and trigger are too short (recommended between trip times are approximately 50ms or 60ms). If we have multiple ultrasonic sensors and we want to minimize the effect cross-talking, it makes sense to generate a sequence shots, even so, it will be very difficult to distinguish between the echos received by a another robot or sensors, so you must assume that the measurement might contain errors.
  • In theory the wave bounces on the mirrors in a specular manner and therefore if the incident angle is too high it won’t see it. The truth is that since the majority of surfaces are not completely smooth or have edges or other internal elements that can reflect the wave, sensor is able to detect, in the vast majority of cases, objects that have an angle of incidence greater than sensor detection angle.
  • These ultrasonic sensors are designed to operate in the air, not in the water.
  • Glass and mirrors are theoretically detected, but reflections are specular.
  • The color of objects do not affect to this sensor.
  • Other variables such as temperature and pressure tend to have some influence, but are often ignored in most of the cases.


First describes how to connect the cables to arduino. In this case, it is recommended to use separate cables to connect arduino IO expansion board, breadboard or directly to the arduino. For simplicity, here direct connections to the arduino, are displayed if you use a breadboard or IO expansion plate connections must be equivalent.

A single sensor reading:

TRIG pins and ECHO can be connected to any Digital IO of Arduino pin. Pins VCC and GND can lead to any of the pins of the breadboard.


In the case of using IO expansion card connections could be as:sr04_nano_io_bb


The NewPing library that allows you to control the firing of the ultrasonic sensor with Arduino IDE. You can download the sample code here:

With ArduBlock we can use the following sample code:


You can download here the example with Ardublock code:

Reading multiple ultrasounds:

Sequential read:

If we are going to use several ultrasounds at the same time, the most common solution is to connect a TRIG signal and ECHO for each ultrasound, we allow us to create independent shots (to create a sequence). In this case, the sequential reading of ultrasound may be a drawback for the control of the robot, since each ultrasound requires between 50-60ms between firing (although it can actually be adjusted even the 29ms), which might be too slow when considering several sensors. Advantage is that programming is simplified because it is a sequential reading.


In the case of using the IO expansion card, the connections would be:


The following is an example with three sensors based on the NewPing library with a time of sequencing of 33ms:

With ArduBlock we can use the following sample code:


You can download here the example with Ardublock code:

Reading in parallel with shared TRIG signal:

If there are not enough pins in our arduino board (each ultrasonic sensor consumes two pins or the time to perform a sequential reading is too slow, we can consider a compromise solution in which the TRIG signal is shared by all sensors and signal ECHO is independent, so all senors are triggered at the same time and echos are receveid depending on objects distances. This is a non-standard case and therefore it should not be taken as a reference for the typical cases to use in your robot desings. In addition, you must consider that cross-talking phenomena might appear more frequently, which will generate a noise difficult to remove. In that case, it is convenient to orientate sensors with at least 90° of rotation among them.


The following is a modification of the NewPing library that allows you to simultaneously fire several ultrasounds.

 With ArduBlock we can use the following example (note that the code here is more complicated given the impossibility of firing multiple form three ultrasounds with modules of ultrasound that incorporates Ardublock):

You can download here the example with Ardublock code:

%d bloggers like this: