How does the ultrasonic distance meter sensor work?

These sensors are useful when measuring distance and detecting obstacles thanks to ultrasound. This sensor is ideal for robots, cars and automatons so that they do not collide with obstacles. We explain how it works and how to use it properly.

  1. What is an ultrasound?
  2. Hardware description of the HC-SR04 sensor
  4. Operation of the ultrasonic sensor
  5. What is the ultrasonic sensor used for?
  6. Connection and operation
  7. Code
  8. What is an ultrasound?

Before starting to explain the ultrasound sensor, let’s explain what these are. As the name implies, they are “very loud” sounds, so much so that the human ear cannot hear them, they are inaudible. We are talking about sounds whose frequency ranges from 20KHz onwards.

Human ears can hear sound waves that vibrate in the range of about 20 times per second (a deep thud) to about 20,000 times per second (a high-pitched hiss). However, ultrasound has a frequency of more than 20,000 Hz and is therefore inaudible to humans.

  1. Hardware description of the HC-SR04 sensor

The HC-SR04 Ultrasonic Sensor uses sonar to determine the distance to an object like bats do. Offers excellent non-contact range detection with high precision and stable readings in an easy-to-use package.

In essence, the HC-SR04 Ultrasonic Distance Sensor consists of two ultrasonic transducers.

On the front of the ultrasonic rangefinder there are two metal cylinders. These are transducers. Transducers convert mechanical forces into electrical signals. One acts as a transmitter, which converts the electrical signal into pulses of 40 KHz ultrasonic sound. The receiver listens to the transmitted pulses. If it receives them, it produces an output pulse whose width can be used to determine the distance traveled by the pulse. Simple as that!


The sensor is small, easy to use in any robotics project, and offers excellent non-contact range detection between 2 cm and 400 cm, and with an accuracy of 3 mm. Since it runs on 5 volts, it can be connected directly to an Arduino or any other 5V logic microcontroller.

Among some features that make this sensor special, the following can be highlighted:

  • Detects transparent objects: Since ultrasonic waves can reflect off a glass or liquid surface, and return to the head, even transparent objects can be detected.
  • Objects of complex shape detectable: Presence detection is stable, even for objects such as mesh trays or springs.
  • Fog and Dirt Resistant: Detection is not affected by accumulation of dust or dirt.
  • Black colors or sunlight: Its operation is not affected by sunlight or black material such as Sharp rangefinders (although acoustically soft materials such as fabric can be difficult to detect)


It is important to know that ultrasonic sensors may not detect some objects. This may be because some objects are shaped or positioned in such a way that the sound wave bounces off the object, but is deflected from the ultrasonic sensor. It is also possible that the object is too small to reflect enough sound wave back to the sensor to be detected. Other objects can absorb the sound wave all together (cloth, carpets, etc.), which means that there is no way for the sensor to detect them accurately. These sensors work only where we have the presence of air (they cannot work in a vacuum, they need a means of propagation)

These factors are important to consider when designing and programming a robot using this ultrasonic sensor.

Full Specifications:

Operating voltage DC 5V
Operating current 15 mA
Operating frequency 40 kHz
Max range 4m
Minimum range 2cm
Range precision 3 mm
Angle of measurement 15 degree
Trigger input signal 10 µS TTL pulse
Dimension 45 x 20 x 15 mm

This sensor consists of 4 pins, 2 typical for power and another two for each ultrasonic transducer mentioned.


VCC : It is the power supply for the HC-SR04 ultrasonic distance sensor to which we connect the 5V pin on the Arduino.

Trig (trigger) : The pulse length is proportional to the time it took for the transmitted signal to be detected

Echo : The Echo pin produces a pulse when the reflected signal is received. The length of the pulse is proportional to the time it took for the transmitted signal to be detected.

GND : Must be connected to Arduino ground.

  1. Operation of the ultrasonic sensor

Once we have seen its characteristics and pins, we will see how it really works. How we manage to convert the data into the distance value with the sender and receiver.

To begin, a pulse of at least 10 µS (10 microseconds) in duration is applied to the Trigger pin. In response to that, the sensor transmits an eight-pulse sonic burst at 40KHz. This 8-pulse pattern makes the device’s “ultrasonic signature” unique, allowing the receiver to differentiate the transmitted pattern from ambient ultrasonic noise.

The eight ultrasonic pulses travel through the air away from the transmitter. Meanwhile, the echo pin goes HIGH to begin forming the beginning of the echo return signal.

In case those pulses are not reflected, the echo signal will expire after 38 mS (38 milliseconds) and return to low level. Therefore, a pulse of 38 mS indicates that there is no obstruction within the range of the sensor, as this is how long it has been in the HIGH state.

[Photo: ]

If those pulses are reflected, the Echo pin goes low as soon as the signal is received. This produces a pulse whose width varies from 150 µS to 25 mS, depending on the time it took to receive the signal.


[Photo: ]

In the following image you can see the various pulses, from the “trigger” of the Trigger and then the 8 pulses from the emitter. The wave will be transmitted through the air bouncing off any object within 4 meters and within a 30º angle.

Right after the Echo goes high, it does not go low until the Receiver has received the 8 pulses of the reflected wave.

The width of the pulse (the duration in time) received is then used to calculate the distance to the reflected object. This can be solved using a simple distance-velocity-time equation, a simple equation. In case you forgot, an easy way to remember the equations for distance, speed, and time is to put the letters in a triangle.

As always, it is best explained by an example. Suppose we have an object in front of the sensor at an unknown distance and we receive a pulse 500 µS wide at the Echo pin. Now let’s calculate how far the object is from the sensor. To do this, we will use the following equation.

Distance = Speed ​​x Time

We have the value of Time, that is, 500 µs and we know the speed. Speed? Sure, sound travels at the speed of sound, a more than known number: 340 m / s. We have to convert the speed of sound to cm / µs to calculate the distance with the same units. A quick Google search for “speed of sound in centimeters per microsecond” will say it is 0.034 cm / µs. You could do the math, but looking for them is easier. Anyway, with this information we can already calculate the distance.

Distance = 0.034 cm / µs x 500µs

But it’s not all done! Remember that the pulse indicates the time it took for the signal to be sent and reflected, so to obtain the distance, you will actually have to divide the result by two

Distance = (0.034 cm / µs x 500 µs) / 2

Distance = 8.5 cm

So now we know that the object is 8.5 centimeters from the sensor. But do not worry, these calculations do not have to be done every time nor do you have to do them. For something is an electronic sensor, to program it with your development board.

  1. What is the ultrasonic sensor used for?

The simple technology used by this sensor makes it very useful in the automation industry. Ultrasonic sensors can be used in many types of manufacturing with results that stand out from other options.

Platform security

The construction industry uses both cranes and aerial movement platforms. In this area, ultrasonic sensors help maintain efficient traffic between platforms, maintaining safety on construction sites and work environments.

Bulk production

A common industrial sector demanding precise measurements for their production activities. The ultrasonic sensor is used in these cases to regulate the filling of industrial containers and thus avoid their overflow, both with liquids and solids.

Car detection

Perhaps this is the most common and clearest example of the use of ultrasonic sensors, not only for the industrial sector. Public parking lots use boom systems for vehicle entry and exit. Ultrasonic sensors are responsible for preventing a feather from descending on a car while it is underneath.

Ultrasonic sensor operation is ideal for many types of industries, and there are many more examples besides those listed. This technology is undoubtedly has the ideal characteristics to ensure the quality, safety and flexibility of large-scale industrial projects.

Educational robotics

But this sensor is very useful especially in educational robotics, to learn to program both simple and more complex robots. It serves both to learn the theory, which we have explained, and to make more visual examples and with direct application. We can think of the typical obstacle detection robot, which will turn or change its course when detecting an object so as not to collide.

You can find this working example in our next tutorial:

Example of operation of an obstacle detector 2-wheel robot car with the L298N controller and ultrasound

  1. Connection and operation

The connection of this sensor is very simple, it can be powered directly from the development board at 5V. In our case we will use the Arduino UNO board and we will show the code to execute it in your IDE, but you can use different ones, such as Raspberry Pi or micro: bit.

The module connects to the power of the Arduino so that Vcc goes to 5V and GND to any of the GNDs of the UNO. We will connect the Trig pin to digital pin 9 and the Echo to 8, as shown in the following diagram.

  1. Code

For the programming code, we must first define the Trigger Pin and the Echo Pin that will be connected to the Arduino board.

In this project, EchoPin is assigned to pin D8 and TriggerPin to D9. Then we define the variables for the distance with “cm” (int) and the duration with “duration” (long).

Open a new sketch and paste the following code:

const int EchoPin = 8;

const int TriggerPin = 9;


void setup () {

   Serial.begin (9600);

   pinMode (TriggerPin, OUTPUT);

   pinMode (EchoPin, INPUT);



void loop () {

   int cm = ping (TriggerPin, EchoPin);

/ * Monitoring in centimeters by serial monitor * /

   Serial.print (“Measured distance:”);

   Serial.println (cm);

   delay (1000);


// Calculations for distance

int ping (int TriggerPin, int EchoPin) {

   long duration, distanceCm;


   digitalWrite (TriggerPin, LOW); // to generate a clean pulse we set LOW 4us

   delayMicroseconds (4);

   digitalWrite (TriggerPin, HIGH); // we generate Trigger (trigger) of 10us

   delayMicroseconds (10);

   digitalWrite (TriggerPin, LOW);


   duration = pulseIn (EchoPin, HIGH); // we measure the time between pulses, in microseconds


   distanceCm = duration * 10/292/2; // we convert to distance, in cm

   return distanceCm;


Now, once the code is uploaded, if we open the Serial Monitor from Tools, we can see the distance at which we position the objects in front of the sensor. You can try zooming in and out of your hand or an object to see how the distance varies.

On the other hand, an interesting library that you can use with this module is NewPing.h

You can download this library by  clicking here.

An example of code using this library can be the following. Open a new sketch and copy the following code.

// This uses Serial Monitor to display Range Finder distance readings

// Include NewPing Library

#include “NewPing.h”

// Hook up HC-SR04 with Trig to Arduino Pin 9, Echo to Arduino pin 10

#define TRIGGER_PIN 9

#define ECHO_PIN 8

// Maximum distance we want to ping for (in centimeters).

#define MAX_DISTANCE 400

// NewPing setup of pins and maximum distance.


float duration, distance;

void setup () 


 Serial.begin (9600);


void loop () 


 // Send ping, get distance in cm

 distance = sonar.ping_cm ();


If you open the Serial Monitor you will see something like this:


Micro: bit ultrasonic sensor programming

If you want to use the little BBC board, we leave you an example code for its own MakeCode editor. Find the following blocks and configure it as indicated below.


The connection, as can be seen in the code, is the following:

Ultrasonic sensor Micro: bit
Trigger P0
Echo P1


by Abdullah Sam
I’m a teacher, researcher and writer. I write about study subjects to improve the learning of college and university students. I write top Quality study notes Mostly, Tech, Games, Education, And Solutions/Tips and Tricks. I am a person who helps students to acquire knowledge, competence or virtue.

Leave a Comment