Problem connecting Lilypad to HMC6352

Hello! This is my first post regarding my first ever experience with microcontrollers. So far it has been great fun even though I haven’t done anything but make a light blink :smiley:

Anyway I’m trying to connect my HMC6352 to my Lilypad. I have the data line connected to 4, the timing line to 5, VCC to + and GROUND to -.

I’ve tried variants on what code to use but this is what I have currently:
#include <Wire.h>
int HMC6352Address = 0x42;
// This is calculated in the setup() function
int slaveAddress;
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 = 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);*
    }
    The code compiles and uploads OK but the heading is ALWAYS 0.0. It’s like the compass unit isn’t receiving the commands, or it is receiving them but returning nothing. Any ideas? I’m not even sure what my next step should be…I read something about reducing the power from 5V to 3.3V but I’m not sure I’m comfortable doing that just yet if it involves cutting up my Lilypad!

I read that the port number might be a problem. I don't have my Arduino with me at the moment to test this, but I do recall that it was using port 19 on Vista. What confuses me about this is that I was receiving "some" data in the form of "heading: 0.0". It seems that if the port number was the problem I would receive nothing at all, right? It seems to me that the problem has to be the HMC unit itself, or my connection to pins 4 and 5..?

It’s frustrating not getting what one would expect. As I interfaced an HMC6352 not so long ago I suspect this is an issue with your code:

i = 0;
while(Wire.available() && i < 2)
{
headingData = Wire.receive();

  • i++;*
    }
    The “while” condition will never be true as "Wire.available is anded with i which is zero and so the condition is false. If you add parentheses so it reads “(i < 2)” you should be ok. Rather than using the while loop however you may use the following:
    if (Wire.available==2) {
  • headingValue=Wire.receive();*
  • headingValue=headingValue<<8;*
  • headingValue|=Wire.receive;*
    } else {
  • Serial.println(“Compass is not responding”);*
    }
    Also note that the 70 microseconds reference in your code is a minimum delay between commands. It will take a minimum of 6 milliseconds (6000 microseconds) to prepare a new sample.
    Also a purist would change Wire.send(“A”) to Wire.send(‘A’) as you wll be sending a single byte command as opposed to a string.