Robotics & CV·March 30, 2026·4 min read

Self-Balancing PID Robot

Complementary filter & deadzone-compensated PID motor loop.

Self-Balancing PID Robot
MCU
Arduino Nano
IMU
MPU6050 6-DOF
Driver
L298N H-Bridge
Loop
200 Hz PID

Pipeline & core implementation

Architecture Pipeline
MPU6050Comp FilterPID LoopL298N Motors
What broke first: DC gearmotors stalled below PWM 45 causing upright wobble; adding a calibrated deadzone offset eliminated limit-cycle oscillation.
balance_pid.cpp · C++
float computeMotorPwm(float angleErr, float dt) {
  integral += angleErr * dt;
  float deriv = (angleErr - prevErr) / dt;
  prevErr = angleErr;
  return kp * angleErr + ki * integral + kd * deriv;
}

Sensor fusion & 200 Hz timing

An accelerometer alone reacts cleanly at rest but picks up motor vibration, whereas integrating raw gyroscope rates drifts within seconds. Fusing 98% integrated MPU6050 gyro angle with 2% accelerometer tilt over 400 kHz I2C keeps pitch estimation responsive at a fixed 5 ms control tick.

Fixing gearmotor deadzone wobble

Small yellow DC gearmotors driven through an L298N H-bridge barely turn below PWM ~45, forcing a pure PID controller to hunt back and forth around vertical (limit-cycle oscillation). Mapping non-zero PID sign commands past the static friction threshold locked upright balance.

Two-tier Fusion 360 chassis

Modeled the full stacked frame in Fusion 360 to balance battery mass high enough for a slower natural pendulum period—giving the 200 Hz Nano loop plenty of recovery margin.

Measured benchmarks

Control loop5 ms200 Hz400 kHz I2C MPU6050

Project status & checklist

● Completed
Done
  • Two-tier stacked Fusion 360 3D CAD assembly & BOM
  • 400 kHz I2C MPU6050 complementary filter & 200 Hz PID loop
  • L298N static-friction deadzone compensation eliminating upright wobble
Needs to be done

All planned milestones complete.