I'm trying to connect the MPU 6050 module and HMC 5883L module for my IMU of the Autonomous Car. But there is one i2c connection in Nano (A4, A5). When I run the code for MPU 6050, it shows the results. the code is here. The circuit diagram
#include <MPU6050_tockn.h>
#include <Wire.h>
MPU6050 mpu6050(Wire);
void setup() {
Serial.begin(9600);
Wire.begin();
mpu6050.begin();
mpu6050.calcGyroOffsets(true);
}
void loop() {
mpu6050.update();
Serial.print("angleX : ");
Serial.print(mpu6050.getAngleX());
Serial.print("\tangleY : ");
Serial.print(mpu6050.getAngleY());
Serial.print("\tangleZ : ");
Serial.println(mpu6050.getAngleZ());
}
But when I run the Magnetometer Code, it doesn't give the data.
#include <Wire.h>
#include <DFRobot_QMC5883.h>
DFRobot_QMC5883 compass;
void setup()
{
Serial.begin(115200);
while (!compass.begin())
{
Serial.println("Could not find a valid QMC5883 sensor, check wiring!");
delay(500);
}
if(compass.isHMC()){
Serial.println("Initialize HMC5883");
compass.setRange(HMC5883L_RANGE_1_3GA);
compass.setMeasurementMode(HMC5883L_CONTINOUS);
compass.setDataRate(HMC5883L_DATARATE_15HZ);
compass.setSamples(HMC5883L_SAMPLES_8);
}
else if(compass.isQMC()){
Serial.println("Initialize QMC5883");
compass.setRange(QMC5883_RANGE_2GA);
compass.setMeasurementMode(QMC5883_CONTINOUS);
compass.setDataRate(QMC5883_DATARATE_50HZ);
compass.setSamples(QMC5883_SAMPLES_8);
}
}
void loop()
{
Vector norm = compass.readNormalize();
float heading = atan2(norm.YAxis, norm.XAxis);
float declinationAngle = (4.0 + (26.0 / 60.0)) / (180 / PI);
heading += declinationAngle;
if (heading < 0){
heading += 2 * PI;
}
if (heading > 2 * PI){
heading -= 2 * PI;
}
// Convert to degrees
float headingDegrees = heading * 180/M_PI;
// Output
Serial.print(" Heading = ");
Serial.print(heading);
Serial.print(" Degress = ");
Serial.print(headingDegrees);
Serial.println();
delay(100);
}
I think you've done this right. the MPU6050 has a slave I2C bus that is used to connect the compass. You have wired the setup accordingly.
You need to configure the MPU correctly, but then it will talk to the compass without the Arduino even knowing of that. You can talk to the HMC, but only trough the MPU with special commands. The device scan will not show the HMC, since it is not directly visible from the Arduino. In this setup, you can't use the magnetometer code you show here. You need to use the MPU6050 binding and configure it for a slave compass (I do not know whether any Arduino MPU6050 library supports this feature, though.)