I am using an HMC5883L magnetometer and arduino uno and i am not able to read the values .i am getting everything as -1,-1,-1.
pls help me
thanks in advance.
the code i used is
#include <Wire.h>
//#include <LiquidCrystal.h>
//LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
#define Addr 0x1E // 7-bit address of HMC5883 compass
void setup() {
//lcd.begin(16, 2);
Serial.begin(9600);
Wire.begin(0x1E);
Wire.beginTransmission(Addr);
Wire.write(0x00);
Wire.write(0x01);
Wire.endTransmission();
}
void loop() {
int x, y, z, k;
// Initiate communications with compass
Wire.beginTransmission(Addr);
Wire.write(0x03); // Send request to X MSB register
Wire.endTransmission();
Wire.requestFrom(Addr,6); // Request 6 bytes; 2 bytes per axis
if (Wire.available()) { // If 6 bytes available
x = Wire.read() << 8 ;
x |= Wire.read();
Serial.print(x);
z = Wire.read() << 8 | Wire.read();
Serial.print(z);
y = Wire.read() << 8 | Wire.read();
Serial.print(y);
}
//lcd.setCursor(0, 0);
//Serial.print("Digital Compass");
// Display the angle in LCD with respect to North
float h = (atan2(x,y)) / 0.0174532925;
//Serial.print(h);
if (h < 0)
{
h = h+360;
}
if (h>360)
{
h = 360 - h;
}
// lcd.setCursor(1, 1);
k = int(h);
Serial.print(k);
//lcd.print((char)223);
if (k <= 45)
{
Serial.print("North");
}
else
{
if (k <= 90)
{
Serial.print("North-East");
}
else
{
if ( k <= 135)
{
Serial.print("East");
}
else
{
if (k <= 180)
{
Serial.print("South-East");
}
else
{
if (k <= 225)
{
Serial.print("South");
}
else
{
if (k <= 270)
{
Serial.print("South-West");
}
else
{
if (271 <= k <= 315)
{
Serial.print("West");
}
else
{
if (k <= 360)
{
Serial.print("North-West");
}
}}}}}}}
delay(200);
//lcd.clear();
}