Two-wheel self-balancing robot

Balancing robots work like an inverted pendulum, the same idea behind Segways and rocket landing. Building one is the classic way to learn sensor fusion and closed-loop control on real, unstable hardware.

ESP32Major projectMECH, ECE, EEE
ESP32MPU605tilt 26°Featured
Parts
₹4,630
Build
14 weeks
Board
ESP32
Objectives
5

Block diagram

Inputs
  • MPU605
    MPU6050Measures tilt and tilt rate
Controller
  • ESP32
    ESP32Fuses sensors and runs PID
Power
  • +
    3S Li-ion pack11.1 V for motors
Outputs
  • Two A4988 driversDrive the steppers
  • Two NEMA 17 steppersTurn the wheels
  • PHONE
    Phone appShows readings and sends commands over Bluetooth+

Objectives

  1. To estimate tilt angle with a complementary filter.
  2. To balance upright with PID for at least 5 minutes.
  3. To move forward, back and turn on command without falling.
  4. To report the tuning steps and the response to a push.
  5. To monitor and control the system from a phone over Bluetooth.

Parts and specifications

Total about ₹4,630 at typical online prices. Tap a part for its pinout and datasheet notes.

Design calculations

Complementary filter

θ = 0.98 × (θ + gyro × dt) + 0.02 × accel_angle, with dt = 4 ms. The gyro gives smooth short-term angle and the accelerometer corrects long-term drift.

Loop rate

Run the balance loop at 250 Hz (4 ms). The pendulum's falling time constant √(L/g) for L = 0.15 m is about 0.12 s, so 250 Hz is 30 updates per time constant.

Output to speed

PID output sets wheel speed in steps/s. Max 3000 steps/s at 1/8 microstep = 1600 steps/rev → 1.9 rev/s × π × 80 mm ≈ 0.47 m/s.

Starting gains

Kp ≈ 20, Kd ≈ 0.6, Ki ≈ 0 to start; raise Kp until it holds up and oscillates, then add Kd to damp it.

Pin map

SignalPin or connection
MPU6050 SDA / SCL / INTGPIO 21 / 22 / 19
Left driver STEP / DIRGPIO 25 / 26
Right driver STEP / DIRGPIO 32 / 33
Drivers ENABLE (shared)GPIO 27
Motor supply3S pack 11.1 V to both A4988 VMOT with 100 µF each

Firmware outline

  1. Read the MPU6050 at 250 Hz; compute the angle with the complementary filter.
  2. Inner loop: PID on angle error → wheel speed.
  3. Outer loop (slower, 25 Hz): PID on wheel speed → angle set point, so the robot does not drift away.
  4. Generate step pulses from the speed with a hardware timer for each motor.
  5. Disable motors if the angle passes ±30° (fallen); add steering by a speed difference from Bluetooth commands.

Test plan

TestExpected result
Stand upright, hands offBalances 5 minutes
Small pushRecovers within 2 s
Forward and turn commandsMoves without falling
Angle logged during a pushResponse plotted

These rows start your results table when you plan the project.

Viva practice

Say your answer out loud first, then check. The first 2 answers are free; Maker opens all 5.

1. Why is this an inverted pendulum?
2. Why combine gyro and accelerometer?
3. Why steppers here?
4. What is a cascaded loop?
5. What happens if the loop is too slow?

Make it yours

One or two upgrades for a mini project, two to four for a major one.

Build these first

Datasheets and sources to cite

  • InvenSense MPU-6050 datasheet
  • Allegro A4988 datasheet
  • Ogata, Modern Control Engineering (inverted pendulum)

About this project

How much does the two-wheel self-balancing robot project cost?

About ₹4,630 for the parts in the base list: ESP32 DevKit V1, MPU6050 accelerometer and gyro, A4988 stepper driver × 2, NEMA 17 stepper motor × 2, Li-ion 18650 pack 3S2P (11.1 V), 3S BMS 20 A, LM2596 buck converter, 470 µF 35 V electrolytic × 2, Wheels 80 mm with 5 mm hubs × 2, Three-deck frame and spacers.

How long does it take?

About 12 weeks for the base project, built on an ESP32. Each upgrade adds time.

Which branches is it suitable for?

Mechanical / Mechatronics, Electronics and Communication, Electrical and Electronics, as a major project.

Which pins and design calculations does it use?

MPU6050 SDA / SCL / INT on GPIO 21 / 22 / 19; Left driver STEP / DIR on GPIO 25 / 26; Right driver STEP / DIR on GPIO 32 / 33. Worked calculations on this page: complementary filter, loop rate, output to speed, starting gains.

How do I test it?

Stand upright, hands off: Balances 5 minutes. Small push: Recovers within 2 s. Forward and turn commands: Moves without falling. Angle logged during a push: Response plotted.

What are the objectives?

To estimate tilt angle with a complementary filter. To balance upright with PID for at least 5 minutes. To move forward, back and turn on command without falling. To report the tuning steps and the response to a push.