Welcome to MSPM0 Smart Robot Car repository
6.Infrared sensor avoid
Infrared sensor avoid
Infrared sensor avoid1. Software-Hardware2. Brief Principle2.1 Hardware Schematic Diagram2.2 Physical Connection Diagram2.3 Control Principle3. Main Functions4. Experimental Phenomenon
This tutorial is a comprehensive experiment combining multiple peripherals. You can understand individual peripherals before conducting this experiment.
1. Software-Hardware
KEIL
MSPM0G3507 Development Board
Type-C data cable or DAP-Link
For program download or simulation to the development board
2. Brief Principle
2.1 Hardware Schematic Diagram
.png)

2.2 Physical Connection Diagram
.png)
2.3 Control Principle
Obtain obstacle distance through infrared sensor and ultrasonic module, and control car movement state (rotation, forward and backward) based on the distance.
This project combines infrared sensor and ultrasonic module to implement obstacle avoidance function, obtaining front distance through ultrasonic and side distances through infrared sensor: If obstacles in front or on sides detected by ultrasonic are within the set distance, we call the car control function in the function, then control the car to turn left or right based on the left and right distances from infrared sensor, so that the car can be adjusted in time; If obstacles are not within the set distance, we control the car to move forward.
- Program Flowchart

| Module | Function |
|---|---|
| Ultrasonic Module | External information collection: obtain front obstacle distance |
| Infrared Sensor | External information collection: obtain side obstacle distances |
| Motor | Motion control |
3. 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.
Function: get_light_distance
| Function Prototype | void get_light_distance(uint16_t distance) |
|---|---|
| Function Description | Set infrared obstacle avoidance distance |
| Input Parameters | Infrared obstacle avoidance measurement value |
| Output Parameters | None |
Function: Open_IR_Switch
| Function Prototype | void Open_IR_Switch(void) |
|---|---|
| Function Description | Turn on infrared obstacle avoidance output |
| Input Parameters | None |
| Output Parameters | None |
Function: Close_IR_Switch
| Function Prototype | void Close_IR_Switch(void) |
|---|---|
| Function Description | Turn off infrared obstacle avoidance output |
| Input Parameters | None |
| Output Parameters | None |
4. Experimental Phenomenon
After successfully downloading the program, press the RESET button on the development board and observe the car's effect!
xxxxxxxxxxFor program download, refer to [3. Development Environment Setup and Usage: 3.Uniflash Programming]
Phenomenon:
No obstacle: The car moves forward
With obstacle: Judge the distances of left and right infrared sensors, rotate to the side with greater obstacle distance