self balance robot arduino

 A self-balancing robot using Arduino typically works on the principle of an inverted pendulum. It uses sensors like an MPU6050 (gyro + accelerometer) to detect the angle of tilt and adjusts motors to balance itself.



Components:

  1. Arduino Uno/Nano

  2. MPU6050 (Gyro + Accelerometer)

  3. 2 DC Motors with Encoders (or without, but better with)

  4. Motor Driver (L298N or L9110S)

  5. Wheels + Chassis

  6. Battery Pack (Li-ion or LiPo)

  7. Optional: Bluetooth module (HC-05) for control



Wiring


  • MPU6050

    • VCC → 5V

    • GND → GND

    • SDA → A4 (on Uno)

    • SCL → A5

  • Motor Driver

    • IN1, IN2 → Arduino digital pins (e.g., D8, D9)

    • IN3, IN4 → Arduino digital pins (e.g., D10, D11)

    • ENA/ENB (speed control) → PWM pins (e.g., D5, D6)

    • Motor outputs → DC motors

    • VCC → Motor power (e.g., 7.4V LiPo)

    • GND → Common ground



Arduino Code



#include <Wire.h> #include <MPU6050.h> MPU6050 mpu; float Kp = 20, Ki = 0.5, Kd = 1.2; float setPoint = 0; float input, output, error, previous_error = 0, integral = 0; int motor1Pin1 = 8, motor1Pin2 = 9; int motor2Pin1 = 10, motor2Pin2 = 11; int pwmA = 5, pwmB = 6; void setup() { Serial.begin(9600); Wire.begin(); mpu.initialize(); pinMode(motor1Pin1, OUTPUT); pinMode(motor1Pin2, OUTPUT); pinMode(motor2Pin1, OUTPUT); pinMode(motor2Pin2, OUTPUT); pinMode(pwmA, OUTPUT); pinMode(pwmB, OUTPUT); } void loop() { int16_t ax, ay, az, gx, gy, gz; mpu.getMotion6(&ax, &ay, &az, &gx, &gy, &gz); input = atan2(ay, az) * 180 / PI; error = setPoint - input; integral += error; float derivative = error - previous_error; output = Kp * error + Ki * integral + Kd * derivative; previous_error = error; setMotorSpeed(output); } void setMotorSpeed(float speed) { int motorSpeed = min(abs(speed), 255); if (speed > 0) { digitalWrite(motor1Pin1, HIGH); digitalWrite(motor1Pin2, LOW); digitalWrite(motor2Pin1, HIGH); digitalWrite(motor2Pin2, LOW); } else { digitalWrite(motor1Pin1, LOW); digitalWrite(motor1Pin2, HIGH); digitalWrite(motor2Pin1, LOW); digitalWrite(motor2Pin2, HIGH); } analogWrite(pwmA, motorSpeed); analogWrite(pwmB, motorSpeed); }

Post a Comment

0 Comments