Skip to content

I-M-Sharma/self_balancing_robot

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 

Repository files navigation

Self-Balancing Robot

A self-balancing robot using an Arduino microcontroller, MPU6050/MPU6500 IMU sensor, and PID control algorithm to maintain equilibrium on two wheels.

Overview

This robot uses a gyroscope (and accelerometer from the MPU6050/MPU6500) to measure its tilt angle in real-time. A PID controller processes this data to adjust motor speeds, keeping the robot balanced even when pushed or tilted. The system operates without using the MPU's Digital Motion Processor (DMP).

Hardware Requirements

Microcontroller

  • Arduino UNO, Arduino Nano, or compatible board

Sensors

  • MPU6050/MPU6500 - 6-DOF IMU sensor (accelerometer + gyroscope)

Motor Driver

  • L298N - Dual H-Bridge motor driver module

Motors

  • 2x DC Motors - With wheels attached (5V-12V)

Other Components

  • Power Supply - Battery pack (5V-12V depending on motors)
  • Wires - Jumper and connecting wires
  • Structural Frame - Robot chassis/body

Connection Diagram

MPU6050/MPU6500 to Arduino

MPU Pin Arduino Pin Notes
VCC 5V (or 3.3V) Power for sensor
GND GND Common ground
SCL A5 (UNO/Nano) / 21 (MEGA) I²C clock
SDA A4 (UNO/Nano) / 20 (MEGA) I²C data
INT D2 (optional) Not used in this sketch

L298N Motor Driver to Arduino

Function Arduino Pin L298N Pin Description
ENA D5 (PWM) ENA Left motor speed control
IN1 D6 IN1 Left motor direction 1
IN2 D7 IN2 Left motor direction 2
IN3 D9 IN3 Right motor direction 1
IN4 D8 IN4 Right motor direction 2
ENB D10 (PWM) ENB Right motor speed control
+5V +5V Logic power for L298N
GND GND GND Common ground

L298N Motor Connections

  • Left Motor: Connect to OUT1 and OUT2
  • Right Motor: Connect to OUT3 and OUT4
  • +12V IN / GND: Connect to battery (for motor power)

Software Requirements

Arduino IDE Libraries

Install these libraries in the Arduino IDE (SketchInclude LibraryManage Libraries):

  1. MPU6050_tockn - IMU sensor library

    • Search: "MPU6050_tockn"
    • Author: tockn
  2. PID_v1 - PID controller library

    • Search: "PID"
    • Author: Brett Beauregard
  3. Wire - Built-in (I²C communication)

Installation

  1. Clone/Download the project files
  2. Open self_balancing.ino in Arduino IDE
  3. Install the required libraries (see Software Requirements)
  4. Connect your Arduino board via USB
  5. Select the correct board and COM port
  6. Upload the sketch to your Arduino

Configuration

PID Tuning (Lines 71-74)

Adjust these values to fine-tune balancing behavior:

double Kp = 60;     // Proportional gain
double Ki = 270;    // Integral gain
double Kd = 2.2;    // Derivative gain
  • Kp: Increases responsiveness. Higher values = faster response but may cause oscillation.
  • Ki: Corrects steady-state errors. Use carefully to avoid instability.
  • Kd: Dampens oscillations. Helps smooth out movements.

Motor Speed Calibration (Lines 92-93)

Adjust to balance left and right motor speeds:

double motorSpeedFactorLeft = 0.6;   // Multiply left motor speed
double motorSpeedFactorRight = 0.5;  // Multiply right motor speed

If the robot drifts left, increase motorSpeedFactorRight or decrease motorSpeedFactorLeft.

GPIO Pins (Lines 86-91)

If using different Arduino pins, update these:

int ENA = 5;    // Left motor PWM
int IN1 = 6;    // Left motor direction
int IN2 = 7;    // Left motor direction
int IN3 = 9;    // Right motor direction
int IN4 = 8;    // Right motor direction
int ENB = 10;   // Right motor PWM

How It Works

  1. Sensor Reading: The MPU6050 measures the robot's pitch angle (tilt)
  2. Error Calculation: The PID controller compares actual angle vs. setpoint (0°)
  3. Motor Adjustment: Based on PID output, motor speeds are adjusted to correct the tilt
  4. Feedback Loop: This repeats ~100 times per second (10ms interval)

The balancing principle: If the robot tilts forward, the motor speeds increase to move forward and restore balance. If it tilts backward, motors move backward.

Calibration Steps

  1. Power on the robot and observe serial output (9600 baud)
  2. Keep the robot still during gyroscope calibration (takes ~5 seconds)
  3. Place the robot on a flat surface at a slight angle (15-30°)
  4. Gradually adjust PID values starting with small increments
  5. Test responsiveness to gentle pushes
  6. Fine-tune motor speed factors if drifting occurs

Troubleshooting

Issue Solution
Robot doesn't balance Check MPU6050 connection; recalibrate gyro offsets
Robot unresponsive Verify L298N connections; check PWM pins
Drifts left/right Adjust motor speed factors
Vibrates/oscillates Decrease Kp or increase Kd
Slow response Increase Kp
Power issues Verify battery voltage matches motor specs

Notes

  • Gyro Calibration: Performed automatically on startup with robot held still
  • Serial Monitor: Set baud rate to 9600 to view sensor data
  • Optimal Balance: Robot balances around pitch angle ≈ 0° (vertical)
  • No DMP: Uses direct gyro/accel integration instead of MPU's Digital Motion Processor

Future Enhancements

  • Add wireless remote control (Bluetooth/RF)
  • Implement smartphone app control
  • Add obstacle avoidance (distance sensors)
  • Improve balance algorithm (use Kalman filter)
  • Add speed/direction control via joystick

License

This project is open-source. Feel free to modify and improve it!

Author

Created for Arduino-based robotics learning and experimentation.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages