Translate

DIY Self Balancing Robot with Arduino and MPU6050

 1. Introduction

A Self Balancing Robot is an exciting robotics project that demonstrates the principles of control systems, sensors, and feedback mechanisms. This robot balances itself on two wheels by continuously adjusting its position using data from an orientation sensor.

The working principle is similar to an inverted pendulum system. When the robot tilts forward or backward, sensors detect the change in angle and the controller immediately drives the motors to restore balance.

In this project, we use an Arduino microcontroller, an MPU6050 gyroscope and accelerometer module, and a motor driver module to create a robot capable of balancing automatically. The Arduino reads tilt data from the MPU6050 and calculates corrections using a PID control algorithm. Based on this calculation, the motors move forward or backward to maintain balance.

This project is excellent for students and robotics enthusiasts because it teaches:

  • Sensor interfacing

  • PID control systems

  • Motor control

  • Real-time feedback systems

  • Robotics fundamentals

Once completed, this robot becomes a perfect demonstration model for engineering labs, robotics workshops, and STEM education.


2. Components

The following components are required for building the self balancing robot.

ComponentQuantity
Arduino UNO 1
MPU6050 Gyroscope + Accelerometer Module1
L298N Motor Driver Module1
DC Geared Motors2
Robot Wheels2
Robot Chassis1
Lithium Battery (7.4V or 11.1V)1
Battery Holder1
Jumper WiresAs required
Switch1

3. Circuit and Connections

Pinout Diagram Arduino Uno R3

Pinout Diagram MPU6050
MPU6050 Pinout Diagram


Pinout Diagram L298N

Diagram for Self Balancing Robot


The circuit of the self balancing robot consists of three main sections:

  1. Sensor section

  2. Motor driver section

  3. Power section

MPU6050 to Arduino Connections

MPU6050Arduino
VCC5V
GNDGND
SDAA4
SCLA5

Motor Driver to Arduino

L298NArduino
IN1D8
IN2D9
IN3D10
IN4D11
ENAD5
ENBD6

Motor Connections

Motor A → OUT1, OUT2
Motor B → OUT3, OUT4

Power Connections

Battery → L298N power input

The Arduino receives sensor data from the MPU6050 through I2C communication, processes it, and sends motor control signals to the L298N motor driver.


4. Circuit Working

The self balancing robot continuously monitors its tilt angle using the MPU6050 sensor.

The MPU6050 contains:

  • Accelerometer

  • Gyroscope

The accelerometer measures the robot's orientation relative to gravity, while the gyroscope measures angular velocity.

Arduino reads these values and calculates the robot's tilt angle. When the robot leans forward or backward:

  1. The MPU6050 detects the change in angle.

  2. Arduino processes the angle using a PID algorithm.

  3. The controller determines the required correction.

  4. Motor driver moves the motors forward or backward.

  5. The robot returns to its upright position.

This process happens hundreds of times per second, allowing the robot to maintain balance.


5. Code

#include <Wire.h> #include <MPU6050.h> MPU6050 mpu; // Motor driver pins #define ENA 5 #define ENB 6 #define IN1 8 #define IN2 9 #define IN3 10 #define IN4 11 // PID variables float Kp = 15; float Ki = 0.5; float Kd = 1; float setPoint = 0; float angle; float error; float previous_error = 0; float integral = 0; float derivative; float output; void setup() { Serial.begin(9600); Wire.begin(); mpu.initialize(); if (!mpu.testConnection()) { Serial.println("MPU6050 connection failed"); while (1); } // Motor pins pinMode(ENA, OUTPUT); pinMode(ENB, OUTPUT); pinMode(IN1, OUTPUT); pinMode(IN2, OUTPUT); pinMode(IN3, OUTPUT); pinMode(IN4, OUTPUT); } void loop() { int16_t ax, ay, az; int16_t gx, gy, gz; mpu.getMotion6(&ax, &ay, &az, &gx, &gy, &gz); // Calculate tilt angle angle = atan2(ax, az) * 180 / PI; // PID calculations error = angle - setPoint; integral = integral + error; derivative = error - previous_error; output = Kp * error + Ki * integral + Kd * derivative; previous_error = error; int motorSpeed = constrain(abs(output), 0, 255); // Robot leaning forward if (output > 0) { analogWrite(ENA, motorSpeed); analogWrite(ENB, motorSpeed); digitalWrite(IN1, HIGH); digitalWrite(IN2, LOW); digitalWrite(IN3, HIGH); digitalWrite(IN4, LOW); } // Robot leaning backward else { analogWrite(ENA, motorSpeed); analogWrite(ENB, motorSpeed); digitalWrite(IN1, LOW); digitalWrite(IN2, HIGH); digitalWrite(IN3, LOW); digitalWrite(IN4, HIGH); } delay(10); }

6. Code Working

The program begins by initializing the MPU6050 sensor, I2C communication, and the motor control pins.

Sensor Initialization

In the setup() function, the Arduino starts serial communication and initializes the MPU6050 sensor.

mpu.initialize();

The program also checks whether the sensor is connected properly.

If the MPU6050 is not detected, the Arduino stops execution to prevent incorrect operation.


Sensor Data Reading

Inside the loop() function, the Arduino continuously reads accelerometer and gyroscope values from the MPU6050 sensor.

mpu.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);

These values represent the robot's orientation and motion.


Angle Calculation

The tilt angle of the robot is calculated using the accelerometer values.

angle = atan2(ax, az) × 180 / PI

This converts the measured angle from radians to degrees.

The angle represents how much the robot is tilted forward or backward.


PID Control

The robot uses a PID (Proportional–Integral–Derivative) controller to maintain balance.

The PID formula used is:

Output = Kp × Error + Ki × Integral + Kd × Derivative

Where:

Kp – Proportional constant
Ki – Integral constant
Kd – Derivative constant

The PID controller calculates how much correction is needed to bring the robot back to the vertical position.


Motor Speed Control

The calculated PID output determines the speed and direction of the motors.

The speed is limited between 0 and 255 using:

motorSpeed = constrain(abs(output),0,255);

This prevents excessive motor speed.

The speed is then applied using PWM control:

analogWrite(ENA, motorSpeed);
analogWrite(ENB, motorSpeed);

Motor Direction Control

If the robot tilts forward, the motors rotate forward.

If the robot tilts backward, the motors rotate in reverse.

This continuous correction keeps the robot balanced on two wheels.


Continuous Balancing

The entire process runs repeatedly with a small delay:

delay(10);

This allows the robot to measure tilt, calculate correction, and adjust motor speed many times per second, enabling stable balancing.


7. Tips

• Keep the center of gravity low for better stability.
• Use high torque geared motors.
• Tune the PID constants carefully.
• Use a good lithium battery for stable voltage.
• Ensure firm mounting of the MPU6050 sensor.
• Avoid loose wiring to prevent sensor noise.

Note: Install the MPU6050 library by Jeff Rowberg before uploading the code.

Arduino IDE →
Sketch → Include Library → Manage Libraries → Search MPU6050 and install


8. Uses

Self balancing robots have many practical and educational applications.

• Robotics learning and training
• Control systems research
• Educational demonstrations
• Autonomous robot development
• Foundation for Segway type vehicles
• Engineering laboratory experiments
• Robotics competitions


9. Conclusion

The Arduino Self Balancing Robot is a fantastic project that combines electronics, programming, and control theory. By using the MPU6050 sensor and PID algorithm, the robot can dynamically correct its position and remain upright on two wheels.

This project helps students understand real-time feedback systems, sensor fusion, and robotics control techniques. Once mastered, the same principles can be used in advanced robotics projects such as self balancing scooters, humanoid robots, and autonomous vehicles.

With proper tuning and construction, this robot can achieve stable and impressive balancing performance.

തുടക്കക്കാർക്കായി ഇലക്ട്രോണിക്സ് ലളിതമായി പഠിക്കാം.

Empowering students in Kerala with hands-on technical skills.