← Posts
robotics

How Robots Balance on Two Wheels: Sensors, PID, and the Inverted Pendulum

By Kedar Chitnis · Crew Member

Originally published on Instagram

A self-balancing robot on two wheels looks like it should fall over. Physically, it is an inherently unstable system -- the center of mass sits above the contact points, and gravity is always trying to tip it. Yet these robots stay upright, correcting continuously thousands of times per second. Understanding how requires three ideas: sensing, computing, and acting.

Step 1: Sensing

The robot needs to know, at every moment, how far it has tilted from vertical. This is the job of an IMU (Inertial Measurement Unit) combining two sensor types.

An accelerometer measures linear acceleration. When the robot is upright and stationary, gravity pulls straight down. As it tilts, the accelerometer detects the changing direction of that gravitational force, revealing the tilt angle.

A gyroscope measures rotational velocity -- how quickly the tilt angle is changing.

Neither sensor is reliable alone. Accelerometers are noisy and sensitive to vibration. Gyroscopes drift over time. A complementary filter or Kalman filter combines their readings to produce a stable, accurate estimate of tilt and tilt rate.

Step 2: Computing -- The PID Controller

With accurate tilt data, the microcontroller runs a PID (Proportional-Integral-Derivative) controller. PID is one of the most widely used control algorithms in engineering, from industrial machinery to aircraft autopilots.

The controller compares the robot's current state to its target (upright, zero tilt). The difference is the error. It responds in three ways:

  • Proportional (Kp): Responds proportionally to the current error. More tilt equals more correction. Provides immediate response but causes oscillation if set too aggressively.
  • Integral (Ki): Accumulates error over time. If the robot has been slightly tilted in one direction for a period, the integral term adds a correction for this sustained offset. Eliminates steady-state error.
  • Derivative (Kd): Responds to how quickly the error is changing. If tilt is increasing rapidly, the derivative applies extra correction to anticipate the trajectory. Acts as a damper, reducing overshoot.

Tuning the three gain values is the practical engineering challenge. Too much proportional gain and the robot oscillates. Too much derivative and it becomes jerky.

Step 3: Acting

The PID output is a motor command. The motors move the wheels in the direction the robot is falling: tip forward, wheels move forward to bring the base back under the center of mass. Tip backward, wheels reverse.

This is the inverted pendulum principle. Rather than fighting the fall, the robot chases it -- continuously repositioning its base beneath its own center of gravity. At equilibrium the robot is falling in every direction simultaneously, with corrections cancelling each fall before it develops.

The full loop -- sense, compute, act -- runs at hundreds of cycles per second, which is why the motion appears smooth even though it is fundamentally a continuous recovery from instability.