Dual-core control two-wheel self-balancing trolley based on STM32 and ESP32
By adopting the dual-core control architecture of STM32 and ESP32 on the two-wheel self-balancing car, combining PID control algorithms and human-computer interaction design, the problems of balance control stability and human-computer interaction friendliness are solved, and the intelligent and personalized interactive experience of the car is realized.
Patent Information
- Application Number
- CN202411835552.4
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-12-13
- Publication Date
- 2025-05-06
AI Technical Summary
The existing two-wheeled self-balancing trolleys have shortcomings in terms of balance control stability and human-computer interaction friendliness, which is difficult to meet users' needs for intelligent and personalized interactive experiences. At the same time, there are limitations in power management, sensor fusion and driving technology.
Adopting a dual-core control architecture based on STM32 and ESP32, the STM32 microcontroller is responsible for real-time balance control, and the ESP32 microcontroller is responsible for wireless communication and intelligent interaction functions. Through PID control algorithm and human-computer interaction design, stable balance and intelligent control of the car are achieved.
It significantly improves the balance control stability and human-computer interaction friendliness of the two-wheeled self-balancing trolley, improves the processing capability and adaptability of the system, and meets users' needs for intelligent and personalized interactive experiences.
Smart Images

Figure CN119937535A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of mobile robot technology, and in particular to a dual-core controlled two-wheeled self-balancing car based on STM32 and ESP32. The car achieves high stability and convenient operation through advanced control algorithms and human-computer interaction technology. Background Art
[0002] As an important branch of intelligent mobile robots, two-wheeled self-balancing cars have received great attention in the fields of automation and robotics in recent years. With its unique two-wheel design, this car can achieve flexible movement and steering while maintaining a stable upright posture, showing its wide application potential in search and rescue, inspection and monitoring, entertainment and transportation, and other fields.
[0003] However, the design and implementation of existing two-wheeled self-balancing vehicles face a series of technical challenges. Among them, the most critical is the stability of balance control. Due to the special structure of the two-wheeled self-balancing vehicle, it is inherently dynamic and unstable, and requires complex control algorithms to achieve balance. Traditional control methods often rely on a single control unit, which not only limits the processing power of the system, but also makes it difficult to adapt to the changing external environment and task requirements.
[0004] In addition, human-computer interaction friendliness is also an important aspect in the design of two-wheeled self-balancing cars. Users have increasingly higher requirements for the control method, interactive interface, and response speed of two-wheeled self-balancing cars. Existing interaction methods, such as manual operation and limited automated instructions, can no longer meet users' expectations for intelligent and personalized interactive experience.
[0005] The limitations of technology are also reflected in power management, sensor fusion, drive technology, etc. The efficiency of power management directly affects the endurance of the car; the accuracy and stability of sensor technology determine the car's ability to perceive the environment; and motor drive technology is related to the car's power output and motion performance.
[0006] In view of the above challenges, the present invention proposes a dual-core interactive control solution based on STM32 and ESP32 single-chip microcomputers, aiming to overcome the limitations of the prior art through technological innovation and achieve a significant improvement in the balance control stability and human-computer interaction friendliness of the two-wheeled self-balancing car. Through this solution, the performance of the two-wheeled self-balancing car can be effectively improved, its application scope can be broadened, and the growing demand of the market and users for intelligent mobile robots can be met. Summary of the invention
[0007] The present invention provides a dual-core controlled two-wheeled self-balancing car based on STM32 and ESP32. The car achieves significant improvements in stability and interactive friendliness through innovative control strategies and human-computer interaction design. The following are the main features and innovations of the present invention:
[0008] 1. Dual-core control architecture: The STM32 microcontroller is the main control unit, responsible for the balance control task with high real-time requirements. With its high performance and low power consumption, the STM32 microcontroller can quickly respond to and process data from the MPU6050 angle sensor and encoder, and accurately control the DRV8848 motor driver through the PID control algorithm to achieve balance control of the car. The ESP32 microcontroller is responsible for handling wireless communication and intelligent interaction functions, including Bluetooth control, voice control, and automatic obstacle avoidance. The high integration and low power consumption of the ESP32 microcontroller make it an ideal wireless communication and intelligent control unit.
[0009] 2. PID control algorithm: This invention adopts advanced PID control algorithm, which dynamically adjusts the motor drive signal by real-time analysis of MPU6050 sensor data to ensure that the car can maintain stable balance in various environments. The parameters of the PID algorithm are carefully adjusted to achieve the best control effect.
[0010] 3. Friendly human-computer interaction: By integrating the ASRPRO voice module and ultrasonic sensor with the ESP32 single-chip microcomputer, the present invention realizes Bluetooth control, voice control and automatic obstacle avoidance functions. Users can send control commands to smart devices via Bluetooth, or directly control the movement of the car through voice commands. The ultrasonic sensor provides real-time perception of the surrounding environment, enabling the car to automatically avoid obstacles.
[0011] 4. Modular design: The system design of the present invention adopts a modular concept. The STM32 and ESP32 microcontrollers seamlessly exchange key information through serial communication, building an efficient and collaborative dual-core interactive system. This design not only improves the modularity of the system, but also enhances the flexibility and response speed of information processing. BRIEF DESCRIPTION OF THE DRAWINGS
[0012] a) Figure 1 Hardware overall structure design diagram
[0013] b) Figure 2 STM32 overall program flow chart
[0014] c) Figure 3 ESP32 Program Flowchart
[0015] d) Figure 4 Control mode switching priority diagram
[0016] e) Figure 5 Rotation Angle Flowchart
[0017] f) Figure 6 PCB top layer layout
[0018] g) Figure 7 PCB bottom layer layout DETAILED DESCRIPTION
[0019] 1. Hardware design
[0020] The hardware system we designed is as follows Figure 1 As shown, the entire hardware system is mainly composed of three key components: power module, balance control module and remote control module.
[0021] The power module receives the 12V voltage powered by the battery through the 12V battery interface, and then converts it into the 3.3V and 5V voltages required by the system through two TPS62140DC-DC converters for system use.
[0022] The core of the balance control module is the STM32C8T6 microcontroller. The STM32C8T6 microcontroller reads the data of the MPU6050 angle sensor and encoder, and uses the PID algorithm to control the DRV8848 motor driver to drive the motor to rotate to achieve balance control. In addition, it also receives the movement instructions of the ESP32-32S module and performs the corresponding movement operations according to the received movement instructions.
[0023] The design of the remote control module is based on the ESP32-32S module, which is responsible for controlling the movement of the two-wheeled self-balancing car. The ESP32-32S module communicates with the STM32C8T6 microcontroller through the serial port and sends movement commands to adjust the movement of the car. The remote control is divided into two modes: Bluetooth and voice. Bluetooth control establishes a connection with the smartphone through the built-in Bluetooth function of the ESP32-32S module, receives and processes commands from the mobile phone; voice control relies on the ASRPRO voice module to recognize the voice captured by the microphone and convert it into instructions to send to the ESP32-32S module. The ESP32-32S module further processes these instructions, generates corresponding movement instructions, and sends them to the STM32C8T6 microcontroller to achieve precise control of the car. In addition, the ESP32-32S module can also read ultrasonic sensor data and realize automatic obstacle avoidance through automatic control logic. In terms of program downloading and debugging, both the ESP32-32S module and the ASRPRO voice module are integrated with the CH340 download circuit to facilitate the development and testing process.
[0024] 2. Software Design
[0025] The core tasks of the car's software design are mainly divided into two parts: balance control and remote control. The balance control part mainly undertakes the following three key subtasks: upright control, speed control and steering control. As for the remote control part, it includes Bluetooth control, voice control and automatic obstacle avoidance control using ultrasonic sensors. We divide the overall program design into three main parts: STM32 program, ESP32 program and voice control program.
[0026] The STM32 program has the core responsibilities, including reading the data of the MPU6050 angle sensor and encoder, using the PID algorithm to control the rotation of the motor to make the two-wheeled self-balancing car stand upright and move, and receiving the instructions from the ESP32. The specific implementation of the STM32 program is as follows Figure 2 The main program shown is composed of the following parts: main program, serial port idle interrupt handler and data read interrupt handler. The main program is responsible for initializing each module controlled by STM32 and executing the pick-up and drop-down detection of the self-balancing car, as well as controlling the LED light to flash after the data is successfully received. The serial port idle interrupt handler is used to receive and parse the data sent by ESP32 and perform corresponding operations. The data read interrupt handler is responsible for reading sensor data and using the PID control algorithm to maintain the balance or movement of the two-wheeled self-balancing car.
[0027] The ESP32 program is the hub of the remote control. It not only communicates with the smartphone via built-in Bluetooth, but is also responsible for reading the data from the ultrasonic sensor and receiving instructions from the voice control module. The ESP32 microcontroller determines the current control mode based on the internal control logic, and generates corresponding car movement instructions based on this, and then sends these instructions to the STM32 to drive the movement of the two-wheeled self-balancing car. Figure 3 As shown in the figure, its implementation consists of four parts: module initialization, Bluetooth control, voice control and automatic control. Module initialization is responsible for the configuration of each hardware module at startup; Bluetooth control realizes wireless control of the balance car through the mobile phone Bluetooth; voice control allows users to operate through voice commands; automatic control uses ultrasonic sensors to achieve automatic obstacle avoidance. When the system is running, the three control modes determine the currently activated mode through the state of the mode flag bit, and switch between modes by changing the flag bit, ensuring the flexibility and diversity of the balance car's operation. In order to prevent control conflicts, the three control modes are as follows Figure 4 As shown, the two control modes have different priorities. Bluetooth control has the highest priority, followed by voice control, and automatic control has the lowest priority. Bluetooth control can send a command to switch the current control mode at any time. In contrast, voice control can only switch modes when automatic control or voice control mode is activated. Automatic control mode cannot actively switch and can only wait for other modes to intervene. At any given moment, only one control method is active.
[0028] The voice control program consists of two parts: the main function and the voice recognition interrupt processing function. The main function is responsible for executing power-on initialization, system initialization and main loop. The voice recognition interrupt processing function is triggered after the voice command is successfully recognized and performs the corresponding operation according to the current operation mode.
[0029] Among them, the automatic control process needs to rotate a specific angle, but because the angle data is cyclic from 0 to 360 degrees, when the angle exceeds 0 degrees or 360 degrees, a jump will occur, so we need a certain algorithm to realize the function of rotating to a specific angle. Figure 5 As shown in the figure, the core idea of the algorithm is to find the shortest rotation path from the current angle to the target angle. If the calculated shortest path is smaller than the error range we set, we think it has rotated close enough to the target angle. If the shortest path exceeds the allowable error, the balancing car will rotate in the direction indicated by the shortest path. The direction of rotation can be clockwise or counterclockwise, depending on the relative position of the current angle to the target angle. We calculate the path lengths required to reach the target angle clockwise and counterclockwise respectively.
[0030] If the current angle is less than the target angle, the calculation formula is as follows:
[0031] L 顺时针 =θ 目标 +(360°-θ 当前 ) (1.1)
[0032] L 逆时针 =θ 目标 -θ 当前 (1.2)
[0033] If the current angle is greater than the target angle, the calculation formula is as follows:
[0034] L 顺时针 =θ 当前 -θ 目标 (1.3)
[0035] L 逆时针 =θ 目标 +(360°-θ 当前 ) (1.4)
[0036] By comparing the path lengths required for clockwise and counterclockwise rotations, we can determine which path is shorter. If the rotation angle of the shortest path is less than the error we set, then we believe that the target angle has been reached. Otherwise, the balancing car will rotate in the direction of the shortest path until it reaches the target angle or an angle within the error range.
[0037] 3. Physical design and debugging
[0038] We used EasyEDA to complete the schematic drawing of the hardware design according to the previous hardware design circuit. After completing the schematic drawing, we can start the PCB design. Before designing, we first layout the positions of each part of the device. The layout structure is as follows Figure 6 and Figure 7 As shown. On the front side of the PCB board is an ultrasonic sensor to facilitate the two-wheeled self-balancing car to measure distance and achieve automatic obstacle avoidance. The interfaces on the left side of the PCB from front to back are the power interface, power switch, and left motor interface, and the interfaces on the right side from front to back are the microphone interface, right motor interface, ESP32 TYPE-C interface, and ASRPRO voice module TYPE-C interface. These interfaces and switches are distributed on the edge of the board for easy operation. In addition, the button is designed on the back edge of the PCB board for easy pressing during debugging. As for other devices that do not affect the layout, they are distributed on the top or bottom layer of the PCB board according to the convenience of wiring. After completing the device layout, we wired the PCB to realize the circuit design. In order to facilitate the design, we designed the PCB as a four-layer board, the top and bottom layers are used for signal line wiring, inner layer 1 is used for power wiring, and inner layer 2 is used for GND copper grounding.
Claims
1. A dual-core controlled two-wheeled self-balancing car based on STM32 and ESP32, characterized by:
1. An STM32F103C8T6 microcontroller is used as the main control unit; 2. An ESP32 microcontroller as an auxiliary control unit; 3. An MPU6050 angle sensor, connected to the STM32 microcontroller, to provide angle data; 4. A DRV8848 motor driver, connected to the STM32 microcontroller, is used to drive the motor to rotate; 5. An ASRPRO voice module, connected to the ESP32 microcontroller, for voice control; 6. An ultrasonic sensor connected to the ESP32 microcontroller for automatic obstacle avoidance; 7. The STM32 microcontroller controls the DRV8848 motor driver through a PID control algorithm; 8. The ESP32 microcontroller establishes a connection with the smartphone through the Bluetooth module, receives and parses the control instructions from the mobile phone, and converts these instructions into specific movement instructions, which are passed to the STM32 microcontroller to realize wireless control of the car; 9. The system realizes information exchange between the STM32 microcontroller and the ESP32 microcontroller through serial port communication.
2. The two-wheeled self-balancing vehicle according to claim 1, characterized in that: The STM32 microcontroller controls the DRV8848 motor driver through a PID control algorithm, wherein the PID control algorithm includes three links: proportional (P), integral (I) and differential (D), which are used to adjust the speed and direction of the motor in real time to maintain the balance of the car.
3. The two-wheeled self-balancing vehicle according to claim 1 or 2, characterized in that: The ESP32 microcontroller establishes a connection with the smartphone via a Bluetooth module, receives and parses control instructions from the mobile phone, and converts these instructions into specific movement instructions, which are then passed to the STM32 microcontroller to achieve wireless control of the car.
4. The two-wheeled self-balancing vehicle according to claims 1 to 3, characterized in that: The ESP32 single-chip microcomputer receives and recognizes the user's voice commands through the voice module, converts the recognition results into control commands, and transmits them to the STM32 single-chip microcomputer to realize voice control of the car.
5. The two-wheeled self-balancing vehicle according to claim 1, characterized in that: The ESP32 single-chip microcomputer detects obstacles ahead through an ultrasonic sensor, generates obstacle avoidance instructions based on the detection results, and transmits them to the STM32 single-chip microcomputer to realize automatic obstacle avoidance of the car.