Advertisement
Intermediate Time: 2–3 weeks Robotics

Self-Balancing Robot (Inverted Pendulum)

Build a two-wheeled self-balancing robot using MPU6050 IMU, Kalman filter, and PID control — a classic control theory project.

PIDIMUMPU6050Inverted PendulumControl TheoryArduino
DifficultyIntermediate
Duration2–3 weeks
Components10 items
Steps7 steps

Introduction

Build a two-wheeled self-balancing robot using MPU6050 IMU, Kalman filter, and PID control — a classic control theory project. This comprehensive guide covers everything from design through implementation, testing, and deployment.

Theory & Background

A self-balancing robot is an inverted pendulum — inherently unstable. Without control, it falls. The control system continuously measures the tilt angle from vertical, predicts the falling direction, and accelerates the wheels in that direction to bring the center of mass back over the wheel base. This is the same principle used in Segway personal transporters and modern hoverboards.

Advertisement

Components & Requirements

10 components required for this project.

#ComponentPurposeQty
1Arduino Nano/UnoReal-time PID control loopx1
2MPU6050 6-DOF IMU (Gyro+Accel)Tilt angle measurementx1
3L298N Motor DriverDriving two DC gear motorsx1
4DC Gear Motors with Encoders (12V, 300RPM)Drive wheels with speed feedbackx2
565mm Rubber WheelsTraction wheelsx2
611.1V 3S LiPo (1300mAh)Compact power sourcex1
7Custom 3D-Printed or Laser-Cut ChassisTall, narrow body structurex1
8Voltage Regulator (LM2596 buck)5V for Arduino from LiPox1
9Bluetooth HC-05Remote speed setpoint and tuningx1
10On/Off Power Switch (rated > 10A)Safe power cutoffx1

Step-by-Step Implementation

Follow these 7 steps carefully.

1
Understanding the Inverted Pendulum Problem

A self-balancing robot is an inverted pendulum — inherently unstable. Without control, it falls. The control system continuously measures the tilt angle from vertical, predicts the falling direction, and accelerates the wheels in that direction to bring the center of mass back over the wheel base. This is the same principle used in Segway personal transporters and modern hoverboards.

2
Complementary Filter for Tilt Angle

MPU6050 provides raw accelerometer and gyroscope data. Accelerometer measures absolute angle (arctan(Ax/Az)) but is noisy from motor vibration. Gyroscope measures angular rate (degrees/second) — integrate over time for angle, but drifts. Complementary filter combines both: angle = 0.98 × (angle + gyro_rate × dt) + 0.02 × accel_angle. The 0.98/0.02 ratio means 98% trust in gyro for fast changes, 2% correction from accelerometer to prevent drift.

3
Kalman Filter (Advanced Alternative)

Kalman filter is the optimal linear state estimator. State vector: [angle, gyro_bias]. Prediction step uses gyroscope measurements. Update step corrects with accelerometer. The filter tracks its own uncertainty (covariance matrix) and weights measurements accordingly. Kalman filter outperforms complementary filter in noisy environments. Use the Kalman library for Arduino: Kalman kalmanX; angle = kalmanX.getAngle(accel_angle, gyro_rate, dt).

4
PID Controller Design

setpoint = 0° (vertical). error = setpoint - measured_angle. PID output drives motor speed: forward to catch falling backward, reverse to catch falling forward. Critical: control loop must run fast (> 100 Hz) because the pendulum falls quickly. Use a 10ms control loop (100Hz). Start with only P control (Kp=15–25), then add D (Kd=0.5–2) to reduce oscillation, add small I (Ki=50–150) to correct for mechanical imperfections.

5
Chassis Design Considerations

Critical mechanical factors: low center of mass (CoM) position improves controllability — too high makes it too sensitive, too low makes balancing impossible (CoM below wheel center = stable, not self-balancing). Wheel radius determines speed-to-motor-PWM ratio. Heavy motors low on chassis, battery centered. Symmetric weight distribution — calibrate IMU mounting angle to match true vertical when balanced.

6
Remote Control and Setpoint Modification

Connect HC-05 Bluetooth. Phone app (MIT App Inventor or Serial Bluetooth Terminal): send 'F', 'B', 'L', 'R', 'S' commands. Implement: setpoint += 0.3 for forward tilt (drives robot forward as it catches up), setpoint -= 0.3 for backward. For turning: add differential speed offset to one motor. This setpoint manipulation is more stable than trying to overcome PID with direct motor commands.

7
Tuning Process and Testing

Tuning order: Kd first (prevents oscillation during tuning of P), then Kp (increase until robot stands for 3+ seconds without going out of control), then Ki (small value to handle constant disturbances). Test on flat and slightly inclined surfaces. Test with 100g weight added high (harder) and low (easier) on chassis. Good tune: robot should absorb a gentle push and return to balance within 1–2 oscillations.

Code & Implementation

Core code for balancing_robot.ino:

balancing_robot.ino C/C++
#include <MPU6050_tockn.h>
#include <Wire.h>
MPU6050 mpu(Wire);

#define ENA 9 #define IN1 7 #define IN2 8
#define ENB 6 #define IN3 4 #define IN4 5

float Kp=25, Ki=120, Kd=1.2;
float error=0, prev_error=0, integral=0;
float setpoint = 0.5; // Tune this for balance point

void setMotors(int L, int R) {
  L = constrain(L,-255,255); R = constrain(R,-255,255);
  analogWrite(ENA, abs(L)); analogWrite(ENB, abs(R));
  digitalWrite(IN1, L>0); digitalWrite(IN2, L<0);
  digitalWrite(IN3, R>0); digitalWrite(IN4, R<0);
}

void setup() {
  Wire.begin(); mpu.begin(); mpu.calcGyroOffsets(true);
  pinMode(IN1,OUTPUT); pinMode(IN2,OUTPUT); // etc.
}

void loop() {
  static unsigned long lastTime = 0;
  mpu.update();
  float angle = mpu.getAngleX(); // Tilt angle in degrees

  float dt = (millis() - lastTime) / 1000.0;
  lastTime = millis();

  error = setpoint - angle;
  integral = constrain(integral + error * dt, -40, 40);
  float output = Kp*error + Ki*integral + Kd*(error-prev_error)/dt;
  prev_error = error;

  if(abs(angle) > 45) { setMotors(0,0); integral=0; return; } // Fallen
  setMotors(-output, -output); // Negative because falling forward needs backward motors
  delay(10);
}

Testing & Troubleshooting

Test Self-Balancing Robot (Inverted Pendulum) by verifying each subsystem individually before full integration.

!
Troubleshooting Tips

Verify power voltages, check ground connections, use serial monitor for debug.

Real-World Applications

*Educational control theory demonstration
*Personal mobility device prototype
*Delivery robot base platform
*Photography stabilizer platform
*Two-wheeled inspection robot
*Autonomous indoor mapping base
*Robotic soccer player
*Human-robot interaction research

Extensions & Next Steps

  • Implement LQR (Linear Quadratic Regulator) for optimal control
  • Add GPS for outdoor autonomous navigation
  • Build a Segway-style personal transporter (scaled up)
  • Add computer vision for person-following behavior
  • Implement fall detection and graceful recovery maneuver

Interactive Playground

Coming Soon

An interactive simulator will be available here — simulate circuits and run code in-browser without hardware.

Frequently Asked Questions

How fast does the control loop need to run for stable balancing?
Minimum 50 Hz (20ms loop) for stable balancing with a well-tuned PID. Most successful implementations run at 100–200 Hz (5–10ms loop). Faster control loops enable higher derivative gains (Kd) which improve disturbance rejection and reduce oscillation. Below 30 Hz the robot typically falls because the gyroscope integration error accumulates too fast and PID response is too slow for the inverted pendulum dynamics.
Why does my robot drift forward or backward even when balanced?
Drift indicates either: (1) IMU is not mounted exactly level with the chassis vertical axis — calibrate the tilt offset angle so balanced = 0° reading, (2) unequal friction or wear in left vs right wheel/motor — add differential correction, (3) integral windup on a slight slope — the integral term accumulates to overcome gravity on an incline. Use the setpoint adjustment (slight forward/backward tilt setpoint) to compensate for mechanical imperfections.
What is the difference between a Segway and this project?
Conceptually identical — both are self-balancing two-wheeled vehicles using the inverted pendulum principle with PID control. Differences: Segway uses industrial-grade gyroscopes and accelerometers with Kalman filtering, high-power brushless motors with encoders for speed control, mechanical redundancy with dual sensors, much higher payload capacity, and advanced lean-to-steer control. This project is the educational-scale equivalent demonstrating the exact same fundamental control theory.
Advertisement