Problem with I2C connection with MPU9250

Hi!

I am trying to get some values from a MPU9250. I am using Bolderflights Library but I get the error "I2C connection unsuccessful". So I did some I2C scanning and with one of the scanners it says that I have a MPU6500, but on the chip it is labeled MPU-9250 / 6500. Can that be a problem? I get in the scanner that I have a connection on 0x68 with I read from. Thanks beforehand!

  • UPDATE *
    This code finds the I2C channel and prints: found i2c device on 0x68 and is mpu6500 and ready to use:
#include "MPU9250.h"

uint8_t addrs[7] = {0};
uint8_t device_count = 0;

template <typename WireType = TwoWire>
void scan_mpu(WireType& wire = Wire) {
    Serial.println("Searching for i2c devices...");
    device_count = 0;
    for (uint8_t i = 0x68; i < 0x70; ++i) {
        wire.beginTransmission(i);
        if (wire.endTransmission() == 0) {
            addrs[device_count++] = i;
            delay(10);
        }
    }
    Serial.print("Found ");
    Serial.print(device_count, DEC);
    Serial.println(" I2C devices");

    Serial.print("I2C addresses are: ");
    for (uint8_t i = 0; i < device_count; ++i) {
        Serial.print("0x");
        Serial.print(addrs[i], HEX);
        Serial.print(" ");
    }
    Serial.println();
}

template <typename WireType = TwoWire>
uint8_t readByte(uint8_t address, uint8_t subAddress, WireType& wire = Wire) {
    uint8_t data = 0;
    wire.beginTransmission(address);
    wire.write(subAddress);
    wire.endTransmission(false);
    wire.requestFrom(address, (size_t)1);
    if (wire.available()) data = wire.read();
    return data;
}

void setup() {
    Serial.begin(115200);
    Serial.flush();
    Wire.begin();
    delay(2000);

    scan_mpu();

    if (device_count == 0) {
        Serial.println("No device found on I2C bus. Please check your hardware connection");
        while (1)
            ;
    }

    // check WHO_AM_I address of MPU
    for (uint8_t i = 0; i < device_count; ++i) {
        Serial.print("I2C address 0x");
        Serial.print(addrs[i], HEX);
        byte ca = readByte(addrs[i], WHO_AM_I_MPU9250);
        if (ca == MPU9250_WHOAMI_DEFAULT_VALUE) {
            Serial.println(" is MPU9250 and ready to use");
        } else if (ca == MPU9255_WHOAMI_DEFAULT_VALUE) {
            Serial.println(" is MPU9255 and ready to use");
        } else if (ca == MPU6500_WHOAMI_DEFAULT_VALUE) {
            Serial.println(" is MPU6500 and ready to use");
        } else {
            Serial.println(" is not MPU series");
            Serial.print("WHO_AM_I is ");
            Serial.println(ca, HEX);
            Serial.println("Please use correct device");
        }
    }
}

void loop() {
}

But when I read it this way it doesnt work:

#include "MPU9250.h"



Mpu9250 imu;

void setup() {
 if (!imu.setup(0x68)) {
    Serial.println("IMU initialization unsuccessful");
    while (1) {}
  }
}

Best regards Max

@lordmax2, your topic has been moved to a more suitable location on the forum.

What port does your code access, they have to be the same?

My code accesses the same port as found with the I2C scanner. 0x68

The MPU6500_WHOAMI_DEFAULT_VALUE is 0x70, so you have indeed a MPU-6500.

It seems that the MPU-9250 has a magnetometer and a MPU-6500 does not. They are the same for everything else.

If you buy modules from good sellers (Adafruit, Sparkfun, Pololu) then you don't get counterfeit chips and you don't get random surprise sensors.

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