I2C scanner mpu6050

Use it for scanning i2c bus for devices and sensors

NBNB this scanner opens for secondary hidden i2c on the backside of an mpu6050

On the backside can be sensors like bmp085 etc (on Gy boards)


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

/*
   scanner for devices on i2c bus incl hidden side of mpu60502

    NBNB this scanner opens for secondary hidden i2c on the backside of an mpu6050
    On the backside can be sensors like bmp085 etc (on Gy boards)

*/

MPU6050 mpu;

void setup()
{
  Wire.begin();

  Serial.begin(115200);
  while (!Serial);             // Leonardo: wait for serial monitor
  mpu.initialize();
  mpu.setI2CBypassEnabled(true);
  delay(500);
  Serial.println("\nI2C Scanner");
}

void testSlave(byte address)
{
  byte resp[10];


  switch (address) {
    case 0x68:  // mpu6050
    case 0x69:
      Wire.beginTransmission(address);
      Wire.write(byte(0x75));  // get id register from register addr 0x75
      Wire.endTransmission();
      Wire.requestFrom(address, 1);
      resp[0] = Wire.read();
      Serial.print("id: 0x"); Serial.print(resp[0], HEX); Serial.print("\tIMU: mpu6050: 0x68/0x72?(clone?), MPU6000: 0X70, mpu9250: 0x71)");
      break;
    case 0x76:  // bmp280
    case 0x77:  // bmp
      Wire.beginTransmission(address);
      Wire.write(byte(0xd0));  // get id register on addr 0xd0
      Wire.endTransmission();
      Wire.requestFrom(address, 1);
      resp[0] = Wire.read();
      Serial.print("id: 0x"); Serial.print(resp[0], HEX); Serial.print("\tbosch pressure sensor bmp085: 0x55, bmp180: 0x55, bmp280: 0x58)");
      break;
    case 0x1e:  // hmc5883L /hmc5983
      Wire.beginTransmission(address);
      Wire.write(byte(0x0a));  // get id register from addr 0x0a
      Wire.endTransmission();
      Wire.requestFrom(address, 3);
      resp[0] = Wire.read();
      resp[1] = Wire.read();
      resp[2] = Wire.read();
      Serial.print("id: 0x"); Serial.print(resp[0], HEX); Serial.print(" ");
      Serial.print("0x"); Serial.print(resp[1], HEX); Serial.print(" ");
      Serial.print("0x"); Serial.print(resp[2], HEX); Serial.print(" ");
      Serial.print(" HMC: 5983 0x48 0x34 33, HMC5883L 0x48 0x34 0x33  ! same ...");
      break;
    case 0x0d: // qmc5883l
      Wire.beginTransmission(address);
      Wire.write(byte(0x0d));  // get id register from addr 0x0d
      Wire.endTransmission();
      Wire.requestFrom(address, 1);
      resp[0] = Wire.read();
      Serial.print("id: 0x"); Serial.print(resp[0], HEX); Serial.print(" ");
      Serial.print("\tQMC5883L 0xff");
      break;

    default :;
  }

}

void scann(int all)
{
  byte error, address;
  int nDevices;

  Serial.println("\n\n\n\nScanning...");

  nDevices = 0;
  for (address = 1; address < 127; address++ )
  {
    // The i2c_scanner uses the return value of
    // the Write.endTransmisstion to see if
    // a device did acknowledge to the address.
    Wire.beginTransmission(address);
    error = Wire.endTransmission();

    if (error == 0)
    {
      Serial.print("I2C device: 0x");
      if (address < 16)
        Serial.print("0");
      Serial.print(address, HEX);
      Serial.print("  (");
      Serial.print(address);
      Serial.print(") ");
      testSlave(address);
      Serial.println("");

      nDevices++;
    }
    else if (all == 1) {
      Serial.print("0X");
      if (address < 16)
        Serial.print("0");
      Serial.print(address, HEX);

      switch (error) {
        case 1: Serial.println(" err 1: data to long to fit in tx buffer");
          break;
        case 2: Serial.println(" err 2: received NACK on TX of address - NO DEVICE ?!");
          break;
        case 3: Serial.println(" err 3: received NACK on TX of data");
          break;
        case 4: Serial.println(" err 4: other err");
          break;
        case 5: Serial.println(" err 4: timeout");
          break;
      }
    }
  }

  if (nDevices == 0)
    Serial.println("No I2C devices found\n");
  else {
    Serial.print("found: "); Serial.print(nDevices); Serial.println(" devices");
  }
}

void loop()
{
  Serial.println("show all i2c device addresses");
  scann(1);
  delay(1000);  // wait 1 sec
  Serial.println("show only found found devices");
  scann(0);
  delay(5000);
}




/*

   Wire.endtransmission()  returns ...

  0: success.
  1: data too long to fit in transmit buffer.
  2: received NACK on transmit of address.
  3: received NACK on transmit of data.
  4: other error.
  5: timeout

  XXXXXXXXXXXXXXXXXXXXXXXX
  mpu6500 et al
  https://wolles-elektronikkiste.de/en/mpu9250-9-axis-sensor-module-part-1
  PROBMLEM 9250 IS A 6500 ??
  https://github.com/kriswiner/ESP32/issues/24

  https://www.tindie.com/products/onehorse/all-st-motion-sensor-breakout-board/
*/

Jens