Connection failed in MPU6050 sensor

Hello everyone! I bought a GY-87 module sesnor, this module has BMP180, MPU6050 and QMC5883L sesnors. Link amazon:

I am using Arduino NANO for this project. The connection NANO - GY is:


3.3V 3.3V

Fisrt I used the BMP180 sensor, and it works correct. The serial monitor prints “BMP180 init success”.

But when I tried to calibrate the MPU6050, the monitor serial prints “Connection failed in MPU6050”.
I upload an example to MPU, that only use the Wire library.

#include <Wire.h>
const int MPU_ADDR = 0x68;
int16_t accelerometer_x, accelerometer_y, accelerometer_z;
int16_t gyro_x, gyro_y, gyro_z;
int16_t temperature;
char tmp_str [7];
char* convert_int16_to_str(int16_t i) {
  sprintf(tmp_str, "%6d", i);
  return tmp_str;

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


void loop() {
  Wire.requestFrom(MPU_ADDR, 7*2, true);

  accelerometer_x =<<8 |;
  accelerometer_y =<<8 |;
  accelerometer_z =<<8 |;
  temperature =<<8 |;
  gyro_x =<<8 |;
  gyro_y =<<8 |;
  gyro_z =<<8 |;

  Serial.print("aX = "); Serial.print(convert_int16_to_str(accelerometer_x));
  Serial.print(" | aY = "); Serial.print(convert_int16_to_str(accelerometer_y));
  Serial.print(" | aZ = "); Serial.print(convert_int16_to_str(accelerometer_z));

  Serial.print(" | tmp = "); Serial.print(temperature/340.00+36.53);
  Serial.print(" | gX = "); Serial.print(convert_int16_to_str(gyro_x));
  Serial.print(" | gY = "); Serial.print(convert_int16_to_str(gyro_y));
  Serial.print(" | gZ = "); Serial.print(convert_int16_to_str(gyro_z));

  delay (700);

I use I2C_scanner to find 0x68 ADDRESS. Arduino Playground - I2cScanner.

The serial monitor prints:

aX: -6840 | aY: -2064| aZ: 840| tmp: 44.32| gX: -630| gY: 574| gZ: -158

Then I tried to calibrate the sensor, but it doesn’t works. I use this libraries: arduino_MPU6050_lib_pack.rar - Google Drive.

The code:

// I2Cdev and MPU6050 must be installed as libraries
#include "I2Cdev.h"
#include "MPU6050.h"
#include "Wire.h"

//Change this 3 variables if you want to fine tune the skecth to your needs.
int buffersize=1000;     //Amount of readings used to average, make it higher to get more precision but sketch will be slower  (default:1000)
int acel_deadzone=8;     //Acelerometer error allowed, make it lower to get more precision, but sketch may not converge  (default:8)
int giro_deadzone=1;     //Giro error allowed, make it lower to get more precision, but sketch may not converge  (default:1)

// default I2C address is 0x68
// specific I2C addresses may be passed as a parameter here
// AD0 low = 0x68 (default for InvenSense evaluation board)
// AD0 high = 0x69
//MPU6050 accelgyro;
MPU6050 accelgyro(0x68); // <-- use for AD0 high

int16_t ax, ay, az,gx, gy, gz;

int mean_ax,mean_ay,mean_az,mean_gx,mean_gy,mean_gz,state=0;
int ax_offset,ay_offset,az_offset,gx_offset,gy_offset,gz_offset;

void setup() {
  // join I2C bus (I2Cdev library doesn't do this automatically)
  TWBR = 24; // 400kHz I2C clock (200kHz if CPU is 8MHz). Leonardo measured 250kHz.

  // initialize serial communication

  // initialize device

  // wait for ready
  while (Serial.available() &&; // empty buffer
  while (!Serial.available()){
    Serial.println(F("Send any character to start sketch.\n"));
  while (Serial.available() &&; // empty buffer again

  // start message
  Serial.println("\nMPU6050 Calibration Sketch");
  Serial.println("\nYour MPU6050 should be placed in horizontal position, with package letters facing up. \nDon't touch it until you see a finish message.\n");
  // verify connection
  Serial.println(accelgyro.testConnection() ? "MPU6050 connection successful" : "MPU6050 connection failed");
  // reset offsets

void loop() {
  if (state==0){
    Serial.println("\nReading sensors for first time...");

  if (state==1) {
    Serial.println("\nCalculating offsets...");

  if (state==2) {
    Serial.print("\nSensor readings with offsets:\t");
    Serial.print("Your offsets:\t");
    Serial.println("\nData is printed as: acelX acelY acelZ giroX giroY giroZ");
    Serial.println("Check that your sensor readings are close to 0 0 16384 0 0 0");
    Serial.println("If calibration was succesful write down your offsets so you can set them in your projects using something similar to mpu.setXAccelOffset(youroffset)");
    while (1);

void meansensors(){
  long i=0,buff_ax=0,buff_ay=0,buff_az=0,buff_gx=0,buff_gy=0,buff_gz=0;

  while (i<(buffersize+101)){
    // read raw accel/gyro measurements from device
    accelgyro.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
    if (i>100 && i<=(buffersize+100)){ //First 100 measures are discarded
    if (i==(buffersize+100)){
    delay(2); //Needed so we don't get repeated measures

void calibration(){

  while (1){
    int ready=0;



    if (abs(mean_ax)<=acel_deadzone) ready++;
    else ax_offset=ax_offset-mean_ax/acel_deadzone;

    if (abs(mean_ay)<=acel_deadzone) ready++;
    else ay_offset=ay_offset-mean_ay/acel_deadzone;

    if (abs(16384-mean_az)<=acel_deadzone) ready++;
    else az_offset=az_offset+(16384-mean_az)/acel_deadzone;

    if (abs(mean_gx)<=giro_deadzone) ready++;
    else gx_offset=gx_offset-mean_gx/(giro_deadzone+1);

    if (abs(mean_gy)<=giro_deadzone) ready++;
    else gy_offset=gy_offset-mean_gy/(giro_deadzone+1);

    if (abs(mean_gz)<=giro_deadzone) ready++;
    else gz_offset=gz_offset-mean_gz/(giro_deadzone+1);

    if (ready==6) break;

Here, firstly prints MPU6050 connection failed, and the final message with the Offsets doesn’t appear.
The GY is in horizontal position and facing up. I have been waitig for 4 minutes and nothing appears…

If someone can help me, please!

Comment out the TWBR line and try again. Most probably you have to long wires for a 400kHz bus speed.

What I have to comment out in TWBR?

Have you got 4k7 pullup resistors on the SDA and SCL lines?

Tom.... :slight_smile:

Hi Tom,

No page says that I have to put resistors to use MPU6050, I have to do it?

I connected to 3.3V

Most controllers have them already fitted to the I2C lines, but it will not hurt to add external ones.
I2C needs pullup resistors and some problems with I2C devices seems to be because of the lack of pullups.
Yes you can put them from SDA to 3V3 and SCL and 3V3.

Tom.... :slight_smile:

Thansk for helping!

But I upload codes without resistors and the sensor prints numbers… and when I move it the numbers changes. The problem is when I want to calibrate the sensor, it doesn’t calibrate.

If the sensor was broken, it always prints 0, no?

Can I use the pullup of the Arduino NANO? How can I write in the code that I’m using the Pullup of the Arduino to SCL and SDA?


Can I use the pullup of the Arduino NANO? How can I write in the code that I'm using the Pullup of the Arduino to SCL and SDA?

You don't do anything to your code, you physically add them to your circuit, the code has nothing to do with I2C pullups.

Tom... :slight_smile:


Okey, so if I put this resistors (connected from SCL and SDA to VCC (3.3V)), the code from Arduino doesn't prints "MPU6050 connection failed" and the sensor can be calibrated?

Because I told you before that the sensor works (not correctly due to the calibration), it's very strange...


...the code won't print...

Sorry!! It's first conditional :slight_smile:

What I have to comment out in TWBR?

I wrote "the TWBR line", so I meant the following line:

  TWBR = 24; // 400kHz I2C clock (200kHz if CPU is 8MHz). Leonardo measured 250kHz.


I eliminated this line, and itkeep printing connection failed....

If the sensor was broken, it always prints 0, no?

No, it would always return only 1 bits, so the results you get may be the result of a broken sensor. But it seems you have a sensor listening on the MPU6050 address but not behaving exactly as such a sensor. It it possible that you got a Chinese fake chip?


The sensor is in the GY-87 module sensor. I bought it in amazon:

I think it is not a fake chip... Why do you say that?

Why do you think the chip is NOT fake? They are everywhere these days, especially with sellers like Amazon, eBay and Alibaba. And there are always failing modules.

Have you tried connecting 5V to Vccin instead of 3V3 to the 3V3 pin on the 6050 and using 4k7 resistors from SDA to 5V and SCL to 5V?

If not then please try it, using the 5V rather than 3V3 supply from the Nano.

Tom... :slight_smile: