Ultrasonic ranging1. Software-Hardware2. Brief principle1. Hardware schematic diagram2、Physical connection diagram3. Control principle3. Project configuration1. Description2. Pin configuration4. Main functions1. User function5. Experimental phenomena
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.
STM32F103CubeIDE
STM32 expansion board
Ultrasonic Module
Type-C data cable or ST-Link
Download programs or simulate the development board·
Serial Assistant
Receive serial port data and print
(Schematic name) | Control pin | Specific meaning |
---|---|---|
TRIG | PF11 | Trigger terminal |
ECHO | PF12 | Receiver |
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:
Model | HC-SR04 | Detection distance | 2-400cm |
---|---|---|---|
Working voltage | 5V | High precision | Up to 0.3cm |
Operating current | 15mA | Dead zone | 2cm |
Operating frequency | 40KHz | Pin sequence | VCC, Trig (control end), Echo (receiving end), GND |
Quiescent operating current | <2mA | Input trigger signal | 10uS TTL pulse |
Sensing angle | Not greater than 15° | Input echo signal | Output TTL level signal, proportional to range |
Range range | 2cm-4m (peak) | Level output | TTL 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;
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.
Omitted project configuration part: New project, chip selection, project configuration, SYS of pin configuration, RCC configuration, clock configuration and project configuration content
Please refer to [2. Development environment construction and use: STM32CubeIDE installation and use] to understand how to configure the omitted parts of the project.
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.
Function: Ultrasonic_GPIO_Init
Function prototype | void Ultrasonic_GPIO_Init(void) |
---|---|
Function description | Ultrasonic pin initialization |
Input parameters | None |
Output parameters | None |
Function: Get_distance
Function prototype | float Get_distance(void) |
---|---|
Function description | Ultrasonic range detection |
Input parameters | None |
Output parameters | Output distance |
Function: Bsp_TIM7_Init
Function prototype | void Bsp_TIM7_Init(void) |
---|---|
Function description | Open timer terminal |
Input parameters | None |
Output parameters | None |
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.
xxxxxxxxxx
For program download, please refer to [2. Development environment construction and use: program download and simulation]
The effect is as follows: