yahboom
HomeLearn
Solution
Blog
Online Store
About Us
EN/中文
STM32-BoardCar📖
0. Get started quickly
  • 0.ReadMe
  • 1. Installation Instructions
  • 2. Version configuration
  • 3. Smart car installation video
  • 4. Precautions for use
1. Introduction to STM32 development board
  • 1. Learn about STM32
  • 2. Introduction to STM32 development board
  • 3. Development board pin assignment
  • 4. Introduction to HAL library and LL library
2. Setting up and using the development environment
  • 1. Common development tools
  • 2. STM32CubeMX Install - Use
  • 3. STM32CubeIDE Install - Use
  • 4. Common Driver Installation
  • 5. Program download and simulation
  • 6. Engineering experience and migration
3. Development board basic tutorial
  • 1. RGB searchlight (GPIO)
  • 2. Buzzer control (GPIO)
  • 3. Button control (EXTI)
  • 4. Serial communication (UASRT)
  • 5. Voltage detection (ADC)
  • 6. Internal temperature sensor(ADC)
  • 7. Independent watchdog(IWDG)
  • 8. Window watchdog(WWDG)
  • 9. Basic timer (TIM6)
  • 10. Universal Timer (TIM3)
  • 11. Advanced timers (TIM1)
  • 12. I2C Communication (IIC)
  • 13. Off-chip Flash (SPI)
  • 14. On-chip Flash (Flash)
  • 15. DMA:USART
  • 16. DMA:SPI
  • 17. DMA:I2C
  • 18. USB communication
  • 19. USB analog U disk
  • 20. FreeRTOS
4. Car expansion tutorial
  • 1. Servo control
  • 2. Basic use of ultrasonic steering gear gimbal
  • 3. Motor control
  • 4. OLED display light sensitivity value
  • 5. Infrared sensor
  • 6. Four-way tracking module
  • 7. Infrared remote control
  • 8. Ultrasonic ranging
  • 9. PS2 controller test
  • 10.K230 Vision Module
  • 11.AI voice interaction integration module
5. Robot car tutorials
  • 1. Car line patrol
  • 2. Ultrasonic follow
  • 3. Ultrasonic obstacle avoidance
  • 4. Ultrasonic obstacle avoidance (servo gimbal version)
  • 5. Infrared sensor obstacle avoidance
  • 6. Infrared remote control
  • 7. PS2 handle remote control
  • 8.K230 face tracking on PTZ
  • 9.K230 color tracking
  • 10.K230 Color Line Patrol
  • 11.K230 QR code command
  • 12.K230 machine code tracking
  • 13.K230 road sign indication action (voice)
  • 14.K230 color response (voice)
  • 15.K230 gesture control (voice)
  • 16.K230 Rock-Paper-Scissors Game (Voice)
6. Operation video
  • 1-How to install STM32CubeIDE software
  • 2-How to create Demo project
  • 3-How to download code into STM32 robot car
  • 4-How to open and compile a code file for STM32
  • 5-About IR control and Charge battery
Download
  • Code
  • SCH
  • Software File
  • STM32 Chip DataSheet
  • Installation manual
Download ZIP

Welcome to STM32-BoardCar repository

10.K230 Color Line Patrol

K230颜色循迹

k230 color line patrol

k230 color line patrol1. Software-Hardware2. Brief principle1. Hardware schematic2. Physical connection diagram3. Control principle3. Main functions4. Partial code analysis5. Experimental phenomenon

This tutorial is a comprehensive experiment combining multiple peripherals. You can first understand a single peripheral and then conduct this experiment.

1. Software-Hardware

  • STM32F103CubeIDE

  • STM32 robot development board

k230 module: external

  • Type-C data cable or ST-Link

Download or simulate the development board

2. Brief principle

1. Hardware schematic

2023103104.png

2. Physical connection diagram

Motor wiring

2023103103

K230 wiring (Note: The wiring in the figure below is for position reference only. We are equipped with K230 dual-head PH2.0 4Pin all-black wire, with fool-proof design, no need to worry about wiring problems)

PixPin_2025-06-10_19-15-08

3. Control principle

  • Program flow chart

Yes
No
Start
Key1 is pressed
K230 starts learning color threshold
K230 detects the target?
K230 sends data according to the specified protocol
MCU analyzes data and controls the car to give feedback
Car goes straight

 

Control principle:

PID control

PID control is a commonly used closed-loop control algorithm that minimizes the error between the actual output and the expected output of the system by continuously adjusting the output of the controller.

PID parametersFunctionPurpose
P (Proportional)Determines the response speed of the PID controller outputAccelerates the system response speed
I (Integral)Determines the response degree of the PID controller to the accumulated errorEliminates steady-state error
D (Differential)Determines the response degree of the PID controller to the error change rateImproves the steady-state performance of the system
ModuleFunction
k230 visual moduleLearn the color of the track and then send the data to the microcontroller through the serial port

Communication protocol:

When k230 recognizes the color track, it will send a data frame through the serial port in the following format

Experimental routineStart characterLengthDelimiterRoutine numberDelimiterxDelimiteryDelimiterwDelimiterhEnd character
Color recognition$XX,01,XXX,XXX,XXX,XXX#

MCU part:

Receive the data sent by k230 through the serial port and process each byte of the data. Extract valid data from the data frame content and store it in the array. Then perform PID processing on the array data, and the MCU uses the processed data to drive the motor to move along the track.

3. Main functions

Function: APP_IRR_PID_Init

Function prototypevoid APP_IRR_PID_Init(void)
Function descriptionInitialize k230, infrared PID parameters
Input parametersNone
Return valueNone

Function: APP_K230_PID_Calc

Function prototypevoid APP_K230_PID_Calc(void)
Function descriptionControl motor movement after PID calculation
Input parametersNone
Return valueNone

Function: wheel_Ctrl

Function prototypevoid wheel_Ctrl(int16_t V_x, int16_t V_y, int16_t V_z)
Function descriptionControl motor movement after PID calculation
Input parameter 1Speed in the x-axis direction (straight)
Input parameter 2Speed in the y-axis direction (translation)
Input parameter 3Speed in the z-axis direction (rotation)
Return valueNone
For the underlying driver, you can refer to [Chapter 3: Advanced Timer Tutorial] and [Chapter 4: Motor Control Tutorial]
For the application layer, you can read the source code in the project file by yourself

4. Partial code analysis

  • yb_protocol.c

​x
void Pto_Data_Parse_K230(uint8_t *data_buf, uint8_t num)
{
uint8_t pto_head = data_buf[0];
uint8_t pto_tail = data_buf[num-1];
if (!(pto_head == PTO_HEAD && pto_tail == PTO_TAIL)) {
return;
}

uint8_t data_index = 1;
uint8_t field_index[PTO_BUF_LEN_MAX1] = {0};
int i = 0;

// Parse comma separator
for (i = 1; i < num-1; i++) {
if (data_buf[i] == ',') {
data_buf[i] = 0;
field_index[data_index] = i;
data_index++;
}
}

// Handle pure string without comma Road sign recognition
if (data_index == 1) {
uint8_t len = num - 2;
memcpy(lubiao, (char*)data_buf+1, len);
lubiao[len] = '\0'; // Make sure the string ends with '\0'
}
else { // Process multi-field data
for (i = 0; i < data_index; i++) {
uint8_t start = field_index[i] + 1;
uint8_t len;

if (i == data_index - 1) {
// Last field, length is calculated before the end mark
len = (num - 1) - start;
} else {
// Middle field, length is the next delimiter position minus the current start position
len = field_index[i + 1] - start;
}

if (i == 6 && data_index == 7) {
memcpy(msg, (char*)data_buf + start, len);
msg[len] = '\0'; // Add string terminator
}
else if (i == 2 && data_index == 3) {
memcpy(game, (char*)data_buf + start, len);
game[len] = '\0'; // Add string terminator
}
else {
values1[i] = Pto_Char_To_Int((char*)data_buf + start);
}
}

// Protocol length verification
uint8_t pto_len = values1[0];
if (pto_len != num) {
printf("pto_len error:%d , data_len:%d\n", pto_len, num);
return;
}
pto_id = values1[1];
}
int x = values1[2];
int y = values1[3];
int w = values1[4];
int h = values1[5];
// int degress = values[7];
/* Open when debugging
// printf("dmcode:x:%d, y:%d, w:%d, h:%d,\n", x, y, w, h);
// printf("msg:%s\n", game);
// // printf("game:%d\n", pto_id
// Debug output
// printf("msg:%s\n", msg);
*/
}

5. Experimental phenomenon

After successfully downloading the program, we need to experiment in an environment with relatively stable light. (Because the color will be affected by the light, if the tracked color is not collected, you need to press the K230 reset button to re-collect)

Put the track color under the k230 lens to learn. After learning, press the k1 button on the expansion board, and the car will patrol the track.

K230 color tracking
K230 QR code command

COMPANY

  • About us
  • Team Style
  • Blog

LEARNING & EXPLORATION

  • Tutorials
  • Edge AI hardware
  • Educational robots
  • Embedded intelligent hardware

WHERE TO BUY

  • Yahboom Online Store
  • Amazon
  • Alibaba
  • AliExpress
  • Mercado

TECHNOLOGY SUPPORT

  • Email: support@yahboom.com

BE A DISTRIBUTOR

  • Email: sales@yahboom.com
QR Code

Copyright © 2017-2026 Yahboom. All Rights Reserved.

EN/中文