Magnetic – HMC6352

The HMC6352 will tell you the compass heading in degrees. It does not have tilt compensation, so you must keep it level to get an accurate measurement.

  • Simple I2C interface
  • 2.7 to 5.2V supply range
  • 1 to 20Hz selectable update rate
  • True drop-in solution
  • 0.5 degree heading resolution
  • 1 degree repeatability
  • Supply current : 1mA @ 3V

Connecting to an Arduino

  • GND to GND
  • VCC to 5v
  • SDA (data line) to Analog Input 4
  • SCL (clock line) to Analog Input 5

Arduino code

// Sample code for the HMC6352 Compass Module.
// Based on this Wiring example http://wiring.org.co/learning/libraries/hmc6352sparkfun.html
#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
}
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(10);               // 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(50);                   // wait
}