We have a costume pcb with HMC5883, the circuit is the same as this one :
https://www.sparkfun.com/tutorials/301
All the capacitors in place, and if I check with an IC2 connection test program from here, He find the device at 0x48 (data sheet says 0x1E).
When I am taking measurements, I always get the same values no matter what,
3769 4004 3533
3769 4004 3533
3769 4004 3533
3769 4004 3533
3769 4004 3533
3769 4004 3533
and angle is 49.5 constant .
This is the program :
#include <Wire.h>
#define DEVICE (0x1E) //1E
#define TO_READ (6)
byte buff[TO_READ] ;
const float xOffset = 0;//103.0; // Offset required to adjust x coordinate to zero origin
const float yOffset =0;// -165.0; // Offset required to adjust y coordinate to zero origin
const float declination = 70.1; // Enter magnetic declination mrads here (local to your geo area)
void setup()
{
Serial.begin(9600);
while (!Serial)
{;}
Serial.println("Started");
Wire.begin();
//measurment mode
writeTo(DEVICE, 0x02, 0x00); //set measuring mode
writeTo(DEVICE, 0x01, 0x20); // set scaling to +1.3Ga
}
void loop()
{
int x=0, y=0, z=0,xs=0,ys=0,zs=0;
uint8_t x_msb; // X-axis most significant byte
uint8_t x_lsb; // X-axis least significant byte
uint8_t y_msb; // Y-axis most significant byte
uint8_t y_lsb; // Y-axis least significant byte
uint8_t z_msb; // Z-axis most significant byte
uint8_t z_lsb; // Z-axis least significant byte
// Get the value from the sensor
readFrom(DEVICE, 0x3,(uint8_t)1, &x_msb) ;
readFrom(DEVICE, 0x4,(uint8_t)1, &x_lsb) ;
readFrom(DEVICE, 0x5,(uint8_t)1, &z_msb) ;
readFrom(DEVICE, 0x6,(uint8_t)1, &z_lsb) ;
readFrom(DEVICE, 0x7,(uint8_t)1, &y_msb) ;
readFrom(DEVICE, 0x8,(uint8_t)1, &y_lsb) ;
x = x_msb << 8 | x_lsb;
y = y_msb << 8 | y_lsb;
z = z_msb << 8 | z_lsb;
float gScale = .92; // Scale factor for +1.3Ga setting
float adjx = x - xOffset;
float adjy = y - yOffset;
xs = adjx * gScale;
ys = adjy * gScale;
zs = z * gScale;
Serial.println(xs,DEC);
Serial.print(" ");
Serial.print(ys,DEC);
Serial.print(" ");
Serial.print(zs,DEC);
Serial.print(" ");
float heading = atan2(ys, xs);
heading += declination / 1000; // Declination for geo area
if (heading < 0);
heading += 2*PI;
if (heading > 2*PI)
heading -= 2*PI;
float angle = heading * 180/M_PI;
//Serial.print(heading);
//Serial.print(" A: ");
//Serial.println(angle);
delay(100);
}
//READ WRITE FUNCTIONS TO AX
void writeTo(int device, byte address, byte val)
{
Wire.beginTransmission(device); //start transmission to device
Wire.write(address); // send register address
Wire.write(val); // send value to write
Wire.endTransmission(); //end transmission
}
void readFrom(int device, byte address, int num, byte buff[])
{
Wire.beginTransmission(device); //start transmission to device
Wire.write(address); //sends address to read from
Wire.endTransmission(); //end transmission
Wire.requestFrom(device, num); // request 6 bytes from device
int i = 0;
while(Wire.available()) //device may send less than requested (abnormal)
{
buff[i] = Wire.read(); // receive a byte
i++;
}
}
How can we debug this? How can i know that i do get data from the chip and this is not just garbage? what could go wrong ?
Thanks .