7 years, 8 months ago.

MPU6050 doesn't work with Nucleo-F411RE

I'm using the following library with my nucleo-f411re and i can't get it to work. https://developer.mbed.org/users/garfieldsg/code/mpu6050_test/

To begin with i need to make some changes to make it compile, 1. Update mbed in the compiler 2. Make the following change in I2Cdev.h

#ifndef I2Cdev_h
#define I2Cdev_h

#include "mbed.h"

//#define I2C_SDA p28
//#define I2C_SCL p27

As i have a different board i comment out these lines to allow compiler to use default definition for I2C_SDA and I2C_SCL.

Upon running the code i get the following through my serial viewer.

MPU6050 initialize 
MPU6050::initialize start
MPU6050::initialize end
MPU6050 testConnection 
MPU6050::testConnection start
DeviceId = 0
MPU6050 test failed 
0;0;-21490;0;0;4117
0;0;-21490;0;0;4117
0;0;-21490;0;0;4117
0;0;-21490;0;0;4117
0;0;-21490;0;0;4117
0;0;-21490;0;0;4117

I have tested the MPU6050 i own with an i2c scanner and i manage to get a device as 0x68 . I have also managed to get the device to work with the following library, "https://developer.mbed.org/users/onehorse/code/MPU6050IMU/ " after removing the code for the nokia lcd.

Any idea what could be wrong?

Did you found a solution? Bc i have the same problem today.

posted by Fabrice Longchamp 12 Apr 2018

1 Answer

7 years, 8 months ago.

Problem could be the unreliable implementation of the i2c byte write operations on several platforms. You should use the I2C block write and read operations whenever possible.

The I2CDev lib currently uses the code below:

bool I2Cdev::writeBytes(uint8_t devAddr, uint8_t regAddr, uint8_t length, uint8_t *data)
{
    i2c.start();
    i2c.write(devAddr<<1);
    i2c.write(regAddr);
    for(int i = 0; i < length; i++) {
        i2c.write(data[i]);
    }
    i2c.stop();
    return true;
}

Modify this method into an I2C blockwrite. That means you have to create a new array, fill the first entry with regAddr, copy the data array into the remainder and then send all length+1 bytes to the MPU6050.

bool I2Cdev::writeBytes(uint8_t devAddr, uint8_t regAddr, uint8_t length, uint8_t *data)
{
    uint8_t mydata [128];
   
    mydata [0]=regAddr;
    for(int i = 0; i < length; i++) {
        mydata [i+1] = data[i];
    }
    i2c.write(devAddr<<1, mydata, length+1);
    
    return true;
}

was this answer helpful? I have the same problem today.

posted by Fabrice Longchamp 12 Apr 2018