So im trying to get this baby working. My arduino sense the magnetic compass but just read out 0 degrees. It looks like my compass is very similar to the HMC6352 so i tryed different codes and playing with them but no luck.
#include <Wire.h>
int compassAddress = 0x42 >> 1; // From datasheet compass address is 0x42
// shift the address 1 bit right, the Wire library only needs the 7
// most significant bits for the address
int reading = 0;
void setup()
{
Wire.begin(); // join i2c bus (address optional for master)
Serial.begin(9600); // start serial communication at 9600bps
pinMode(48, OUTPUT);
digitalWrite(48, HIGH);
}
void loop()
{
// step 1: instruct sensor to read echoes
Wire.beginTransmission(compassAddress); // transmit to device
// the address specified in the datasheet is 66 (0x42)
// but i2c adressing uses the high 7 bits so it's 33
Wire.send('A'); // command sensor to measure angle
Wire.endTransmission(); // stop transmitting
// step 2: wait for readings to happen
delay(1000); // datasheet suggests at least 6000 microseconds
// step 3: request reading from sensor
Wire.requestFrom(compassAddress, 2); // request 2 bytes from slave device #33
// step 4: receive reading from sensor
if(2 <= Wire.available()) // if two bytes were received
{
reading = Wire.receive(); // receive high byte (overwrites previous reading)
reading = reading << 8; // shift high byte to be high 8 bits
reading += Wire.receive(); // receive low byte as lower 8 bits
reading /= 10;
Serial.println(reading); // print the reading
}
delay(2000); // wait for half a second
}
have you tried int compassAddress = 0x42; - so without the shift ?
IIRC - Wire.endTransmission(); returns a value that can be checked if transmission worked...
Change:
Wire.send('A'); // command sensor to measure angle
=> #define COMPASS_MEASURE 0x65 // use the hex codes as stated in the PDF
Wire.send(COMPASS_MEASURE);
YOu missed to send command 0x74 or 0x76 on page 3 - start sampling
Finally getting som numbers out. But doesnt make sense because readings are just for the declination angle. Found a xls dokument stating the second mode (reading the data) should have 3 slave write addresses: 77-65 and 61.
Did my best to paste in a copy of the spreadsheet:
The first mode slave_write_address slave_command function
0X42 0X76 stop continuously update
0X42 0X70 stop cabilation
0X42 0X72 stop
0X42 0X74 stop