Hey guys,
I've got an HMC6352 digital compass sitting in front of me, and for the life of me I can't make it work. I've read everything I can find on using it with the Arduino, and still can't make it work. I've tried doing what is said in the playground, including:
- SDA to Analog pin 4 and SCL to analog pin 5
- No pullup resistors on SDA and SCL
- Using 3v (also tried 5V) instead of 5v to the Vcc of the compass module.
I'm using the code from the SparkFun site:
#include <Wire.h>
int HMC6352Address = 0x42;
int slaveAddress; // This is calculated in the setup() function
int ledPin = 13;
boolean ledState = false;
byte headingData[2];
int i, headingValue;
void setup()
{
// Shift the device's documented slave address (0x42) 1 bit right
// This compensates for how the TWI library only wants the
// 7 most significant bits (with the high bit padded with 0)
slaveAddress = HMC6352Address >> 1; // This results in 0x21 as the address to pass to TWI
Serial.begin(9600);
pinMode(ledPin, OUTPUT); // Set the LED pin as output
Wire.begin();
}
void loop()
{
// Flash the LED on pin 13 just to show that something is happening
// Also serves as an indication that we're not "stuck" waiting for TWI data
ledState = !ledState;
if (ledState) {
digitalWrite(ledPin,HIGH);
}
else
{
digitalWrite(ledPin,LOW);
}
// Send a "A" command to the HMC6352
// This requests the current heading data
Wire.beginTransmission(slaveAddress);
Wire.send("A"); // The "Get Data" command
Wire.endTransmission();
delay(10); // The HMC6352 needs at least a 70us (microsecond) delay
// after this command. Using 10ms just makes it safe
// Read the 2 heading bytes, MSB first
// The resulting 16bit word is the compass heading in 10th's of a degree
// For example: a heading of 1345 would be 134.5 degrees
Wire.requestFrom(slaveAddress, 2); // Request the 2 byte heading (MSB comes first)
i = 0;
while(Wire.available() && i < 2)
{
headingData[i] = Wire.receive();
i++;
}
headingValue = headingData[0]*256 + headingData[1]; // Put the MSB and LSB together
Serial.print("Current heading: ");
Serial.print(int (headingValue / 10)); // The whole number part of the heading
Serial.print(".");
Serial.print(int (headingValue % 10)); // The fractional part of the heading
Serial.println(" degrees");
delay(500);
}
When running this, it only ever output heading: 0.0. So I started ripping apart the code and found that even after requesting data from the device, it the device doesn't respond. I don't have an oscilliscope, but Wire.available() only ever returns 0.
Anyone got any clues? We really need to get this working asap, we went for a digital i2c compass for ease of use and quick connectivity, but apparently that's not going to happen?
Thanks so much for any help anyone can give me!!
(Using Duemilanove with software 0018, and yes I've quadruple checked to make sure all the connections are proper).