HC-SR04 and Arduino – wiring diagram

Measuring distances in the 21st century with a ruler and a pencil is considered outdated. Nowadays, most modern methods are measurements using infrared rays and ultrasound.

Measuring distances in the 21st century with a ruler and a pencil is considered outdated. Nowadays, most modern methods are measurements using infrared rays and ultrasound. With the Arduino, you can assemble a mini device for this task.

For these purposes, there are many different modules that are connected to your controller, but in today’s article, I will discuss only one of them, the HC-SR04 module, which is popular, which is based on the principle of the sonar operation.

To implement the project from this article, we need the following components:

  • Arduino Uno R3
  • Ultrasonic Range Finder HC-SR04
  • Breadboard
  • Wires


HC-SR04 and Arduino – wiring diagram
The module radiates sound to the object being studied and then accepts its return back. The time that the sound was on the way is multiplied by the speed of sound, and then the distance of the object is displayed.

There are also infrared modules – they are also capable of measuring the distance. However, the ultrasound module has some advantages. For example, its measurements are not affected by various sources of light or color or obstacles.

One of the few reasons for determining the error of the module lies in the temperature changes in the medium of the object being measured, however, they are hardly noticeable (we recommend to take measurements at temperatures above zero, and even then, this is no longer due to the error, but requirements for the performance of this module).

The HC-SR04 module has a fairly low cost among its counterparts in the application environment. It is able to measure a distance in the range from 2 to 450 cm. It works on a voltage of 5 volts, the viewing angle is up to 15 degrees.


An ultrasonic sensor is connected to the Arduino using four pins: two power pins (5-volt pins and gnd – ground) and two pins of the TRIG (T) and ECHO (R) output signals.

The connection diagram is shown in the picture on the right:

Now that we know the names of the outputs of the signals from the module, we can make out the principle of the module. The distance is measured, as mentioned earlier, by measuring the time of passage of the sound wave to the object and back.

The module has two so-called “eyes” (T and R, which are mutually connected with the outputs of Trig and Echo). First, a pulse is sent to the Trig pin, which is converted into 8 pulses with a frequency of 40 kHz and sent to the object under study through the “eye” T. Then the signal is reflected from the object and received by the “eye” R and fed to the Echo pin.

Everything is very simple: from one “eye” the signal goes forward, it is reflected from the object, it is caught by the second ”eye”, the total sound signal transmission is measured, and the distance is calculated.

After connecting the module to the board, connect the latest one to the computer and download the following program code via the development environment:

#define Trig 8
#define Echo 9

void setup()
pinMode (Trig, OUTPUT);
pinMode (Echo, INPUT); 
Serial.begin (9600); 
unsigned int impulseTime=0;
unsigned int distance_sm=0;

void loop()
digitalWrite (Trig, HIGH); 
delayMicroseconds (10);
digitalWrite (Trig, LOW); 
impulseTime=pulseIn (Echo, HIGH); 
distance_sm =impulseTime/58; 
Serial.println( distance_sm); 
delay (200);

After downloading this sketch to the port monitor, data on the distance to the object will be displayed.

Answers to frequently asked questions beginners:
Why are some strange characters displayed in the port monitor instead of numbers?

It is necessary to set the speed of the port monorair to the same as the speed of the serial connection for data exchange. In the sketch, this value is written in the line

Serial.begin (9600);
– 9600 baud (or bits per second). In the port monitor, you can change it in the lower right corner.

Why in the cycle divide the length by 58? Where did this number come from?

As you know, the speed of sound is 34 cm/ms or 0.034 m / ms, and the distance is equal to the product of time and speed. Means

s = time * 0.034 m / mks

s = time * 1/29 measured distances (if we convert to an ordinary fraction).
Since the signal went through two measured distances, it is necessary to divide it by two and from here we get that

s = time * 1/58
On this, our article came to an end. I hope it was interesting for you. All successful compilation and wait for new lessons and articles!

Share on facebook
Share on google
Share on twitter
Share on linkedin
Share on pinterest