Account
Projects Forums Code & Files Design Sketches Photos Videos Chat Sites People Sounds  
 
  •  
 
 
Articles
05.02.2010

Ok so I struggled a bit to get this to work so it makes sense to post it here in case you are having a similar problem getting HMC6352 compass module to work with Arduino. The code below is not mine- I got it from Sparkfun’s forums

Using Arduino NG rev C board and Arduino IDE v.0008. It didn’t work when I compiled and uploaded the code with v.0007. So don’t use it. SDL and SCL lines don’t have any pullup resistors in my setup. Probably because v.0008 enables the pullup resistors on ATmega168.

From v.0008 UPDATES file
Activating TWI/I2C pullup resistors on the ATmega168 (in addition to the ATmega8).

NOTES

*Using Arduino NG rev C board and Arduino IDE v.0008

*SDA to Analog pin 4 and SCL to analog pin 5

*No pullup resistors on SDA and SCL

*Using 3v instead of 5v to the Vcc of the compass module.

CODE:

#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[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);
} 

Via: http://recombine.net/blog/article/49/hmc6352-sparkfun-compass-and-arduino

 
Comments
Order by: 
Per page: 
 
  •  asicsning wrote 74 Days Ago (neutral) 
     
    0
    http://www.ghdhaironlineaustralia.com/sitemap.xml
     
       
     
     
    -1 point
     
  •  jackpercy wrote 105 Days Ago (neutral) 
     
    0
    October 13, 5:30 p.m. or so, in foshan, Canada Goose sale guangdong HuangQi traffic accident happened, a van to hit a 2 years after the girls, make a bit, but did not hesitate to get off, but to start saving once again from the girl who run over, and then have a car run again. In a few minutes after, 18 people who pass through the scene, but no one reaches or call the police, until 19 th also is a SheHuang aunt found only after the lift. According to the latest report, girls Canada goose parka sale has been brain death, life could happen at any time, even dangerous estimated future brain-damaged states can't recover. http://www.canadagoosejacket2011sale.com/
     
       
     
     
    -2 points
     
  •  asicsning wrote 176 Days Ago (neutral) 
     
    0
    http://www.toryburchusashoes.com/
     
       
     
     
    -3 points
     
  •  asicsning wrote 189 Days Ago (neutral) 
     
    0
    http://www.ghdstraightenersuksale.co.uk/
     
       
     
     
    -3 points
     
  •  asicsning wrote 195 Days Ago (neutral) 
     
    0
    [url=http://www.mbttrainersuksale.co.uk/]MBT[/url] [url=http://www.mbtukshoessale.co.uk/]MBT Shoes[/url] [url=http://www.oakleyuksunglassessale.co.uk/]Oakley Sunglasses[/url] [url=http://www.cheapghdstraightenerssale.co.uk/]GHD Straighteners[/url] [url=http://www.cheapuggsaleuk.co.uk/]UGG Boots[/url]
     
       
     
     
    -3 points
     
  •  bongbot wrote 697 Days Ago (neutral) 
     
    0
    Thanks a bunch for posting this, it ended days of frustration on my part, i would have never figured out i needed to move the slave address over.
     
       
     
     
    2 points
     
Actions
Rating
3 votes
Copyright © 2012 Arduino User Community -
Not affiliated with or endorsed by Arduino (Arduino.cc)