Connecting HMC5883L Compass module to Arduino uno

Compass

http://www.miniinthebox.com/hmc5883l-digital-compass-module_p903477.html?currency=USD&litb_from=paid_adwords_shopping&gclid=CIjQ2-v537wCFUHNOgodPXUAxw#prod_description

I have connected this compass to the arduino uno. However, I just keep having -1 for x y and z so it seems to be not working. Here is the code.

#include <Wire.h>

#define Addr 0x1E // 7-bit address of HMC5883 compass

void setup() {
Serial.begin(9600);
delay(100); // Power up delay
Wire.begin();

// Set operating mode to continuous
Wire.beginTransmission(Addr);
Wire.write(byte(0x02));
Wire.write(byte(0x00));
Wire.endTransmission();
}

void loop() {
int x, y, z;

// Initiate communications with compass
Wire.beginTransmission(Addr);
Wire.write(byte(0x03)); // Send request to X MSB register
Wire.endTransmission();

Wire.requestFrom(Addr, 6); // Request 6 bytes; 2 bytes per axis
if(Wire.available() <=6) { // If 6 bytes available
x = Wire.read() << 8 | Wire.read();
z = Wire.read() << 8 | Wire.read();
y = Wire.read() << 8 | Wire.read();
}

// Print raw values
Serial.print(“X=”);
Serial.print(x);
Serial.print(", Y=");
Serial.print(y);
Serial.print(", Z=");
Serial.println(z);

delay(500);
}

The two most likely problems are not connecting the compass correctly to the Arduino, or forgetting the 4.7 K pullup resistors on SCL and SDA.

There are already pullup resistors to 3.3V on that board (I think I can see them on the photo).

You can use 5V from the Uno to the 5V of the sensor board, without using the 3.3V pin either one.

Did you run the i2c_scanner ? http://playground.arduino.cc/Main/I2cScanner

this code works fine on my Mega with the L883 chip.

William