← Back to Projects
Self-Balancing Robot: ESP32 Hardware Build
January 2025 · Team Side Project
ESP32PIDEmbedded C++

Overview

A side project with a small team, started in January 2025 for fun: to see a balancing controller work on real hardware. The result is a two-wheeled robot on a laser-cut chassis with 3D-printed wheels, driven by an ESP32.

System

  • Controller: ESP32
  • Sensor: MPU6050 IMU (accelerometer + gyroscope)
  • Motor driver: BTS7960
  • Power: 12 V Li-ion battery
  • Control: a single PID loop on the tilt (pitch) angle. There is no multi-state control, so velocity and position are not regulated.

The signal path is IMU → complementary filter → tilt angle → PID → BTS7960 → motors.

My Contribution

The team built the chassis and electronics together. I focused on the controller firmware, which I developed step by step:

  • Motor testing: verified motor direction and speed control through the BTS7960 before adding any feedback.
  • IMU data acquisition: read the raw MPU6050 accelerometer and gyroscope data on the ESP32.
  • Filtering: applied a complementary filter to fuse both sensors into a stable tilt angle.
  • PID control and tuning: implemented the PID loop on the tilt angle and tuned it on the real robot: raise Kp until the robot oscillates, add Kd to damp it, then add Ki to remove the remaining offset.

Result

The robot balances on its own and recovers from external disturbances, driven by joystick input.

Limitations and Lessons

  • Surface dependent: the robot only balances on high-grip surfaces such as a rubber mat. On a smooth floor it fails to balance.
  • Motor deadzone not compensated: the motors do not respond to small control outputs, which limits how finely the controller can correct small tilt errors. Adding deadzone compensation is the next improvement.
  • Lesson: a good tilt estimate and a well-tuned PID are not enough on their own. Actuator behavior, such as deadzone and wheel grip, strongly affects real balancing, and a simple simulation would not show it.

Later, the same problem was studied in more depth in a university course project, with a full dynamic model and multi-state LQR-PID control in simulation: Self-Balancing Robot: Hybrid LQR-PID Control in Simscape.