Ultrasonic ranging

This tutorial demonstrates: How to connect an external expansion board and use the ultrasonic module to measure distance and then print the data through the serial port.

1. Software-Hardware

2. Brief principle

1. Hardware schematic diagram

image-20230925153122335

2、Physical connection diagram

image-20230926100744845

3. Control principle

(Schematic name)Control pinSpecific meaning
TRIGPF11Trigger terminal
ECHOPF12Receiver

Ultrasonic Module:

It is a module that uses ultrasonic waves for non-contact physical quantity measurement. It can accurately measure distance, speed, flow and other physical quantities by transmitting and receiving ultrasonic signals, and converts the measurement results into digital signal output. This article will popularize the ultrasonic module Its working principle and function.

The types of ultrasonic modules on the market are mainly divided into the following categories:

Ultrasonic module information for this experiment:

ModelHC-SR04Detection distance2-400cm
Working voltage5VHigh precisionUp to 0.3cm
Operating current15mADead zone2cm
Operating frequency40KHzPin sequenceVCC, Trig (control end), Echo (receiving end), GND
Quiescent operating current<2mAInput trigger signal10uS TTL pulse
Sensing angleNot greater than 15°Input echo signalOutput TTL level signal, proportional to range
Range range2cm-4m (peak)Level outputTTL level

Ranging principle: Input a high potential for more than 10 microseconds at the trigger end of the ultrasonic module to emit ultrasonic waves. After transmitting the ultrasonic waves and before receiving the returned ultrasonic waves, the receiving end is at a high potential. Therefore, the program can calculate the distance of the measured object from the high pulse duration of the "response" pin.

Test distance = (high level time * speed of sound (340M/S))/2;

image-20230926100744845

Note: The above timing diagram shows that you only need to provide a pulse trigger signal of more than 10us, and the module will internally send out 8 40kHz cycle levels and detect echoes. Once an echo signal is detected, an echo signal is output. The pulse width of the reverberated signal is proportional to the measured distance. The distance can be calculated from the time interval between the transmitted signal and the received echo signal.

3. Project configuration

1. Description

Omitted project configuration part: New project, chip selection, project configuration, SYS of pin configuration, RCC configuration, clock configuration and project configuration content

2. Pin configuration

image-20230926100744845

image-20230926100744845

image-20230926100744845

image-20230926100744845

image-20230926100744845

image-20230926100744845

image-20230926100744845

4. Main functions

According to our tutorial STM32CubeIDE can generate the corresponding gpio.c, gpio.h, tim.c and tim.h files. For later transplantation and peripheral module driver, we will place the automatically generated code in the BSP under the project file. folder.

1. User function

Function: Ultrasonic_GPIO_Init

Function prototypevoid Ultrasonic_GPIO_Init(void)
Function descriptionUltrasonic pin initialization
Input parametersNone
Output parametersNone

Function: Get_distance

Function prototypefloat Get_distance(void)
Function descriptionUltrasonic range detection
Input parametersNone
Output parametersOutput distance

Function: Bsp_TIM7_Init

Function prototypevoid Bsp_TIM7_Init(void)
Function descriptionOpen timer terminal
Input parametersNone
Output parametersNone

5. Experimental phenomena

After downloading the program, you need to place obstacles in the direction facing the ultrasonic module. Open the serial port assistant and set the parameters as shown in the figure below. Then we can use the serial port assistant to view the measured ultrasonic distance.

The effect is as follows:

image-20230926100744845