So for the past week I was testing out the MPU9250 module because I need the yaw value for a compass. Yesterday it just stops working so I tought I had wired it up rong or something else but I checkt everything and als the I2C was still working on the board. In the evening I started it up again and it works again so I was happy. Today I start working again but after like 5 to 10 min it just randomly stops working again. I checked I2C connection and nothing was connected to it. After a hour and a half I pluged it back in and it works again. I don't know why it acts like that.
Here is the code I am running to check if there is a connection with I2C:
#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");
}
static constexpr uint8_t AK8963_ADDRESS {0x0C}; // Address of magnetometer
static constexpr uint8_t AK8963_WHOAMI_DEFAULT_VALUE {0x48};
byte cb = readByte(AK8963_ADDRESS, AK8963_WHO_AM_I);
if (cb == AK8963_WHOAMI_DEFAULT_VALUE) {
Serial.print("AK8963 (Magnetometer) is ready to use");
} else {
Serial.print("AK8963 (Magnetometer) was not found");
}
}
}
void loop() {
}
And this is the Serial monitor log. You see in the time stamps that I left it for a while and the pluged it back in:
12:17:32.852 -> No device found on I2C bus. Please check your hardware connection
12:17:44.654 -> Searching for i2c devices...
12:17:44.692 -> Found 0 I2C devices
12:17:44.692 -> I2C addresses are:
12:17:44.692 -> No device found on I2C bus. Please check your hardware connection
13:48:48.082 -> Searching for i2c devices...
13:48:48.122 -> Found 1 I2C devices
13:48:48.122 -> I2C addresses are: 0x68
13:48:48.122 -> I2C address 0x68 is MPU9250 and ready to use
13:48:48.122 -> AK8963 (Magnetometer) was not found
I also don't know why the magnetometer is not found.