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.
- Parts
- ₹4,630
- Build
- 14 weeks
- Board
- ESP32
- Objectives
- 5
Block diagram
- MPU6050Measures tilt and tilt rate
- ESP32Fuses sensors and runs PID
- 3S Li-ion pack11.1 V for motors
- Two A4988 driversDrive the steppers
- Two NEMA 17 steppersTurn the wheels
- Phone appShows readings and sends commands over Bluetooth+
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.
- To monitor and control the system from a phone over Bluetooth.
Parts and specifications
- ESP32 DevKit V1Dual-core 240 MHz, 520 KB SRAM, Wi-Fi + Bluetooth, 3.3 V logic, 12-bit ADC (use ADC1 pins when Wi-Fi is on)₹450Buy
- MPU6050 accelerometer and gyro3-axis ±2 to ±16 g and ±250 to ±2000 °/s, 16-bit, I²C 0x68, motion interrupt pin₹150Buy
- A4988 stepper driver8 to 35 V, 1 A per phase without heatsink (2 A with cooling), up to 1/16 microstep, set current with Vref₹240Buy
- NEMA 17 stepper motor1.8° per step (200 steps/rev), 1.5 to 1.7 A per phase, about 4 kg·cm holding₹1,300Buy
- LM2596 buck converter4 to 40 V in, 1.25 to 37 V out, up to 2 A continuous (3 A peak), about 85% efficient₹90Buy
- × 2Wheels 80 mm with 5 mm hubs₹300
- Three-deck frame and spacers₹400
Total about ₹4,630 at typical online prices. Tap a part for its pinout and datasheet notes.
Design calculations
θ = 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.
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.
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.
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
| Signal | Pin or connection |
|---|---|
| MPU6050 SDA / SCL / INT | GPIO 21 / 22 / 19 |
| Left driver STEP / DIR | GPIO 25 / 26 |
| Right driver STEP / DIR | GPIO 32 / 33 |
| Drivers ENABLE (shared) | GPIO 27 |
| Motor supply | 3S pack 11.1 V to both A4988 VMOT with 100 µF each |
Firmware outline
- Read the MPU6050 at 250 Hz; compute the angle with the complementary filter.
- Inner loop: PID on angle error → wheel speed.
- Outer loop (slower, 25 Hz): PID on wheel speed → angle set point, so the robot does not drift away.
- Generate step pulses from the speed with a hardware timer for each motor.
- Disable motors if the angle passes ±30° (fallen); add steering by a speed difference from Bluetooth commands.
Test plan
| Test | Expected result |
|---|---|
| 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 |
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.
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.
