How To Connect HC-SR04 Ultrasonic Rangefinder To Arduino

Table of contents:

How To Connect HC-SR04 Ultrasonic Rangefinder To Arduino
How To Connect HC-SR04 Ultrasonic Rangefinder To Arduino

Video: How To Connect HC-SR04 Ultrasonic Rangefinder To Arduino

Video: How To Connect HC-SR04 Ultrasonic Rangefinder To Arduino
Video: How To: Use an Arduino with an Ultrasonic Sensor (HC-SR04) 2024, May
Anonim

In this article, we will connect the HC-SR04 ultrasonic rangefinder-sonar to the Arduino.

Ultrasonic sensor HC-SR04
Ultrasonic sensor HC-SR04

Necessary

  • - Arduino;
  • - ultrasonic sensor HC-SR04;
  • - connecting wires.

Instructions

Step 1

The action of the HC-SR04 ultrasonic rangefinder is based on the principle of echolocation. It emits sound impulses into space and receives a signal reflected from an obstacle. The distance to the object is determined by the propagation time of the sound wave to the obstacle and back.

The sound wave is triggered by applying a positive pulse of at least 10 microseconds to the TRIG leg of the rangefinder. As soon as the pulse ends, the rangefinder emits a burst of sound pulses with a frequency of 40 kHz into the space in front of it. At the same time, the algorithm for determining the delay time of the reflected signal is launched, and a logical unit appears on the ECHO leg of the rangefinder. As soon as the sensor picks up the reflected signal, a logic zero appears on the ECHO pin. The duration of this signal ("Echo delay" in the figure) determines the distance to the object.

Distance measurement range of HC-SR04 rangefinder - up to 4 meters with a resolution of 0.3 cm. Observation angle - 30 degrees, effective angle - 15 degrees. Consumption current in standby mode is 2 mA, during operation - 15 mA.

The principle of operation of the ultrasonic rangefinder HC-SR04
The principle of operation of the ultrasonic rangefinder HC-SR04

Step 2

The power supply of the ultrasonic rangefinder is carried out with a voltage of +5 V. The other two pins are connected to any digital ports of the Arduino, we will connect to 11 and 12.

Connecting HC-SR04 Ultrasonic Rangefinder to Arduino
Connecting HC-SR04 Ultrasonic Rangefinder to Arduino

Step 3

Now we will write a sketch that determines the distance to the obstacle and outputs it to the serial port. First, we set the numbers of the TRIG and ECHO pins - these are pins 12 and 11. Then we declare the trigger as an output and echo as an input. We initialize the serial port at 9600 baud. At each repetition of the loop (), we read the distance and output it to the port.

The getEchoTiming () function generates a trigger pulse. It just creates a current of 10 microseconds pulse, which is a trigger for the start of radiation by the rangefinder of a sound packet into space. Then she remembers the time from the beginning of the transmission of the sound wave to the arrival of the echo.

The getDistance () function calculates the distance to the object. From the school physics course, we remember that distance is equal to speed multiplied by time: S = V * t. The speed of sound in the air is 340 m / s, the time in microseconds we know is "duratuion". To get the time in seconds, divide by 1,000,000. Since the sound travels twice the distance - to the object and back - you need to divide the distance in half. So it turns out that the distance to the object S = 34000 cm / sec * duration / 1.000.000 sec / 2 = 1.7 cm / sec / 100, which we wrote in the sketch. The microcontroller performs multiplication faster than division, so I replaced "/ 100" with the equivalent "* 0, 01".

Sketch for working with ultrasonic sonar HC-SR04
Sketch for working with ultrasonic sonar HC-SR04

Step 4

Also, many libraries have been written to work with an ultrasonic rangefinder. For example, this one: https://robocraft.ru/files/sensors/Ultrasonic/HC-SR04/ultrasonic-HC-SR04.zip. The library is installed in a standard way: download, unzip to the libraries directory, which is located in the folder with the Arduino IDE. After that, the library can be used.

Having installed the library, let's write a new sketch. The result of its work is the same - the serial port monitor displays the distance to the object in centimeters. If you write float dist_cm = ultrasonic. Ranging (INC); in the sketch, then the distance will be displayed in inches.

Ultrasonic sonar sketch using library
Ultrasonic sonar sketch using library

Step 5

So, we connected the HC-SR04 ultrasonic rangefinder to the Arduino and received data from it in two different ways: using a special library and without.

The advantage of using the library is that the amount of code is significantly reduced and the readability of the program is improved, you do not have to delve into the intricacies of the device and you can immediately use it. But this is also the disadvantage: you understand less well how the device works and what processes take place in it. In any case, which method to use is up to you.

Recommended: