Skip to content

Shass27/A-Self-Balancing-Robot

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

A Self-Balancing Robot

Abstract

This project presents the design and implementation of a two-wheeled self-balancing robot using an ESP32 microcontroller and MPU6050 inertial measurement unit. The system employs a PID control algorithm to maintain vertical equilibrium by continuously adjusting motor responses based on real-time orientation data. The implementation demonstrates effective closed-loop control in an inherently unstable mechanical system.

1. Introduction

Self-balancing robots represent a classic control systems problem, where an inverted pendulum must be stabilized through active feedback control. This project implements such a system using cost-effective components and demonstrates the practical application of complementary filtering and PID control theory.

1.1 Objectives

  • Achieve autonomous vertical stabilization using sensor fusion and PID control
  • Implement gyroscope calibration with persistent storage
  • Design a state machine to handle different operational modes (waiting, balancing, fallen)
  • Optimize control parameters for stable operation

2. System Architecture

The system comprises three primary subsystems: sensing, control, and actuation. Data flows from the MPU6050 sensor through the ESP32 processor to the motor driver system, forming a closed-loop control architecture.

┌─────────────┐     ┌─────────────┐     ┌─────────────┐
│   MPU6050   │────▶│    ESP32    │────▶│   Motors    │
│   Sensor    │     │ Controller  │     │  (L298N)    │
└─────────────┘     └─────────────┘     └─────────────┘
       ▲                   │                    │
       │                   │                    │
       └───────────────────┴────────────────────┘
              Feedback Loop (Tilt Angle)

3. Hardware Components

3.1 Microcontroller

  • ESP32: Main processing unit featuring dual-core processor and built-in I2C interface
  • Operating Voltage: 3.3V logic level
  • Clock Speed: 240 MHz

3.2 Sensors

  • MPU6050: 6-axis IMU (3-axis accelerometer + 3-axis gyroscope)
  • Communication: I2C protocol (SDA: GPIO21, SCL: GPIO22)
  • I2C Address: 0x68

3.3 Actuators

  • DC Motors: Two geared motors for differential drive
  • Motor Driver: L298N Dual H-bridge motor driver
  • PWM Channels: GPIO26, GPIO27 (Motor A), GPIO25, GPIO14 (Motor B)
  • PWM Frequency: 2 kHz with 8-bit resolution

4. Software Implementation

4.1 Sensor Fusion

The system employs a complementary filter to combine accelerometer and gyroscope data:

angle = α × (angle + gyroRate × dt) + (1 - α) × accAngle

Where α = 0.95, balancing the stability of accelerometer data against the responsiveness of gyroscope measurements.

4.2 PID Control Algorithm

The control system implements a Proportional-Integral-Derivative controller:

output = Kp × error + Ki × ∫error dt + Kd × dTerm

Tuned Parameters:

  • Kp = 13.2 (Proportional gain)
  • Ki = 0.75 (Integral gain)
  • Kd = 0.7 (Derivative gain)
  • Integral limit = ±60 (anti-windup)

Note: The derivative term uses the negative gyroscope rate (-gyroY/131.0) directly rather than the traditional derivative of error, providing faster response by predicting the system's angular velocity.

4.3 State Machine

The robot operates in three distinct states:

  1. WAIT_UPRIGHT: Motors disabled, waiting for angle < 5°
  2. BALANCING: Active PID control, monitoring for falls (angle > 35°)
  3. FALLEN: Motors disabled, waiting for manual recovery

4.4 Gyroscope Calibration

To compensate for sensor bias, the system performs a 2000-sample calibration during initialization. Calibration data is stored in EEPROM for persistence across power cycles, reducing startup time.

5. Project Structure

A-Self-Balancing-Robot/
├── Final_Code/
│   └── Final_Code.ino          # Main implementation
└── Sensors Testing/
    ├── Motor_test/             # Motor driver validation
    └── MPU6050_test/           # IMU sensor validation

6. Installation and Usage

6.1 Hardware Setup

  1. Connect MPU6050 to ESP32 via I2C (SDA→21, SCL→22)
  2. Wire motor driver to GPIO pins: 26, 27, 25, 14
  3. Ensure adequate power supply for motors (separate from ESP32 if possible)

6.2 Software Setup

  1. Install Arduino IDE with ESP32 board support
  2. Required libraries (built-in with ESP32 core):
    • Wire (I2C communication)
    • EEPROM (calibration storage)
  3. Upload Final_Code.ino to ESP32
  4. Open Serial Monitor (115200 baud) for diagnostics

6.3 Operation

  1. Power on the system with robot lying flat
  2. Wait for gyroscope calibration (or automatic load from EEPROM)
  3. Lift robot to vertical position (within ±5°)
  4. System automatically enters balancing mode

7. Results and Performance

The implemented system successfully achieves:

  • Stable balancing within ±5° of vertical
  • Recovery from disturbances up to 35° tilt angle
  • Fast response time through optimized PID parameters
  • Minimal steady-state error through integral control

Key Performance Metrics:

  • Balance angle threshold: ±5°
  • Fall detection threshold: ±35°
  • Control loop frequency: ~100-200 Hz
  • Motor PWM resolution: 8-bit (0-255)

License

This project is open-source and available for educational purposes.


Authors: Shass27, sumedha2932-source Platform: Arduino IDE

About

A two-wheeled self-balancing robot built with ESP32 and MPU6050, implementing sensor fusion and PID control for real-time stabilization.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages