Hello,
I have recently bought an HMC5883L 3-Axis Digital compass module to use it with arduino.
I soldered the pins myself as you can see in the pic.
And I connected it to my arduino Uno using 5v Vcc, SCL to pin A5, SDA to pin A4 (I2C protocol) and I used pull up 4.7K resistors.
I tested it with an I2C Scanner and I found the device on the address 0x1E, then I used a simple example to read from it but it seems to give almost same values whatever I rotate it in all directions, with Z being always 0.
I wrote this code to see all the values in the 13 registers inside the device:
#include <Wire.h> //I2C Arduino Library
#define addr 0x1E //I2C Address for The HMC5883
void setup(){
Serial.begin(9600);
Serial.print("HMC5833L COMPASS SENSOR BEGIN");
Serial.println();
Wire.begin();
Wire.beginTransmission(addr);
Wire.write(0x02);
Wire.write(0x00);
Wire.endTransmission();//Set measueremnt mode to continuous.
}
void loop(){
int cra,crb,mr,xm,xl,zm,zl,ym,yl,sr,ira,irb,irc; // Variables to read from registers
Wire.beginTransmission(addr);//Set the pointer to the first register to start reading.
Wire.write(0x00);
Wire.endTransmission();
Wire.requestFrom(addr, 9);
if(9<=Wire.available()){
cra = Wire.read();
crb= Wire.read();
mr = Wire.read();
xm= Wire.read();
xl = Wire.read();
zm= Wire.read();
zl= Wire.read();
ym= Wire.read();
yl= Wire.read();
}
Wire.beginTransmission(addr);//Set the pointer to the register 0x09 "Status register"
Wire.write(0x09); //bcz after reading data output the pointer jumps back to 0x03.
Wire.endTransmission();
Wire.requestFrom(addr, 4);
if(4<=Wire.available()){
sr= Wire.read();
ira= Wire.read();
irb= Wire.read();
irc= Wire.read();
}
// Show Values
Serial.print("Configuration register A: ");
Serial.println(cra,HEX);
Serial.print("Configuration register B: ");
Serial.println(crb,HEX);
Serial.print("Mode register: ");
Serial.println(mr,HEX);
Serial.print("X MSB Value: ");
Serial.println(xm,HEX);
Serial.print("X LSB Value: ");
Serial.println(xl,HEX);
Serial.print("Z MSB Value: ");
Serial.println(zm,HEX);
Serial.print("Z LSB Value: ");
Serial.println(zl,HEX);
Serial.print("Y MSB Value: ");
Serial.println(ym,HEX);
Serial.print("Y LSB Value: ");
Serial.println(yl,HEX);
Serial.print("Status register: ");
Serial.println(sr,HEX);
Serial.print("Identification register register A: ");
Serial.println(ira,HEX);
Serial.print("Identification register register B: ");
Serial.println(irb,HEX);
Serial.print("Identification register register C: ");
Serial.println(irc,HEX);
Serial.println();
delay(5000);
}
And I am getting this output in the serial monitor:
There is many weird things in thes registers (ZMSB, ZLSB, Status register):
1- Z MSB and LSB are always 0 no matter what.
2- As said in the datasheet, the status register turns 3 after reading from the mode register, but it
has to go back to 1 after reading the all 6 data output registers.
Here it stays 3 after reading from the 6 output registers.
3- The most significant bit of Configuration register A should be 0 (reserved) as said in the data
sheet, but here is 1 (Idk if that's Ok or not).
I cannot tell what the actual problem here is, why I am getting this weird behavior, so I wanna ask anyone here having experience with these sensors, what might be the problem ??
You can find attached the datasheet for the HMC5883L.




