Self balancing robot but not balance

Hii, I'm currently working on a self-balancing robot project following a YouTube tutorial, with the following components: Arduino Uno, MPU6050, L298N, and yellow DC motors. The programming code is like this:

#include "I2Cdev.h"
#include <PID_v1.h>
#include "MPU6050_6Axis_MotionApps20.h"
#include <Wire.h>
MPU6050 mpu;
bool dmpReady = false;
uint8_t mpuIntStatus, devStatus;
uint16_t packetSize, fifoCount;
uint8_t fifoBuffer[64];
Quaternion q;
VectorFloat gravity;
float ypr[3];
double setpoint = -2.5;
double input, output;
double Kp = 40, Ki = 1, Kd = 5;
PID pid(&input, &output, &setpoint, Kp, Ki, Kd, DIRECT);
volatile bool mpuInterrupt = false;
void dmpDataReady() { mpuInterrupt = true; }
void setup() {
  if (devStatus == 0) {
  Serial.println("MPU6050 siap, DMP aktif.");
  // ...
} else {
  Serial.print("DMP gagal. Kode: ");
  Serial.println(devStatus);
}
  Serial.begin(115200);
  mpu.initialize();
  devStatus = mpu.dmpInitialize();
  // Sesuaikan offset dengan kalibrasi kamu
  mpu.setXGyroOffset(-479);
  mpu.setYGyroOffset(84);
  mpu.setZGyroOffset(15);
  mpu.setZAccelOffset(1638);
  if (devStatus == 0) {
    mpu.setDMPEnabled(true);
    attachInterrupt(2, dmpDataReady, RISING);
    mpuIntStatus = mpu.getIntStatus();
    dmpReady = true;
    packetSize = mpu.dmpGetFIFOPacketSize();
    pid.SetMode(AUTOMATIC);
    pid.SetSampleTime(10);
    pid.SetOutputLimits(-127, 127);
  } else {
    Serial.println("DMP failed.");
  }
  pinMode(6, OUTPUT); pinMode(9, OUTPUT); // Motor kiri
  pinMode(10, OUTPUT); pinMode(11, OUTPUT); // Motor kanan
  pinMode(5, OUTPUT); pinMode(3, OUTPUT); // ENA & ENB
}
void loop() {
  if (!dmpReady) return;
  if (!mpuInterrupt && fifoCount < packetSize) return;
  mpuInterrupt = false;
  mpuIntStatus = mpu.getIntStatus();
  fifoCount = mpu.getFIFOCount();
  if ((mpuIntStatus & 0x10) || fifoCount == 1024) {
    mpu.resetFIFO();
    Serial.println("FIFO overflow!");
    return;
  }
  if (mpuIntStatus & 0x02) {
    while (fifoCount < packetSize) fifoCount = mpu.getFIFOCount();
    mpu.getFIFOBytes(fifoBuffer, packetSize);
    fifoCount -= packetSize;
    // Ambil orientasi
    mpu.dmpGetQuaternion(&q, fifoBuffer);
    mpu.dmpGetGravity(&gravity, &q);
    mpu.dmpGetYawPitchRoll(ypr, &q, &gravity);
    input = ypr[1] * 180 / M_PI;  // Ambil pitch dalam derajat
    // Deteksi jatuh
    if (abs(input - setpoint) > 45) {
      Stop();
      Serial.println("Robot jatuh! Emergency Stop.");
      return;
    }
    pid.Compute();
    Serial.print("Pitch: "); Serial.print(input);
    Serial.print(" | Output: "); Serial.println(output);
    // Kendali motor
    if (abs(output) < 5) {
      Stop();
    } else if (output > 0) {
      Forward(abs(output));
    } else {
      Reverse(abs(output));
    }
  }
}
void Forward(int speed) {
  digitalWrite(6, HIGH); digitalWrite(9, LOW);  // Kiri maju
  digitalWrite(10, HIGH); digitalWrite(11, LOW); // Kanan maju
  analogWrite(5, speed); analogWrite(3, speed);
  Serial.println("Forward");
}
void Reverse(int speed) {
  digitalWrite(6, LOW); digitalWrite(9, HIGH);  // Kiri mundur
  digitalWrite(10, LOW); digitalWrite(11, HIGH); // Kanan mundur
  analogWrite(5, speed); analogWrite(3, speed);
  Serial.println("Reverse");
}
void Stop() {
  analogWrite(5, 0); analogWrite(3, 0);
  Serial.println("Stop");
}

The problem is that my robot won't balance and seems disconnected from the gyro. And uhh can someone solve this but translate it into indonesia language so i can understand it better :slight_smile:

Apakah Anda membuat kode tersebut, atau hanya menyalinnya? Jika seperti dugaan saya itu adalah salinan, apakah Anda menyalin/menempelkannya atau Anda cukup bodoh untuk benar-benar mencoba mengetik ulang semuanya.
Saya harap itu adalah kreasi Anda sendiri, tetapi itu menimbulkan tantangan tersendiri, mungkin saja itu salah. Anda perlu menambahkan debug hingga Anda menemukan sumber kesalahannya. Tidak ada yang bisa membantu lagi karena Anda tidak menunjukkan hasil apa pun.

Did you create the code, or is it just a copy? If as I suspect it's a copy, did you copy/paste or were you foolish enough to actually attempt to re-key it all.
I hope it's your own creation but that brings it's own challenges, it may simply be wrong. You will need to add debug until you find the source of the error. Can't help more since you don't show any output.

1 Like

well yeah, i copied almost everything from the tutorial and tried to solve the problems with my friends. tbh i dont really know what im making nor what im doing, the teacher is also doesnt help much :sob: also what kind of output should i post? a pic? vid?

Translate the English you read/receive into Indonesian.

Where do you find these libraries?

#include "I2Cdev.h"
#include <PID_v1.h>
#include "MPU6050_6Axis_MotionApps20.h"

I2Cdev.h seems to be resident in the IDE.
This file: PID_v1.h seems to now be PID_v1_bc.h
MPU6050_6Axis_MotionApps20 does not seem to exist (but you have it, or you could not compile your sketch).

The teacher teaches you more than you need to know. Your job as a student is to listen to your teacher. The problem is not the teacher.

gosh, this might be kinda embarrasing to say but my friends have been using some a.i to help with the coding and the result is to add that "mpu6050_6Axis_MotionApps20" library

This is far beyond your ability... consider trying a rolling "vehicle" and not a robot... but keep going until you burn out or catch up...

Both are trouble.

Verify your power supply is sufficient to supply power for the L298N as well as the motors. Many non-working "vehicle" projects start with a weak power supply.

You need to test every device in your project. Start with your L298N. Write a small sketch to make each motor go in one direction, then the other, then use PWM to change the rate of motor, then make both wheels do some movement.

Next, a small sketch with the MPU6050. See that it can register movement. Then, see that it can register and correct for stability... for the device to be stable, it needs to always be correcting itself. The MPU library (every library) has a folder full of examples. Run them and learn from them.

okay, i will definitely try that and post an update thank u so much :sob:

Adding to your list of things to do... learn to write "non-blocking" code. Your devices will need to operate "immediately" (when the vehicle tips or moves... which is all the time) and not wait for code running another device. For this, read about "arduino multiple events." I do not have knowledge enough to help you through the balancing logic.