Self-Balancing Lego Robot: From Dynamic Modeling to Hardware Implementation

Designed and built a self-balancing robot using Lego Spike, starting with deriving equations of motion and formulating a state-space model. Validated the design through MATLAB simulations, static/dynamic sensor testing, and iterative hardware prototyping. Achieved close alignment between simulated predictions and physical performance, demonstrating robust control system integration. The robot was built using two motors, two wheels, and the LEGO Spike hub, which includes a built-in battery and an IMU (gyroscope + accelerometer). All code runs directly on the hub. A picture of the hardware setup is shown below.

Project Image

Modelling the System

The dynamic model was derived using Newton’s laws, summing forces and moments. The system considers four states: position, linear velocity, tilt angle, and angular velocity. A small-angle approximation was applied (sin(θ)≈θ, cos(θ)≈1), introducing an error below 1.5% for angles between ±15°. The resulting state-space representation was used both for simulation and control design.

Control Approach

Simulations

Simulation Image 1 Simulation Image 2 Simulation Image 3

Challenges Encountered

Final Results

The LQR controller consistently stabilized the robot for several minutes (battery-dependent). In contrast, the PID controller showed inconsistent behavior, with brief stabilization (up to a minute) depending on the surface and tuning.

Video GitHub Repository