Self-Balancing Inverted Pendulum Robot (PID + Kalman)
Master non-linear control systems by creating a two-wheeled balancing robot using an MPU6050 IMU and high-speed stepper motors.
The ultimate classic robotics challenge: stabilizing an inherently unstable inverted pendulum robot using high-speed IMU complementary / Kalman filtering, cascaded PID position and angle controllers, and zero-backlash NEMA 17 stepper motors.
Bill of Materials (BOM) & Components
| Component | Specifications | Qty | Estimated Cost |
|---|---|---|---|
| NEMA 17 Stepper Motors (0.4Nm Torque, 1.8°) | Instantaneous response with zero gearbox backlash | 2 | $26 |
| TMC2209 SilentStepStick Stepper Drivers | 1/256 microstepping with SpreadCycle high-torque mode | 2 | $16 |
| MPU6050 6-Axis Gyroscope & Accelerometer Module | I2C 400kHz fast mode with hardware DLPF filter | 1 | $4 |
| ESP32 DevKit V1 Microcontroller | 240MHz 32-bit dual-core for 500Hz deterministic PID execution | 1 | $7 |
| 3S 11.1V 1500mAh 30C LiPo Battery | Low internal resistance for high-acceleration current spikes | 1 | $18 |
Electrical & Architecture Summary
ESP32 samples MPU6050 over I2C at 500Hz. Computes pitch angle via Kalman filter. Inner loop computes balancing torque, outer loop stabilizes position and responds to Bluetooth joystick commands.
Step-by-Step Assembly & Configuration
Frame Assembly & Mass Distribution Optimization
Mount the battery at the top of the chassis. Counter-intuitively, higher center of mass increases the moment of inertia and makes inverted pendulum stabilization easier.
- Assemble 3-tier acrylic / 3D printed stack.
- Mount MPU6050 exactly on the wheel axle center line to eliminate centripetal acceleration artifacts.
Kalman Filter Implementation & 500Hz Sensor Loop
Fuse accelerometer tilt angle with integrated gyroscope rate to calculate drift-free true pitch angle.
- Calibrate gyro zero-rate bias offsets at startup.
- Verify filter output on Serial Plotter: response must have zero lag and clean filtering.
Cascaded PID Controller Tuning
Tune the fast inner pitch loop (Kp_angle, Kd_angle) until the robot balances stably in place, then tune the outer position/velocity loop.
- Increase Kp until robot oscillates slightly.
- Add Kd to dampen oscillation until balance is rock-solid.
- Add position P-loop to prevent the robot from drifting across the room.