Magnetometer - I2C, HMC5883L - data received is question marks - circuit damage?

Hi -

Data should be appearing as in this video at 6.20 seconds, but I'm only getting horizontal question marks like so: ????, ????, ????, ????

Since I can't see anything wrong with the code (though I am the ultimate beginner to C++, I only have a bit of background in python). I have added it on the bottom though as it could be the code seeing as it's displaying horizontally.

Does anyone know if this means the HMC5883L isn't functional so isn't producing any readings?

Any guidance would be greatly appreciated!

Thanks.

*/

// Arduino Wire library is required if I2Cdev I2CDEV_ARDUINO_WIRE implementation
// is used in I2Cdev.h
#include "Wire.h"

// I2Cdev and HMC5883L must be installed as libraries, or else the .cpp/.h files
// for both classes must be in the include path of your project
#include "I2Cdev.h"
#include "HMC5883L.h"

// class default I2C address is 0x1E
// specific I2C addresses may be passed as a parameter here
// this device only supports one I2C address (0x1E)
HMC5883L mag;

int16_t mx, my, mz;

#define LED_PIN 13
bool blinkState = false;

void setup() {
    // join I2C bus (I2Cdev library doesn't do this automatically)
    Wire.begin();

    // initialize serial communication
    // (38400 chosen because it works as well at 8MHz as it does at 16MHz, but
    // it's really up to you depending on your project)
    Serial.begin(38400);

    // initialize device
    Serial.println("Initializing I2C devices...");
    mag.initialize();

    // verify connection
    Serial.println("Testing device connections...");
    Serial.println(mag.testConnection() ? "HMC5883L connection successful" : "HMC5883L connection failed");

    // configure Arduino LED pin for output
    pinMode(LED_PIN, OUTPUT);
}

void loop() {
    // read raw heading measurements from device
    mag.getHeading(&mx, &my, &mz);

    // display tab-separated gyro x/y/z values
    Serial.print("mag:\t");
    Serial.print(mx); Serial.print("\t");
    Serial.print(my); Serial.print("\t");
    Serial.print(mz); Serial.print("\t");
    
// To calculate heading in degrees. 0 degree indicates North
    float heading = atan2(my, mx);
    if(heading < 0)
      heading += 2 * M_PI;
    Serial.print("heading:\t");
    Serial.println(heading * 180/M_PI);

    // blink LED to indicate activity
    blinkState = !blinkState;
    digitalWrite(LED_PIN, blinkState);
}

Have you set the serial monitor to 38400 Baud (lower right corner)?

The setting must match the Serial.begin() statement in setup().

Ah no I hadn't!! Okay that makes sense - thank you!!! It's still only returning 0's though. :frowning:

"mag: 0 0 0 heading: 0.00
mag: 0 0 0 heading: 0.00
mag: 0 0 0 heading: 0.00
mag: 0 0 0 heading: 0.00"

What was printed as the result of this line?

    Serial.println(mag.testConnection() ? "HMC5883L connection successful" : "HMC5883L connection failed");

Pullup resistors are required for I2C communications to work, and the module has to be fully 5V compatible (possibly with level shifters on the data lines), if you are using a 5 V Arduino. Please post a link to the exact HMC5883L module, and post a hand drawn wiring diagram.

Sorry for delay in response - I've been away!

So it does say that the connection has failed. So that means the arduino isn't connected to magnetometer right.

It is the GY273 module. My supervisor who managed to get a signal from it before reckons it's a 3.3V connection but the following link says 5V, but I orignally had it on 5V and the result was still the same.

I also tried out the test code on the above link in case as the code I was using didn't mention about assigning registers but it's still producing 0s.

:confused: :frowning:

#include <Wire.h> //I2C Arduino Library

#define addr 0x1E //I2C Address for The HMC5883

void setup(){
  
  Serial.begin(9600);
  Wire.begin();
  
  
  Wire.beginTransmission(addr); //start talking
  Wire.write(0x02); // Set the Register
  Wire.write(0x00); // Tell the HMC5883 to Continuously Measure
  Wire.endTransmission();
}


void loop(){
  
  int x,y,z; //triple axis data

  //Tell the HMC what regist to begin writing data into
  Wire.beginTransmission(addr);
  Wire.write(0x03); //start with register 3.
  Wire.endTransmission();
  
 
 //Read the data.. 2 bytes for each axis.. 6 total bytes
  Wire.requestFrom(addr, 6);
  if(6<=Wire.available()){
    x = Wire.read()<<8; //MSB  x 
    x |= Wire.read(); //LSB  x
    z = Wire.read()<<8; //MSB  z
    z |= Wire.read(); //LSB z
    y = Wire.read()<<8; //MSB y
    y |= Wire.read(); //LSB y
  }
  
  // Show Values
  Serial.print("X Value: ");
  Serial.println(x);
  Serial.print("Y Value: ");
  Serial.println(y);
  Serial.print("Z Value: ");
  Serial.println(z);
  Serial.println();
  
  delay(500);
}
}

Run the I2C Scanner program to see if it detects the module at the correct address.

If not, the wiring is wrong or the module is dead.

Thank you so much for your help that led me to the solution. Some HMC5883L modules apparently have a chip that makes it a QMC5883L so the address wasn't the same and now it is working fine. :smiley:

Thanks again!