I need help with mpu6050 library

Hi, I would appreciate if someone can help me. It turns out that I connected the accelerometer with gyroscope GY-521 to the Arduino Leonardo. So I set out to upload the code and I had verified normal, but when I go to serial monitor I get nothing.

So I tried everything to find out where the problem was, and I saw that the code stayed on the line
if (!mpu.testConnection()) { while (1); }

I tried uninstalling and reinstalling the MPU6050 library from electronic cats, and it still doesn't give me an answer. The truth is that I don't understand what could be happening with the library, since there are a few times when it works but others when it does not.
Any suggestions as to what might be going on?

#include <Wire.h>
#include <I2Cdev.h>
#include <MPU6050.h>
#include <Mouse.h>

MPU6050 mpu;
int16_t ax, ay, az, gx, gy, gz;
int vx, vy;

void setup() {
  Serial.begin(9600);
  Wire.begin();
  mpu.initialize();
  if (!mpu.testConnection()) { while (1); }
}

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

  vx = (gx+300)/150;  // change 300 from 0 to 355
  vy = -(gz-100)/150; // same here about "-100"  from -355 to 0

  Serial.print("gx = ");
  Serial.print(gx);
  Serial.print(" | gz = ");
  Serial.print(gz);
  
  Serial.print("        | X = ");
  Serial.print(vx);
  Serial.print(" | Y = ");
  Serial.println(vy);
  
  Mouse.move(vx, vy);
  
  delay(20);
}

did you try with the library's example

Hi, I would appreciate if someone can help me. It turns out that I connected the accelerometer with gyroscope GY-521 to the Arduino Leonardo. So I set out to upload the code and I had verified normal, but when I go to serial monitor I get nothing.

So I tried everything to find out where the problem was, and I saw that the code stayed on the line
if (!mpu.testConnection()) { while (1); }

I tried uninstalling and reinstalling the MPU6050 library from electronic cats, and it still doesn't give me an answer. The truth is that I don't understand what could be happening with the library, since there are a few times when it works but others when it does not.
Any suggestions as to what might be going on?

#include <Wire.h>
#include <I2Cdev.h>
#include <MPU6050.h>
#include <Mouse.h>

MPU6050 mpu;
int16_t ax, ay, az, gx, gy, gz;
int vx, vy;

void setup() {
  Serial.begin(9600);
  Wire.begin();
  mpu.initialize();
  if (!mpu.testConnection()) { while (1); }
}

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

  vx = (gx+300)/150;  // change 300 from 0 to 355
  vy = -(gz-100)/150; // same here about "-100"  from -355 to 0

  Serial.print("gx = ");
  Serial.print(gx);
  Serial.print(" | gz = ");
  Serial.print(gz);
  
  Serial.print("        | X = ");
  Serial.print(vx);
  Serial.print(" | Y = ");
  Serial.println(vy);
  
  Mouse.move(vx, vy);
  
  delay(20);
}

This is where I got the code.

I2C scanner

What did the I2C scanner report?

I have merged your cross-posts @dmzekt.

Cross-posting is against the Arduino forum rules. The reason is that duplicate posts can waste the time of the people trying to help. Someone might spend a lot of time investigating and writing a detailed answer on one topic, without knowing that someone else already did the same in the other topic.

Repeated cross-posting can result in a suspension from the forum.

In the future, please only create one topic for each distinct subject matter. This is basic forum etiquette, as explained in the "How to get the best out of this forum" guide. It contains a lot of other useful information. Please read it.

Thanks in advance for your cooperation.

At first it had detected me twice, but now that I put it back, it kept scanning.

Yes, I even tried with the IMU_Zero example and nothing appears in the serial monitor.

post an image of your project? If the I2C scan is not working how can you expect your program to work? Did you solder the wires into the mpu thingy?

Using one of these,


Post a schematic of your project.

Why is your MPU set to the alternate address?

It is an air mouse prototype.

And I upload a video to YouTube with more or less the problem I have. After it works for a few seconds, it doesn't work again and I have to wait some time to reconnect it and it works again for a few seconds.

1 Like

Hi,
Try putting 4K7 pullup resistors on the SDA and SCL lines.
One each between SDA and 5V and the other between SCL and 5V.

Tom... :smiley: :+1: :coffee: :australia:

With the MPU giving issues and the MPU defaulting to the alternate I2C address, the MPU may be at fault. Try another MPU.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.