DIY ultrasonic distance sensor. Ultrasonic range finder hc-sr04 - Measuring technology - Tools

Arduino ultrasonic distance sensors are very popular in robotics projects due to their relative simplicity, sufficient accuracy and availability. They can be used as devices that help to avoid obstacles, obtain the dimensions of objects, simulate a map of the room, and signal the approach or removal of objects. One of the common options for such a device is a distance sensor, the design of which includes an ultrasonic rangefinder HC SR04. In this article we will get acquainted with the principle of operation of a distance sensor, consider several options for connecting to Arduino boards, an interaction diagram and example sketches.

The ability of an ultrasonic sensor to determine the distance to an object is based on the principle of sonar - by sending a beam of ultrasound and receiving its reflection with a delay, the device determines the presence of objects and the distance to them. Ultrasonic signals generated by the receiver, reflected from the obstacle, return to it after a certain period of time. It is this time interval that becomes a characteristic that helps determine the distance to the object.

Attention! Since the operating principle is based on ultrasound, such a sensor is not suitable for determining the distance to sound-absorbing objects. Objects with a flat, smooth surface are optimal for measuring.

Description of the HC SR04 sensor

The Arduino distance sensor is a non-contact type device, and provides highly accurate measurement and stability. Its measurement range ranges from 2 to 400 cm. Its operation is not significantly affected by electromagnetic radiation and solar energy. The module kit with HC SR04 arduino also includes a receiver and transmitter.

The ultrasonic rangefinder HC SR04 has the following technical parameters:

  • Supply voltage 5V;
  • The operating current parameter is 15 mA;
  • Current strength in passive state< 2 мА;
  • Viewing angle – 15°;
  • Touch resolution – 0.3 cm;
  • Measuring angle – 30°;
  • Pulse width – 10 -6 s.

The sensor is equipped with four leads (standard 2.54 mm):

  • Positive type power contact – +5V;
  • Trig (T) – input signal output;
  • Echo (R) – output signal output;
  • GND – “Ground” pin.

Where to buy SR04 module for Arduino

The distance sensor is a fairly common component and can be easily found in online stores. The cheapest options (from 40-60 rubles per piece), traditionally on the well-known website.

HC-SR04 distance sensor module for Arduino Another option for the HC-SR04 ultrasonic sensor from a reliable supplier
Proximity sensors SR05 Ultrasonic HC-SR05 (improved performance) Module HC-SR05 HY-SRF05 for UNO R3 MEGA2560 DUE from a reliable supplier

Scheme of interaction with Arduino

To obtain data, you must perform the following sequence of actions:

  • Apply a 10 microsecond pulse to the Trig output;
  • In the hc sr04 ultrasonic rangefinder connected to arduino, the signal will be converted into 8 pulses with a frequency of 40 kHz, which will be sent forward through the emitter;
  • When the pulses reach the obstacle, they will be reflected from it and will be received by the R receiver, which will provide an input signal at the Echo output;
  • On the controller side, the received signal should be converted into distance using formulas.

When dividing the pulse width by 58.2, we get data in centimeters, when dividing by 148, we get data in inches.

Connecting HC SR04 to Arduino

Connecting an ultrasonic distance sensor to an Arduino board is quite simple. The connection diagram is shown in the figure.

We connect the ground pin to the GND pin on the Arduino board, and connect the power output to 5V. We connect the Trig and Echo outputs to the arduino via digital pins. Connection option using a breadboard:

Library for working with HC SR04

To make it easier to work with the HC SR04 distance sensor on Arduino, you can use the NewPing library. It has no ping issues and adds some new features.

Features of the library include:

  • Ability to work with various ultrasonic sensors;
  • Can work with a distance sensor through just one pin;
  • No 1 second lag when no echo ping;
  • There is a built-in digital filter for simple error correction;
  • The most accurate distance calculation.

You can download the NewPing library

Accuracy of distance measurement with HC SR04 sensor

The accuracy of the sensor depends on several factors:

  • air temperature and humidity;
  • distance to the object;
  • location relative to the sensor (according to the radiation diagram);
  • quality of performance of sensor module elements.

The principle of operation of any ultrasonic sensor is based on the phenomenon of reflection of acoustic waves propagating in the air. But as you know from a physics course, the speed of sound propagation in the air depends on the properties of that air itself (primarily on temperature). The sensor, emitting waves and measuring the time until they return, has no idea in what medium they will propagate and takes a certain amount for calculations. average value. IN real conditions Due to the air temperature factor, HC-SR04 may have an error of 1 to 3-5 cm.

The distance to the object factor is important because... the probability of reflection from neighboring objects increases, and the signal itself attenuates with distance.

Also, to increase accuracy, you need to correctly direct the sensor: make sure that the object is within the cone of the radiation pattern. Simply put, the HC-SR04’s “eyes” should look directly at the subject.

To reduce errors and measurement uncertainty, the following actions are usually performed:

  • the values ​​are averaged (we measure several times, remove the spikes, then find the average);
  • using sensors (for example, ) the temperature is determined and correction factors are applied;
  • the sensor is mounted on a servomotor, with which we “turn the head”, moving the radiation pattern to the left or right.

Examples of using a distance sensor

Let's look at an example of a simple board project Arduino Uno and distance sensor HC SR04. In the sketch we will receive the value of the distance to objects and output them to the port monitor in the Arduino IDE. You can easily change the sketch and connection diagram so that the sensor signals when an object is approaching or moving away.

Connecting the sensor to Arduino

When writing the sketch, the following pinout option for connecting the sensor was used:

  • VCC: +5V
  • Trig – 12 pin
  • Echo – 11 pin
  • Ground (GND) – Ground (GND)

Sketch example

Let's start working with the sensor right away with a relatively complex version - without using external libraries.

In this sketch we perform the following sequence of actions:

  • With a short pulse (2-5 microseconds) we switch the distance sensor to echolocation mode, in which ultrasonic waves with a frequency of 40 KHz are sent into the surrounding space.
  • We wait for the sensor to analyze the reflected signals and determine the distance based on the delay.
  • We get the distance value. To do this, wait until the HC SR04 produces a pulse proportional to the distance at the ECHO input. We determine the pulse duration using the pulseIn function, which will return to us the time elapsed before the signal level changes (in our case, before the falling edge of the pulse appears).
  • Having received the time, we convert it into distance in centimeters by dividing the value by a constant (for the SR04 sensor this is 29.1 for the “there” signal, the same for the “back” signal, which will give a total of 58.2).

If the distance sensor does not read the signal, then the output signal conversion will never take the value of a short pulse - LOW. Since the delay time for some sensors varies depending on the manufacturer, it is recommended to set its value manually when using the above sketches (we do this at the beginning of the cycle).

If the distance is more than 3 meters, at which the HC SR04 begins to work poorly, it is better to set the delay time to more than 20 ms, i.e. 25 or 30 ms.

#define PIN_TRIG 12 #define PIN_ECHO 11 long duration, cm; void setup() ( // Initialize communication on the serial port Serial.begin (9600); // Define the inputs and outputs pinMode(PIN_TRIG, OUTPUT); pinMode(PIN_ECHO, INPUT); ) void loop() ( // First generate a short pulse lasting 2-5 microseconds digitalWrite(PIN_TRIG, LOW); delayMicroseconds(5); digitalWrite(PIN_TRIG, HIGH); // Setting the signal level high, wait about 10 microseconds. At this moment, the sensor will send signals with a frequency of 40 KHz. delayMicroseconds(10); digitalWrite(PIN_TRIG, LOW); // Delay time for the acoustic signal on the echolocator. duration = pulseIn(PIN_ECHO, HIGH); // Now it remains to convert the time into distance cm = (duration / 2) / 29.1; Serial. print("Distance to object: "); Serial.print(cm); Serial.println(" cm."); // Delay between measurements for correct operation of the sketch delay(250); )

Sketch using the NewPing library

Now let's look at a sketch using the NewPing library. The code will be significantly simplified, because all the previously described actions are hidden inside the library. All we need to do is create an object of the NewPing class, specifying the pins with which we connect the distance sensor and use the object’s methods. In our example, to get the distance in centimeters, we need to use ping_cm().

#include #define PIN_TRIG 12 #define PIN_ECHO 11 #define MAX_DISTANCE 200 // Constant to determine the maximum distance that we will consider correct. // Create an object whose methods we will then use to obtain the distance. // As parameters we pass the pin numbers to which the ECHO and TRIG outputs of the NewPing sonar sensor are connected (PIN_TRIG, PIN_ECHO, MAX_DISTANCE); void setup() ( // Initialize communication via the serial port at a speed of 9600 Serial.begin(9600); ) void loop() ( // Start delay required for correct operation. delay(50); // Get the value from the distance sensor and save it to the variable unsigned int distance = sonar.ping_cm(); // Print the distance in the port monitor Serial.print(distance); Serial.println("cm"); )

An example of connecting an ultrasonic rangefinder HC SR04 with one pin

Connecting the HC-SR04 to Arduino can be done using a single pin. This option is useful if you are working on a large project and do not have enough free pins. To connect, you just need to install a 2.2K resistor between the TRIG and ECHO pins and connect the TRIG pin to the Arduino.

#include #define PIN_PING 12 // The Arduino pin is connected to the trigger and echo pins on the distance sensor #define MAX_DISTANCE 200 // The maximum distance that we can control (400-500cm). NewPing sonar(PIN_PING, PIN_PING, MAX_DISTANCE); // Adjust pins and maximum distance void setup() ( Serial.begin(9600); // Opens a protocol with data and a transmission frequency of 115200 bps. ) void loop() ( delay(50); // Delay of 50 ms between generated waves. 29 ms is the minimum allowable value unsigned int distanceSm = sonar.ping(); // Create a signal, get its duration parameter in µs (uS). Serial.print("Ping: "); Serial.print(distanceSm / US_ROUNDTRIP_CM); // Convert the time parameter into a distance value and display the result (0 corresponds to going beyond the permissible limit) Serial.println("cm"); )

Brief conclusions

Ultrasonic distance sensors are versatile and accurate enough to be used for most hobbyist projects. The article discusses the extremely popular HC SR04 sensor, which easily connects to the Arduino board (for this you should immediately provide two free pins, but there is a connection option with one pin). There are several options for working with the sensor. free libraries(the article discusses only one of them, NewPing), but you can do without them - the algorithm for interacting with the internal controller of the sensor is quite simple, we showed it in this article.

Based on my own experience, the HC-SR04 sensor is accurate to within one centimeter at distances from 10 cm to 2 m. At shorter and longer distances, strong interference may occur, which is highly dependent on surrounding objects and method of use. But for the most part, the HC-SR04 did a great job.

Bruno Gavand

The project, which considers a simple and low-cost solution for an ultrasonic sensor for measuring distance, is based on the company's PIC16F877A microcontroller, but the source code can be adapted by users to other microcontrollers. The sensor can be built into custom designs and devices: presence detectors, robots, car parking systems, distance measuring devices, etc.

Distinctive features:

  • a small number of external components;
  • code size 200 Bytes;
  • working distance range: 30 cm - 200 cm;
  • measurement accuracy ±1 cm;
  • indication when measurement limits are exceeded.

As you know, the speed of sound in air is about 340 m/s. Thus, the principle of an ultrasonic sensor is to send an ultrasonic pulse with a frequency of 40 kHz and monitor the reflected signal (echo). Of course you won't hear any sound, but ultrasonic sensor able to detect the reflected impulse. Therefore, knowing the travel time of the pulse and the reflected ultrasonic signal, we can obtain the distance. Dividing by two, we get the distance from the ultrasonic sensor to the first obstacle from which the signal was reflected.

The device uses a piezoceramic ultrasonic emitter MA40B8S and a piezoceramic ultrasonic sensor MA40B8R open type. The main parameters are shown in the table below.

Device Purpose Frequency Direction,
hail
Capacity,
pF
Region
detection,
m
Input
voltage,
max, V
MA40B8S Emitter 40 kHz 50 (symmetrical) 2000 0.2 … 6 40
MA40B8R Sensor 40 kHz 50 (symmetrical) 2000 0.2 … 6

The company's debugging platform was used for testing.

However, the user can use any PIC microcontroller that has at least one ADC channel and one PWM channel.

Schematic diagram of an ultrasonic sensor

The emitter is controlled via transistor BD135. The 1N4007 diode is used to protect the transistor from reverse voltage. Thanks to the use of a transistor and a resonant circuit, which is formed by parallel connection of a 330 µH inductor L1 and a capacitor formed by the emitter itself, the supply voltage of the emitter will be about 20 V, which ensures a detection range of up to 200 cm. It is worth noting that the emitter can be controlled directly from the microcontroller output, however, in this case the distance range does not exceed 50 cm.

The sensor is connected directly to the ADC of the microcontroller (when using PIC16F877A - channel 1 of the ADC), a resistor connected in parallel with the sensor is necessary for impedance matching.

First you need to send an ultrasonic pulse. A 40 kHz signal is easily obtained using a hardware PWM microcontroller. The reflected signal from the sensor enters the ADC, the resolution of the ADC is 4 mV, which is quite sufficient for reading data from the sensor, and no additional components are needed.

External view of the ultrasonic sensor development board


This sensor is the simplest solution and therefore has several disadvantages: slight vibration of the ultrasonic receiver can lead to incorrect measurements. Since the sent pulse is not modulated or coded, extraneous sources of ultrasonic frequency can interfere with the measurement, all of which can lead to incorrect results(going beyond measurement limits).

Captions on the image:

ultrasonic burst - ultrasonic pulse;
mechanical echo (removed by software) - mechanical echo (removed by software);
ultrasonic wave reflected by remote object - ultrasonic wave reflected from a distant object.

Oscilloscope division value: horizontally - 1 ms/div, vertically - 5 mV/div.

Mechanical echo is eliminated in software by introducing a delay. The reflected wave, having an amplitude of about 40 mV, was received 9.5 ms after the sent pulse. Considering that the speed of sound is 340 m/s, we get:

0.0095 / 2×340 = 1.615 m.

In reality, it was the ceiling of the room at a distance of 172 cm from the sensor; the value of 170 cm was displayed on the LCD display installed on the debugging board.

Downloads

Source code for the project on the PIC16F877A microcontroller (mikroC compiler) -

On occasion, I bought myself an ultrasonic rangefinder HC-SR04. The device is a module with two piezo emitters, one of which serves as an emitter, and the second as a receiver of an ultrasonic wave; plus control electronics to control the emitter and receiver. For connection, the module has a 4-pin connector: two of which provide power (5 volts required), and two more for communication with the microcontroller.

The communication interface here is organized very simply: we apply a short pulse with a duration of 10-15 microseconds to the input and wait for a pulse at the output. As soon as the reflected wave reaches the receiver, the module itself calculates the distance and sends a high-level impulse up to 25 ms to the Echo leg. The length of the output pulse will be proportional to the distance to the obstacle from which the ultrasonic wave was reflected. All we have to do is catch this impulse, calculate its length and convert this value into distance.

Specifications:

  • Supply voltage: 5V
  • Quiescent current:< 2 мА
  • Effective viewing angle:< 15 °
  • Distance range: 2 cm - 500 cm
  • Resolution: 0.3 cm

The characteristics have been copied from the documentation for the module. In addition, the manufacturer provides a formula for calculating the distance depending on the pulse duration.

S=F/58 ; where S is the distance in centimeters, F is the pulse length in microseconds

As you can see, it is not even necessary to know the speed of sound.

For testing, I assembled the following circuit:

The module connects directly to the microcontroller. There is no need to install pull-up resistors; they are already on the module board.

And so, we need to catch just one impulse, and then calculate its length. At first I wanted to use one of the external interrupts of the microcontroller for this purpose, and the interruption had to occur both on the leading edge (transition from low to high state) and on the falling edge (from high to low). That is, you will have to change the configuration of this interrupt on the fly. Plus, you need to use one of the timers, which should measure the pulse length. Too complicated for a small signal fixing operation.. Bascom-AVR has a special command for this case Pulsein . Here is an example of how to catch a signal using this command:

Pulsein A, Pind, 5 , 1

Here in the variable A the value of the pulse length will be written in tens of microseconds taken from the leg Pind.5. The one at the end of the command says that you need to catch a high-level signal. If changed to 0, then the controller will catch a low level signal.

This command does not use interrupts or a hardware timer, but is capable of detecting the occurrence of a pulse and recording its length with a resolution of 10 μs. The command uses a 2-byte variable type to store the pulse length, so the maximum length of the received signal can be 655.35 ms. This is quite enough for the task at hand, but if necessary, you can edit the mcs.lib library file and change the maximum duration of the recorded pulse.

The full program listing is below

$regfile = "m8def.dat"

$crystal = 8000000

"configuration of connecting the display to the MK ports

Config Lcd = 16 * 2

Config Lcdpin= Pin, Rs= Portc. 5 , E= Portc. 4 , Db4= Portc. 3 , Db5= Portc. 2 , Db6= Portc. 1 , Db7= Portc. 0

Config Portd. 4 = Output "output for connecting the Trigger leg

TriggerAlias Portd. 4

Trigger= 0

Config Portd. 5 = Input "input for Echo impulse

Config Portd. 7 = Output "configuration for LED connection

LedAlias Portd. 7

Led= 0

Dim AAs Word "The signal length value is copied here

Dim SAs Single "variable for storing distance

Const K= 0 . 1725 "coefficient for converting pulse length to distance

Waitms 50

Cursor Off

Cls

Lcd "Sonar HC-SR04"

Locate 2 , 1

Lcd "website"

Led= 1

Waitms 100

Led= 0

Wait 3

Do

Trigger= 1 "we give an impulse to the leg Portd.4 with a duration of 15 μs

Waitus 15

Trigger= 0

Waitus 10

Pulsein A, Pind, 5 , 1 "we catch a high-level impulse on PinD.5

Rangefinder is a device for measuring the distance to an object. The rangefinder helps robots in different situations. A simple wheeled robot can use this device to detect obstacles. The flying drone uses a rangefinder to hover above the ground at a predetermined altitude. Using a rangefinder, you can even build a map of the room using a special SLAM algorithm.

1. Operating principle

This time we will analyze the operation of one of the most popular sensors - an ultrasonic (US) rangefinder. There are many different modifications similar devices, but they all work on the principle of measuring the travel time of reflected sound. That is, the sensor sends a sound signal in a given direction, then catches the reflected echo and calculates the flight time of the sound from the sensor to the obstacle and back. From a school physics course we know that the speed of sound in a certain medium is constant, but depends on the density of the medium. Knowing the speed of sound in air and the flight time of sound to the target, we can calculate the distance traveled by sound using the formula: s = v*t where v is the speed of sound in m/s, and t is time in seconds. The speed of sound in air, by the way, is 340.29 m/s. To cope with its task, the rangefinder has two important design features. Firstly, in order for sound to be reflected well from obstacles, the sensor emits ultrasound with a frequency of 40 kHz. To do this, the sensor has a piezoceramic emitter that is capable of generating such high frequency sound. Secondly, the emitter is designed in such a way that the sound does not spread in all directions (as is the case with conventional speakers), but in a narrow direction. The figure shows the radiation pattern of a typical ultrasonic rangefinder. As can be seen in the diagram, the viewing angle of the simplest ultrasonic rangefinder is approximately 50-60 degrees. For a typical use case, where the sensor detects obstacles in front of it, this viewing angle is quite suitable. Ultrasound can even detect a chair leg, while a laser rangefinder, for example, may not notice it. If we decide to scan the surrounding space, rotating the rangefinder in a circle like a radar, the ultrasonic rangefinder will give us a very inaccurate and noisy picture. For such purposes, it is better to use a laser rangefinder. It is also worth noting two serious disadvantages of the ultrasonic rangefinder. The first is that surfaces with a porous structure absorb ultrasound well, and the sensor cannot measure the distance to them. For example, if we decide to measure the distance from a multicopter to the surface of a field with tall grass, we will most likely get very fuzzy data. The same problems await us when measuring the distance to a wall covered with foam rubber. The second disadvantage is related to the speed of the sound wave. This speed is not fast enough to make the measurement process more frequent. Let's say there is an obstacle in front of the robot at a distance of 4 meters. It takes as much as 24ms for the sound to travel back and forth. You should measure 7 times before installing an ultrasonic rangefinder on flying robots.

2. Ultrasonic rangefinder HC-SR04

In this tutorial we will work with the HC-SR04 sensor and the Arduino Uno controller. This popular rangefinder can measure distances from 1-2 cm to 4-6 meters. At the same time, the measurement accuracy is 0.5 - 1 cm. There are different versions of the same HC-SR04. Some work better, others worse. You can distinguish them by the pattern of the board on the reverse side. The version that works well looks like this:

Here's a version that may fail:

3. Connection HC-SR04

The HC-SR04 sensor has four outputs. In addition to ground (Gnd) and power (Vcc), there is also Trig and Echo. Both of these pins are digital, so we connect them to any pins of the Arduino Uno:
HC-SR04 GND VCC Trig Echo
Arduino Uno GND +5V 3 2
Schematic diagram of the device Layout appearance

4. Program

So, let's try to order the sensor to send a probing ultrasonic pulse, and then record its return. Let's see what the timing diagram of the HC-SR04 looks like.
The diagram shows that to start measuring we need to generate at the output Trig positive pulse 10 µs long. Following this, the sensor will release a series of 8 pulses and raise the level at the output Echo, switching to the mode of waiting for the reflected signal. Once the rangefinder senses that the sound has returned, it will complete a positive pulse on Echo. It turns out that we need to do only two things: create a pulse on Trig to start measuring, and measure the length of the pulse on Echo, so that we can then calculate the distance using a simple formula. Let's do it. int echoPin = 2; int trigPin = 3; void setup() ( Serial.begin (9600); pinMode(trigPin, OUTPUT); pinMode(echoPin, INPUT); ) void loop() ( int duration, cm; digitalWrite(trigPin, LOW); delayMicroseconds(2); digitalWrite (trigPin, HIGH); delayMicroseconds(10); digitalWrite(trigPin, LOW); duration = pulseIn(echoPin, HIGH); cm = duration / 58; Serial.print(cm); Serial.println(" cm"); delay (100); ) Function pulseIn measures the length of the positive pulse on the echoPin leg in microseconds. In the program, we record the flight time of the sound in the duration variable. As we found out earlier, we will need to multiply the time by the speed of sound: s = duration * v = duration * 340 m/s Convert the speed of sound from m/s to cm/μs: s = duration * 0.034 m/µs For convenience, we convert the decimal fraction to an ordinary fraction: s = duration * 1/29 = duration / 29 Now let’s remember that the sound traveled two required distances: to the target and back. Let's divide everything by 2: s = duration / 58 Now we know where the number 58 in the program came from! Load the program onto the Arduino Uno and open the serial port monitor. Let's now try to point the sensor at different objects and look at the calculated distance in the monitor.

Tasks

Now that we can calculate distance using a rangefinder, we will make several useful devices.
  1. Construction rangefinder. The program measures the distance every 100ms using a rangefinder and displays the result on a symbolic LCD display. For convenience, the resulting device can be placed in a small case and powered by batteries.
  2. Ultrasonic cane. Let's write a program that will “beep” a buzzer at different frequencies, depending on the measured distance. For example, if the distance to an obstacle is more than three meters, the buzzer makes a sound once every half a second. At a distance of 1 meter - once every 100ms. Less than 10cm - beeps constantly.

Conclusion

The ultrasonic rangefinder is an easy-to-use, low-cost, and accurate sensor that has performed its function well on thousands of robots. As we learned in the lesson, the sensor has disadvantages that should be taken into account when building a robot. A good solution would be to use an ultrasonic rangefinder paired with a laser one. In this case, they will level out each other’s shortcomings.

HC-SR04 is one of the most common and cheapest rangefinders in robotics. It allows you to measure distances from 2cm to 4m (maybe more) with a decent accuracy of 0.3-1cm. The output is a digital signal, the duration of which is proportional to the distance to obstacles.

Ultrasonic range finder

I purchased this sensor a long time ago and was lying in its box, almost forgotten. But within the framework of one project, it was taken out into the open light and, for reference, a fairly compact rangefinder was built based on it and the voltmeter board.

Ultrasonic rangefinder HC-SR04

Sensor characteristics:

Power - 5V
Current consumption: less than 2mA
Effective viewing angle - 15 degrees
Measuring distance - 2cm - 5m
Accuracy - 3mm
Taken from the documentation for the sensor

Working principle of HC-SR04

Principle of operation

The module has 4 pins, two of which are power - ground and +5V, and two more are data. The module is polled in the following way: a pulse with a duration of 10 μs is sent to the Trig pin. The rangefinder generates a package of 8 ultrasonic 40KHz pulses. Which, reflected from most surfaces, return back if they do not fade away along the way. Immediately after sending the signal to Trig, we begin to expect a positive response signal from the Echo output, lasting from 150 μs to 25 ms, which is proportional to the distance to the object. More precisely, the travel time from the sensor to the obstacle and back. If there is no response (the sensor will not hear its echo), then the signal will return 38 ms long. The distance to the object (obstacle) is calculated using the following simple formula:

Where: L is the distance in centimeters to the object, and F is the pulse length at the Echo pin.
The recommended sensor polling time is 50ms or 20Hz.

The first tests of this module were carried out using a digital oscilloscope, which caught the response from the module and manually, by quickly shorting Trig to + power, tried to get a starting 10 μs pulse. In half the cases it worked [:)].

Design

The sensor was connected to a voltmeter board with a common anode, slightly modified to work with it (the unnecessary divider with capacitor was removed and the output from RA3 was added). A microcontroller from version 5 of the voltmeter was used - PIC16F688, with firmware revised for the ultrasonic rangefinder.