Welcome to MSPM0 Smart Robot Car repository
8.Ultrasonic ranging
Ultrasonic Ranging
Ultrasonic Ranging1. Software and Hardware2. Basic Principles2.1 Hardware Schematic2.2 Physical Connection Diagram2.3 Control Principle3. Project Configuration3.1 Description3.2 Pin Configuration4. Main Functions4.1 User Functions5. Experimental Phenomenon
This tutorial demonstrates: how to externally connect and use an ultrasonic module on the expansion board for ranging and display data on OLED.
1. Software and Hardware
KEIL5
MSPM0G3507 Expansion Board
Ultrasonic Module
Type-C data cable or DAP-Link
For programming download or simulation to the development board
Serial Port Assistant
Receive and print serial port data
2. Basic Principles
2.1 Hardware Schematic
.png)
2.2 Physical Connection Diagram
.png)
2.3 Control Principle
| (Schematic Name) | Control Pin | Specific Meaning |
|---|---|---|
| TRIG | PB7 | Trigger End |
| ECHO | PB11 | Receiver End |
Ultrasonic Module:
It is a module that uses ultrasonic waves for non-contact physical quantity measurement. It can accurately measure physical quantities such as distance, speed, and flow rate by emitting and receiving ultrasonic signals, and convert the measurement results into digital signal outputs. This article will explain the working principle and function of the ultrasonic module.
Main types of ultrasonic modules on the market:
- HC-SR04 Ultrasonic Module
- US-100 Ultrasonic Module
- US-015 Ultrasonic Module
- HY-SRF05 Ultrasonic Module
- HC-SR04 Ultrasonic Ranging Module
This experiment's ultrasonic module information:
| Model | HC-SR04 | Detection Distance | 2-400cm |
|---|---|---|---|
| Working Voltage | 5V | High Precision | Up to 0.3cm |
| Working Current | 15mA | Blind Zone | 2cm |
| Working Frequency | 40KHz | Pin Wiring Sequence | VCC, Trig (Control End), Echo (Receiver End), GND |
| Static Working Current | <2mA | Input Trigger Signal | 10uS TTL pulse |
| Detection Angle | Not more 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 of more than 10 microseconds at the trigger end of the ultrasonic module to emit ultrasonic waves. After emitting the ultrasonic waves and before receiving the returned ultrasonic waves, the receiver end is at high potential. Therefore, the program can calculate the distance of the measured object from the high-level pulse duration of the "response" pin. Test Distance = (High Level Time * Sound Speed (340M/S)) / 2;

Note: The above timing diagram shows that you only need to provide a pulse trigger signal of more than 10us. The module will internally emit 8 40kHz cycle levels and detect echoes. Once an echo signal is detected, it will output an echo signal. The pulse width of the echo signal is proportional to the measured distance. Therefore, the distance can be calculated from the time interval between transmitting the signal and receiving the echo signal.
3. Project Configuration
3.1 Description
You can refer to the basic tutorial to complete the development environment setup.
3.2 Pin Configuration





4. Main Functions
4.1 User Functions
Function: Ultrasonic_GPIO_Init
| Function Prototype | void Ultrasonic_GPIO_Init(void) |
|---|---|
| Function Description | Ultrasonic pin initialization |
| Input Parameters | None |
| Output Parameters | None |
Function: Ultrasonic_Printf
| Function Prototype | float Ultrasonic_Printf(void) |
|---|---|
| Function Description | Print distance through serial port and display on OLED |
| Input Parameters | None |
| Output Parameters | None |
Function: Hcsr04GetLength
| Function Prototype | float Hcsr04GetLength(void) |
|---|---|
| Function Description | Get measured distance |
| Input Parameters | None |
| Output Parameters | Measured distance |
Function: Get_TIMER_Count
| Function Prototype | uint32_t Get_TIMER_Count(void) |
|---|---|
| Function Description | Get timer timing time |
| Input Parameters | None |
| Output Parameters | Data |
5. Experimental Phenomenon
After downloading the program, you need to place an obstacle in the direction facing the ultrasonic module. The distance to the obstacle can be viewed through the OLED.
For program download, refer to 【3. Development Environment Setup and Usage: 3. Uniflash burning】
The effect is as follows:
