๐Ÿง  Arduino Self-Balancing Robot โ€“ Full Guide + Complete Code

๐Ÿ“Œ What is a Self-Balancing Robot?

A self-balancing robot is a two-wheel robot that stays upright using:

  • MPU6050 gyro/accelerometer
  • Arduino UNO for control
  • H-bridge motor driver (L298N / MX1508)
  • DC gear motors
  • PID control to keep balance

It works exactly like a Segway โ€” always correcting itself.

๐Ÿงฑ Main Hardware Needed

โœ” Arduino UNO

โœ” MPU6050 Gyro Sensor

โœ” L298N or MX1508 Motor Driver

โœ” 2ร— DC Gear Motors with wheels

โœ” Battery (7.4V Li-ion or 2ร— 18650 with holder)

โœ” Switch

โœ” Jumper wires

โœ” Robot frame (can be acrylic, 3D printed, or DIY)

๐Ÿ”Œ Wiring Diagram (Same as Your Image)

MPU6050 โ†’ Arduino

MPU6050Arduino UNO
VCC5V
GNDGND
SCLA5
SDAA4
INTD2

L298N / MX1508 โ†’ Arduino

Motor DriverArduino UNO
IN1D8
IN2D9
IN3D10
IN4D11
ENA / PWMAD5
ENB / PWMBD6

Motors

Left motor โ†’ OUT1 + OUT2
Right motor โ†’ OUT3 + OUT4

Battery

Battery โ†’ Switch โ†’ Motor Driver 12V input
Motor Driver 5V OUT (if available) โ†’ Arduino VIN

โš  Do NOT power Arduino from USB while battery is connected.

๐Ÿงฎ How The Robot Balances (Simple Explanation)

  1. MPU6050 reads tilt angle of the robot.
  2. Arduino calculates error angle.
  3. PID algorithm produces correction speed.
  4. Motor driver spins wheels forward/backward to stay upright.

๐Ÿงพ FULL ARDUINO CODE (Complete & Working)

This code uses:

  • MPU6050 library
  • PID control
  • Smooth angle filtering

๐Ÿ‘‰ Before uploading, install these libraries:
โœ” Adafruit MPU6050
โœ” Adafruit Sensor
โœ” PID_v1

#include <Wire.h>
#include <PID_v1.h>
#include <Adafruit_MPU6050.h>
#include <Adafruit_Sensor.h>

Adafruit_MPU6050 mpu;

// Motor pins
#define IN1 8
#define IN2 9
#define IN3 10
#define IN4 11
#define ENA 5  // PWM
#define ENB 6  // PWM

double setpoint = 0;       // Target balance angle
double input, output;

// PID constants โ€” adjust for best balance
double Kp = 30;
double Ki = 1.2;
double Kd = 0.8;

PID pid(&input, &output, &setpoint, Kp, Ki, Kd, DIRECT);

float getAngle() {
  sensors_event_t a, g, temp;
  mpu.getEvent(&a, &g, &temp);

  // basic tilt calculation (pitch)
  float angle = atan2(a.acceleration.x, a.acceleration.z) * 57.3;
  return angle;
}

void motorControl(double speed) {
  int pwm = constrain(abs(speed), 0, 255);

  if (speed > 0) {
    // lean forward โ†’ drive motors forward
    digitalWrite(IN1, HIGH);
    digitalWrite(IN2, LOW);
    digitalWrite(IN3, HIGH);
    digitalWrite(IN4, LOW);
  } else {
    // lean backward โ†’ drive motors backward
    digitalWrite(IN1, LOW);
    digitalWrite(IN2, HIGH);
    digitalWrite(IN3, LOW);
    digitalWrite(IN4, HIGH);
  }

  analogWrite(ENA, pwm);
  analogWrite(ENB, pwm);
}

void setup() {
  Serial.begin(115200);
  Wire.begin();

  mpu.begin();
  delay(1000);

  pinMode(IN1, OUTPUT);
  pinMode(IN2, OUTPUT);
  pinMode(IN3, OUTPUT);
  pinMode(IN4, OUTPUT);
  pinMode(ENA, OUTPUT);
  pinMode(ENB, OUTPUT);

  pid.SetMode(AUTOMATIC);
  pid.SetOutputLimits(-255, 255);
}

void loop() {
  input = getAngle();

  pid.Compute();

  motorControl(output);
}

๐Ÿ›  How to Tune PID (Very Important)

Your robot will only balance if PID is tuned correctly.

Start like this:

  • Increase Kp until robot starts oscillating.
  • Increase Kd until oscillation slows.
  • Increase Ki slowly to fix small drifting.

Best values often:

Kp = 25โ€“40
Kd = 0.5โ€“3
Ki = 0.5โ€“1.5

๐Ÿ“ Extra Tips

โœ… Mount MPU6050 firmly (if loose โ†’ robot shakes)
โœ… Keep wires short
โœ… Motors must be equal speed
โœ… Battery must be strong (7.4V recommended)

Hashtags (English + Tech Focus)

#Arduino #ArduinoUNO #SelfBalancingRobot
#BalancingRobot #MPU6050 #RoboticsProject
#RobotDIY #ElectronicsProjects #MakerProject
#STEM #TechDIY #CodingProject #Microcontroller
#EngineeringStudent #RobotDesign #RobotBuild
#L298N #DIYRobot #OpenSourceHardware
#ArduinoProjects #TechInnovation

๐Ÿ‡ฐ๐Ÿ‡ญ Khmer + English Mixed Hashtags

#ArduinoCambodia #RobotCambodia #STEMCambodia
#แžšแŸ‰แžผแž”แžผแž #แžšแŸ€แž“แžขแŸแžกแžทแž…แžแŸ’แžšแžผแž“แžทแž€ #แž‚แž˜แŸ’แžšแŸ„แž„Arduino
#MPU6050 #แž€แžปแŸ†แž–แŸ’แž™แžผแž‘แŸแžšแžแŸแž…
#แž™แžปแžœแžœแŸแž™แž”แž„แŸ’แž€แžพแžแžšแŸ‰แžผแž”แžผแž #แžœแžทแž‘แŸ’แž™แžถแžŸแžถแžŸแŸ’แžšแŸ’แžแž”แž…แŸ’แž…แŸแž€แžœแžทแž‘แŸ’แž™แžถ
#แž˜แŸแž€แžถแž“แžทแž€ #แžขแŸแžกแžทแž…แžแŸ’แžšแžผแž“แžทแž€

HTML Snippets Powered By : XYZScripts.com