Go Down

Topic: HMC6352 Compass with Arduino Mega (Read 2520 times) previous topic - next topic

Jaaagman

Jun 04, 2012, 07:40 pm Last Edit: Jun 04, 2012, 07:51 pm by Jaaagman Reason: 1
I have an issue when I connect my HMC6352 2-axis sensors to my Mega. Initially, I was getting readings that read anywhere from 0-360 degrees (though non-linearly). Now, I am having this issue where it gets readings of about 54.8 degrees at a starting position. If I rotate it 90 degrees CCW, the angle will start decreasing until get a reading of 36.2 degrees. As I rotate it another 90 degrees in the same direction, the readings will decrease to 34.0 degrees where it starts to increase yet again. As I continue to rotate it CCW, it will continue to increase until it reaches the 270 degree mark when it gets to 71.9 degrees and starts decreasing again. What could be causing the issue? Do I have a bad sensor? Here is the code that I used.

   #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.write("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.read();
       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);
   }


Thanks everyone.

Steph

I've recently built a project that uses this sensor, and run into some similar issues. It would give different readings as I rotated it but they varied widely from what was correct.

What I found was that the sensor really does need to be calibrated, once you have positioned / installed it. The datasheet has information on how to run the calibration process, but the short version is, send it 'C' to enter calibration mode, then rotate the entire project at least twice over about 20 seconds, then send it an 'E' to exit calibration mode. This updates its internal registers so it can zero out any magnetic interference from the rest of the build.

Also there's the obvious - don't put anything magnetic or electromagnetic next to it, such as an electric motor. Even a small pager vibrator can confuse the sensor, if it's close enough.

Jaaagman

Yup that seemed to do the trick. Thank you so much!

Jaaagman

Sorry to double post but I seem to have run into another issue with this module. For a while, I seem to have gotten it to work. I left it alone for a couple of days and when I was testing it out today, I found that it wasn't giving any sort of readings at all. I am getting 255 for both the least significant byte and most significant byte. No matter now I calibrate it, it does not seem to want to work. I have used the same wiring setup as I had previously and I have check it a number of times (I am testing it now with an UNO so analog 4 for SDA and analog 5 for SCL). Is my compass module dead?

Docedison

Is it still powered up? is it drawing current?

Doc
--> WA7EMS <--
"The solution of every problem is another problem." -Johann Wolfgang von Goethe
I do answer technical questions PM'd to me with whatever is in my clipboard

Go Up