Skip to content

Balancing Robot

A two-wheeled robot that stands up by itself. It is an inverted pendulum, like a broom balanced on your hand: a gyro and accelerometer measure how far it is leaning, and your code drives the wheels under the fall to catch it. By the last class it stands still on its own, takes a push and recovers, and rolls forward and back when you tell it to.

How it works

flowchart LR
    IMU["MPU6050<br/>accelerometer + gyro"] -->|I²C| F["Complementary filter<br/>(one clean angle)"]
    F --> PID["PID controller<br/>(knobs set Kp, Ki, Kd)"]
    PID -->|PWM + direction| DRV["TB6612 driver"]
    DRV --> M["Two 12 V gear motors"]
    M -->|the robot tilts| IMU
    M --> ENC["Encoders<br/>(count wheel turns)"]
    ENC --> SPD["Outer loop<br/>(measured wheel speed)"]
    SPD -->|target angle| PID

The MPU6050 gives two kinds of reading, and neither is good enough alone. The accelerometer knows which way is down but jumps with every bump. The gyro is smooth but slowly drifts. A complementary filter blends them into one angle you can trust. A PID controller turns that angle into how hard to push the motors, and a TB6612 driver turns that into current for two 12 V gear motors. Each motor has an encoder that counts how far its wheel has turned, so the robot knows what its wheels actually did. In Class 5 a second, slower loop uses those counts to keep the robot from wandering across the room.

Almost every idea in feedback control (noise, filtering, P, I, D) shows up in how the robot behaves. You'll see each one before you fix it.

Try it first in the simulator

The simulator runs this same program on a virtual robot, with live sensor readings and plots. Use it to see what each class changes before you build.

The six classes

Class By the end of class
1 · Read the tilt You read the MPU6050 and plot the accelerometer angle and gyro rate while tilting it.
2 · Fuse it, spin the wheels One fused angle within 2° of a protractor, wheels you can count, and two wheels that chase the tilt.
3 · Build the body The robot is built, shuts off when it falls, and catches itself with P control (then wobbles).
4 · PID with the knobs Tuned with three knobs, it stands untouched for 10 s, then 60 s.
5 · Stay put, then move It balances in one spot and rolls forward and back on command.
6 · Showcase Off the tether: it survives the push test and carries a load.

Before Class 1

  • Install Teleplot. In VS Code, open Extensions and install Teleplot (by alexnesnes). It plots numbers from the serial port. You'll use it every class.
  • Check your GY-521 boards. If the header pins came loose in the bag, they need to be soldered on. Ask Dr. Alvear.
  • Start the body in Onshape. Three flat decks, about 10 cm × 15 cm, with a hole near each corner for M3 threaded rod. The bottom deck also needs to hold the two gear motors and their brackets. Measure your motors, brackets and wheels first. You'll build it in Class 3.

Safety, every class

  • The wheels can spin fast. Keep fingers, hair and cables clear of them whenever the motor supply is on.
  • The power switch stays within reach. If anything does something you didn't expect, switch off the motor power first, then think.
  • The tilt cutoff goes in before the robot stands on the floor (Class 3). Past ±45°, it has fallen, and the motors stop.
  • No LiPo batteries. The motors run from a 12 V wall adapter during tuning and a USB-C power bank at the end.

Credits

  • Teleplot by alexnesnes plots your data in VS Code.
  • Two gear motors with encoders and an MPU6050 is a common design among hobby balancing robots. The code is yours.