Modelling · Electronics · Control
STM32 BLE
Self-Balancing Robot
The motivation behind this project is to further develop my engineering skills through the design and construction of a self-balancing robot. Although relatively simple in concept and admittedly not particularly useful, the project brings together several engineering disciplines, offering an opportunity to explore the challenges involved in taking an idea from initial concept through to physical implementation.
I began by developing a simplified mathematical model of the robot's dynamics to estimate its mechanical requirements. The model provided a quantitative basis for component selection and helped establish the constraints for the physical and electrical design.
With these requirements established, the project extends into electronics and mechanical design, embedded firmware development using Zephyr RTOS, and the exploration of different control strategies to achieve stable balancing.
Ultimately, the goal is not simply to build a self-balancing robot, but to use the project as a platform for continued learning, technical experimentation, and refining my approach to engineering design.
Project topic
01 / The model
From a falling body to a motor-selection envelope.
The model asks a specific engineering question: what motor can move the robot toward upright without it falling, slipping, or losing ground contact? The answer informs more than the motor choice. It allows us to establish the current and voltage that the subsequent electrical design must be designed around.
Wheel analysis
We start by analysing the forces around the wheel. Motor torque acts in the positive clockwise direction. The tire pushes the ground backwards and, provided the wheel does not slip, the floor exerts an equal and opposite forward force Fx on the tire, driving the robot forward. Re-arranging for Tmotor, Newton's second law for rotation therefore gives:
Equation (1) shows that the torque produced by the motor is split between generating the driving force Fx and accelerating the wheels rotationally.
Provided the no-slip condition remains true, we can tie wheel rotation to axle translation through αw = ax / r. Substituting this relationship and rearranging for Fx gives:
Body analysis
Focusing now on the body, the motors apply a reaction torque equal in magnitude and opposite in direction to the torque applied to the wheels. Using the same coordinate axes with the origin at the wheel axle, gravity acting through the body's centre of mass produces a positive clockwise moment, opposing the motor reaction torque.
Meanwhile, the wheels drive the axle forward and accelerate the robot linearly. The wheel force is transmitted to the body through the axle, so it produces no direct moment about that point. At first glance, this leaves only gravity and the motor reaction torque contributing to body rotation. However, the selected frame follows the accelerating axle and is therefore non-inertial. To account for the frame's acceleration, we introduce a pseudo-force −mbax at the body's centre of mass, acting opposite to the axle acceleration. Taking moments about the axle and using the geometry shown in the diagram gives:
We now have two independent equations, Equations (2) and (4), relating the applied motor torque Tmotor to three unknown responses: the driving force Fx, axle acceleration ax, and body angular acceleration αb. Two equations are not enough to determine all three, so we need a third independent relationship that connects the wheel and body motion.
Horizontal and vertical force balance
To do this, we consider the robot as a whole and analyse the external forces acting in the horizontal and vertical directions.
The only external horizontal force is the driving force Fx at the wheel–ground contact. This force accelerates both the wheels and the body horizontally, so the horizontal force balance can be written as:
In the vertical direction, the upward ground reaction N opposes the total weight of the wheels and body. With no other external vertical forces, and noting that the wheels have no vertical acceleration, the vertical force balance can be written as:
To connect the body's rotation to the robot's motion, we examine the kinematics of the body's centre of mass in the stationary ground frame. Its horizontal and vertical acceleration components are denoted by ab,x and ab,y.
With the axle at horizontal position x and the body centre of mass a distance h from it, the body's horizontal position is x + h sin(θb). Differentiating this position twice with respect to time gives its horizontal acceleration:
The axle remains at a fixed height while the wheels stay in contact with the ground. The body's vertical position relative to the axle is h cos(θb), so differentiating twice gives:
Substituting the horizontal body acceleration from Equation (7) into Equation (5) and collecting terms gives the complete horizontal force balance. This provides the third independent relationship needed to close the system, linking the driving force Fx to the axle's horizontal acceleration ax and the body's angular acceleration αb.
Similarly, substituting the vertical body acceleration from Equation (8) into Equation (6) gives the expanded vertical force balance, which allows us to determine the normal force N needed to evaluate the earlier no-slip constraint, Constraint 1, and the upcoming ground-contact constraint, Constraint 2.
The coupled model
We can now combine the wheel torque balance in Equation (2), the body pitch balance in Equation (4), and the horizontal force balance in Equation (9). These provide three equations for Fx, ax, and αb. Eliminating the auxiliary contact force leaves two coupled equations for the two accelerations:
For a given motor torque and initial conditions for the body state variables θb and ωb, the equations above allow us to calculate the accelerations ax and αb. We then use αb to update the body's angular velocity and angle over a small time step. The updated state becomes the starting point for the next calculation, allowing us to iteratively simulate how the body's motion and the forces in the system evolve over time.
From equations to a capability map
Simulation and capability sweep
The coupled model, together with the motor torque–speed relationship, gives us a system of equations with which we can simulate the robot's behaviour across a range of stall torques and no-load speeds. These parameters are commonly listed for commercially available motors, making them useful for comparing potential candidates. For each wheel diameter, we can create a two-dimensional capability map and overlay real motors using their published specifications to assess their suitability for the robot.
To run the simulation, we first need to define the initial conditions and make provisional estimates of the robot's mass properties and geometry. The parameters used are:
- Body mass
- mb = 0.200 kg
- Combined wheel mass
- mw = 0.050 kg for both wheels
- Total modelled mass
- 0.250 kg (250 g)
- Axle-to-body-COM distance
- h = 35.5 mm
- Body pitch inertia
- Jb = mbh2, using a point-mass estimate
- Wheel inertia
- Jw = ½mwr2, recalculated for each radius
- Ground parameters
- μ = 0.8; g = 9.81 m/s2
- Initial state
- 90° from upright; zero initial linear and angular velocity; axle position starts at zero
- Wheel diameters
- 90, 100, and 110 mm
- Stall torque per motor
- 0.5–3.0 kgf·cm, in increments of 0.5 kgf·cm
- No-load speed
- 150–400 rpm, in increments of 50 rpm
- Integration
- Semi-implicit Euler with a fixed 0.05 ms time step
Model assumptions and scope
The model assumes planar motion, equal motor torque, identical wheels, level rigid ground, rolling contact, and a rigid chassis-fixed assembly. Mass properties and friction are provisional. Internal motor and gearbox inertia, drivetrain loss, backlash, tire compliance, electrical motor dynamics, current limits, and thermal effects are omitted. A run also stops if gearbox-output speed reverses, outside the present motor model's operating range.
The 90° release assumes that the body can rotate freely about the axle. An upright crossing measures open-loop mechanical capability. Closed-loop balancing and physical self-righting from a floor-contact configuration require separate analysis and validation. The motor markers are coarse datasheet cross-references, not product-specific simulations.
With these parameters, we can simulate the robot's motion for each motor and wheel combination. A combination is considered successful only if the body reaches upright while satisfying our specified constraints throughout the manoeuvre. In particular, the wheels must remain within the available friction limit, as defined by Constraint 1, and maintain contact with the ground, as required by Constraint 2. Checking these conditions allows us to identify combinations with sufficient mechanical capability under the assumptions of the model.
Running all 108 combinations produces the following capability maps, one for each wheel diameter. Successful combinations are coloured by the time taken to reach upright: green indicates quicker crossings, through yellow to red for slower crossings. Grey cells identify unsuccessful combinations. The published specifications of real motors are overlaid to help identify suitable candidates.
Choice rationale
Motor and wheel selection
Based on the robot's estimated size, the sweep covered wheel diameters of 90, 100, and 110 mm. The maps showed good results with 90 mm wheels and, with the motor manufacturer offering readily available wheel options around this size, the #1435 90 × 10 mm wheel pair was selected. These wheels mount directly onto the output shafts of all the motor candidates, simplifying the mechanical design and assembly for any of the motor choices and avoiding the need for extra hubs or adapters.
Among the motor candidates, both #5165 and #5137 mapped to successful crossings across all three wheel diameters. The #5165 was selected because its nearest map point sat further from the simulated fall region for the selected 90 mm wheel diameter.
02 / Electronics design
The motor selection becomes the electrical design envelope.
The selected motors establish a 6 V operating point and a 1.6 A theoretical stall current per channel. The battery interface spans 9.0 V to 12.6 V, feeding a TPS566242 buck converter. A diode isolates the protected motor rail from reverse current during braking and reversal, while bulk capacitance, a bleed resistor, and an active dump path manage regenerative rail rise.
Each motor channel uses a DRV8702-Q1 gate driver, four CSD16340Q3 N-channel MOSFETs, and a 100 milliohm Kelvin-sensed shunt. This external-MOSFET topology was chosen primarily to distribute heat across several discrete components and their associated PCB copper planes, providing a larger area for heat dissipation. The initial current-chopping threshold is 1.4 A. Physical pull-downs on nSLEEP keep the drivers disabled until the controller has configured PWM, direction, sensing, and fault inputs.
Electronics schematic — Revision 2
3 sheets · Power · Motor Drive · Functional
Lessons learned and Revision 3
-
Secure components before ordering the PCB
Critical components should be ordered before the PCB is sent for manufacture. This leaves time to choose available replacements and update the design if a part has a long lead time, rather than having a finished board waiting for components.
-
Plan assembly around fine-pitch packages
The STM32WB BLE module's small pads made reliable solder-paste application through the stencil difficult. During assembly, manually applying solder under a microscope camera and positioning the module with hot air proved easier to control. Repeated reflow-oven passes also damaged one microcontroller module, highlighting the need to limit repeated thermal exposure.
For future boards, the intended approach is to reflow the standard components first, then fit challenging fine-pitch devices separately using microscope-assisted solder application and controlled hot-air heating. The heating profile and number of rework cycles should remain within each component's assembly limits.
-
Allow for an alternative IMU
Revision 3 should connect pins 2 and 3 of the LSM6DSOTR footprint to VDDIO to support the Bosch Sensortec BMI270 as an alternative. On Revision 2, fitting the BMI270 would require insulating these pins from the grounded pads. Accounting for this during layout would make substitution easier, with the corresponding firmware support.
-
Choose the battery during the electrical design
Battery selection should happen alongside the electronics design rather than being left until mechanical design and assembly. Its dimensions, mass, voltage range, connectors, and charging requirements all influence the PCB and the rest of the robot.
-
Use a compact internal battery connector
The barrel jack provides a convenient connection for an external power supply, but its outward-facing form is less suitable for a battery housed inside the robot. A compact internal connector, such as a keyed three-position vertical battery header with a matching cable connector, would make the wiring cleaner and reduce the space occupied by the power connection.
The connector should be selected alongside the battery, with suitable current capacity, a keyed housing to prevent reversed connection, and secure retention. Its pin assignment would depend on the selected battery.
-
Reserve keepout areas around PCB mounting holes
A capacitor near the top-right mounting hole interferes slightly with the mounting hardware, making the mechanical design more awkward than necessary. Future revisions should reserve component keepout areas around each mounting hole, allowing sufficient clearance for screw heads, washers, standoffs, and assembly-tool access. Defining these clearances during PCB layout would simplify mounting the board and avoid having to design the support structure around nearby components.
-
Give USB-C a charging role
The USB-C connection is useful for testing and will support firmware development with Zephyr, but it currently provides no battery-charging function. Revision 3 should explore using the same connector for both the development interface and onboard battery charging, giving it a practical role during normal use of the robot.
The charging circuitry should be designed around the selected battery's chemistry, cell count, and charging requirements, with power-path management considered if the robot is intended to operate while connected. This would allow charging and firmware development through a single accessible connector.
-
Revisit the battery and power-supply architecture
A lower-voltage battery architecture around 6 V should be explored, using buck–boost regulation where needed. The aim would be to reduce battery size and mass. A lighter robot could also make a medium-power motor more attractive, building on the viable combinations identified in the capability maps and checking them again with the revised mass estimates.
-
User input and Bluetooth advertising
The current design has no dedicated user button because all available GPIO pins on the STM32WB module are allocated. Bluetooth advertising will therefore be started through a command in the UART shell, accessed over USB-C.
A future revision could revisit the controller choice and pin allocation to accommodate a user button. One option would be to replace the integrated module with a discrete Bluetooth-capable microcontroller offering more exposed GPIO pins and an external antenna, providing greater flexibility for user inputs and future expansion.
03 / Mechanical design
The physical architecture follows the assumptions of the model.
The robot is arranged around a vertically mounted PCB above the wheel axle, with the motors low and coaxial with the wheels. This keeps the main electronics platform close to the balancing axis while leaving a compact mechanical structure to connect the motors, wheels, battery, and board.
The CAD assembly brings the PCB, battery, motors, and wheels together within a compact enclosure with removable front and rear covers. The body is intended for additive manufacturing, with internal features to locate the components and provide access during assembly.
Motor mounting
The lower body incorporates shaped seating and retention features to hold the motors in place and align their output shafts with the wheels. The cutaway view exposes this arrangement, showing how the motor supports are integrated into the chassis.
PCB housing
The PCB is housed vertically towards the rear of the enclosure. The rear exploded view separates the board and rear cover from the body, showing their positions relative to the internal supports and mounting points.
Battery housing
The battery occupies the space in front of the PCB, with internal features locating it within the body and a removable front cover closing the enclosure. The front exploded view shows the battery's intended position and how it fits into the housing.
Internal layout and mass alignment
The side exploded view brings these interfaces together: the rear cover and PCB sit on one side of the central enclosure, with the battery and front cover on the other. The transparent assembled view then shows how these parts fit together inside the body.
The internal components were positioned to bring the assembled robot's centre of mass onto the vertical centre plane passing through the wheel axle. Preliminary CAD mass-property calculations place the centre of mass within approximately 1 mm of the vertical line through the axle when the robot is upright. This provides an initial estimate of the mass alignment, to be checked against the physical assembly.
The physical assembly's mass, centre of mass, inertia, and stiffness will be compared with the assumptions used in the capability sweep so the mechanical design can inform the next model iteration.
04 / Firmware
A Zephyr RTOS platform for sensing, safety, and telemetry.
Zephyr RTOS will provide the firmware structure for bringing together IMU acquisition, encoder feedback, motor control, fault handling, and Bluetooth telemetry. The STM32WB platform provides the processing and wireless capability required to observe internal variables without constraining the robot with a data cable.
The firmware architecture will include safe startup and shutdown, PWM and direction control, current and fault monitoring, sensor acquisition, and a telemetry path for measurements used during tuning and system analysis.
05 / Control
Balancing begins when the hardware can be measured.
The control work will combine IMU data, wheel encoder feedback, and motor-current information to estimate the robot state and command the motor torque required to remain upright. The control strategy will be developed alongside measured data from the assembled platform rather than from the initial model alone.
This stage will explore orientation estimation, feedback design, controller tuning, recovery behaviour, and the relationship between electrical current limits and usable balancing torque.


