Welcome to MSPM0 Smart Robot Car repository
2.Ultrasonic servo PTZ basic use
Ultrasonic Servo PTZ Basic Use
Ultrasonic Servo PTZ Basic Use1. Software and Hardware2. Basic Principles2.1 Hardware Schematic2.2 Physical Connection Diagram2.3 Control Principle3. Project Experience3.1 Opening the Project4. Main Functions4.1 User Functions5. Experimental Phenomenon
This tutorial demonstrates: how to externally connect and use an ultrasonic servo PTZ on the expansion board and then print ultrasonic ranging data via serial port.
1. Software and Hardware
KEIL5
MSPM0G3507 Expansion Board
Ultrasonic Servo PTZ
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


2.2 Physical Connection Diagram
.png)
| Servo (S1) | Car | Specific Meaning |
|---|---|---|
| SIG(Yellow) | PA28(Yellow) | Signal Line |
| VCC(Red) | VCC(Red) | Power Supply |
| GND(Brown) | GND(Black) | Ground Line |
| Servo (S2) | Car | Specific Meaning |
|---|---|---|
| SIG(Yellow) | PA7(Yellow) | Signal Line |
| VCC(Red) | VCC(Red) | Power Supply |
| GND(Brown) | GND(Black) | Ground Line |
Ultrasonic Module:
Note: Using the servo PTZ requires adding jumper caps to connect 5V and choose, see the white line.
2.3 Control Principle
| (Schematic Name) | Control Pin | Specific Meaning |
|---|---|---|
| TRIG | PB7 | Trigger End |
| ECHO | PB11 | Receiver End |
| S1 | PA28 | Servo Control Pin |
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.
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.
PWM Control Servo Principle
Generate PWM signals using the timer/counter (TIM) peripheral and control the servo angle by adjusting the duty cycle.

3. Project Experience
Use the project files we provide to directly experience the corresponding functions of the development board.
Later tutorials will not provide this content to avoid repetition. You can refer to 3. Development Environment Setup and Usage: 5. Project Porting for operations
3.1 Opening the Project
Project File Location
Project file path: Under the corresponding course folder in the 【Program Source Code Summary】 folder

Opening the Project File
Copy the project file to the msp_sdk directory under an English path, use KEIL5 to open the project file, and select the .project file when opening

Pin Configuration







4. Main Functions
Mainly introduces the functional code written by users. For detailed code, you can open the project files we provide and view the source code in the Bsp folder.
4.1 User Functions
Function: servo_init
| Function Prototype | void servo_init(void) |
|---|---|
| Function Description | Initialize timer simulated PWM signal output |
| Input Parameters | None |
| Output Parameters | None |
Function: Set_Servo270_Angle
| Function Prototype | void Set_Servo270_Angle(unsigned int angle) |
|---|---|
| Function Description | Control 270-degree servo rotation |
| Input Parameters | angle: rotation angle |
| Output Parameters | None |
Function: Set_Servo_Angle
| Function Prototype | void Set_Servo_Angle(unsigned int angle) |
|---|---|
| Function Description | Control 180-degree servo rotation |
| Input Parameters | angle: rotation angle |
| Output Parameters | None |
Function: Hcsr04GetLength
| Function Prototype | float Hcsr04GetLength (void) |
|---|---|
| Function Description | Calculate and obtain ultrasonic data |
| Input Parameters | None |
| Output Parameters | Ultrasonic data |
Function: get_servo_Printf
| Function Prototype | void get_servo_Printf () |
|---|---|
| Function Description | Servo rotates from 90° to 0°, then to 90° and 180°, during which ultrasonic data is obtained and respectively printf output and displayed on OLED |
| Input Parameters | None |
| Output Parameters | None |
5. Experimental Phenomenon
During installation, you need to center the servo first before installation; the tutorial folder provides a 【MSPM0G_Car_Servo_ultrasonic.hex】 file for calibration. After burning this program, the servo will rotate to 90°. At this time, install the connection board between the ultrasonic and servo PTZ (physical object as shown below) on the servo. When installing the ultrasonic, it needs to face forward (slight deviation is acceptable).

After servo calibration, we can burn the code for this chapter. Then after powering on the car, press the Reset key. We need to connect to the host computer through the Type-C interface, open the serial port assistant, set the parameters as shown in the figure below, and then we can view the measured ultrasonic distance through the serial port assistant.
After the car is powered on, the PTZ will first turn to the middle, then turn left, and then turn right. It cycles back and forth in these three directions. The serial port will print the distance between the ultrasonic measurement and obstacles.
For program download, refer to 【3. Development Environment Setup and Usage: 3. Uniflash burning】
Servo wiring is as follows:


Effect is as follows:
