Welcome to MSPM0 Smart Robot Car repository
7.Light following
Light following
Light following1. Software-Hardware2. Brief Principle2.1 Hardware Schematic Diagram2.2 Physical Connection Diagram2.3 Control Principle3. Main Functions3.1 User 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
Control car movement by obtaining photoresistor resistance values, and then changes in resistance values caused by light source position changes.
- Program Flowchart

- ADC
By using ADC (Analog-to-Digital Converter), the analog voltage output by the photosensitive sensor can be converted to digital values.
MSPM0G3507 includes one 12-bit analog-to-digital converter, and the converted value range is 0 to 2^12-1 (i.e., 0 to 4095);
| Photosensitive Sensor (integrated on development board) | Corresponding Pin |
|---|---|
| LIGHT1 | PA24 |
| LIGHT2 | PB24 |
- OLED
| Product | 0.91-inch OLED Display |
|---|---|
| Resolution | 128*32 |
| Control Chip | SSD1306 |
| Communication Method | I2C (IIC) |
| Working Voltage | 3.3~5V |
| I2C Interface | VCC: Power positive (3.3/5V) GND: Power ground SCL: I2C bus clock signal SDA: I2C bus data signal |
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.
3.1 User Functions
Function: Get_ADC_Value
| Function Prototype | void Get_ADC_Value() |
|---|---|
| Function Description | Used to obtain photosensitive values |
| Input Parameter | None |
| Output Parameter | None |
Function: Get_Sun_Run2
| Function Prototype | void Get_Sun_Run2() |
|---|---|
| Function Description | Light following main program, combines obtained photosensitive values with PID control to achieve following effect |
| Input Parameter | None |
| Output Parameter | None |
Function: PID_Calculate
| Function Prototype | float PID_Calculate(PID_TypeDef *PID,float CurrentValue) |
|---|---|
| Function Description | PID closed-loop control calculation |
| Input Parameter 1 | *PID: PID structure variable address |
| Input Parameter 2 | CurrentValue: current measured value |
| Output Parameter | Expected output value |
4. Experimental Phenomenon
After burning the program, turn on the car power, press the NRST key to run. When the car is set to move forward or be stationary according to the environment, it can move around according to the light source when moving forward, and can follow in 3 directions when stationary.
Note: Since light following is controlled based on photosensitive values, when conducting this case, you must first adjust appropriate photosensitive values for forward and backward control according to key factors.