Two-wheel Balancing Robot

Solo project · Mechanical design, Control systems, Mechatronics, Simulation

GitHub Repository

Objective

A two-wheeled robot balancing on its own is the classic inverted-pendulum problem, used here to work through control systems and mechatronics end to end. The goal was not just getting it to stand upright, but following the whole pipeline: mathematical modeling, controller design, and a physical build.

  • Build a Simulink model capturing the robot’s mass distribution, center of gravity, and motor behavior.
  • Design a PID loop converting pitch angle into corrective motor torque to keep the robot vertical.
  • Fabricate and assemble a physical prototype driven by live sensor feedback.
  • Build a wireless controller to retune PID gains on the fly, without reflashing firmware.
CAD Model of Robot

CAD Model of Robot

System Overview

Simulation and control design

The dynamics, weight, center of gravity, and motor torque characteristics were modeled in Simulink first, so the controller could be designed and tested before any hardware existed. A cascaded PID loop where pitch angle converts tilt into a torque command for the drive motors.

Simulink Model

Simulink Model

Hardware build

The mechanical structure was designed as a full CAD assembly, then 3D-printed and assembled. A Teensy microcontroller ran the real-time loop, reading IMU data over I2C and driving the motors via PWM. Raw accelerometer readings drift and gyroscope readings are noisy on their own, so a complementary filter combined the two into a stable angle estimate.

Final built robot

Final built robot

Wireless tuning tool

Reflashing code every time a gain needed adjusting was slow, so a separate tuning device, built from NRF24L01 radio modules and potentiometers, dialed in Kp, Ki, and Kd live while the robot was running. That replaced the flash-test-repeat cycle with real-time feedback, and serial plots of the sensor and controller output next to it made instability easy to catch quickly.

Tuner

Wireless Tuner

Design decisions

  • Used a simple complementary filter to keep computation fast and still get reliable data.
  • Added a tungsten weight at the top to increase the moment of inertia of the robot which increases the time of fall.
  • Built a wireless tuner to tune the PID gains on the go, which is much faster than modifying and reuploading the code.
  • Compact design that houses the batteries, microcontroller and other electronics.

Outcome

The robot recovered from disturbances of up to 10° in under 2 seconds, settling within 10 cm of its original position. The Simulink model tracked real hardware behavior closely enough that most tuning happened in simulation first, cutting iteration time on the physical unit.