WebbotLib AVR library
WebbotLib It just does it
  C++ documentation  C documentation

HMC5883L Compass

The HMC5883L compass sensor.
The image shows a carrier board from Sparkfun.
Datasheet: http://dlnmh9ip6v2uc.cloudfront.net/datasheets/Sensors/Magneto/HMC5883L-FDS.pdf
This compass is accessed over I2C at a fixed address of 0x3C. Since the device is really a magnetometer then the floating point maths library is used to calculate the bearing, roll and pitch in degrees.
The raw magnetometer values can also be accessed via the various 'getRaw' functions.
The device allows you to specify a refresh rate (ie how many times per second the readings are updated) and this can be set up in Project Designer. Although not particularly recommended; it is possible to modify the refresh rate at run time using the various 'refresh' functions.
Assuming you have called the device 'myCompass' in Project Designer then here is some example code:
// read the compass values and store the results
// Print out the values in degrees
cout << " Bearing:" << myCompass.getBearing();
cout << " Roll:" << myCompass.getRoll();
cout << " Pitch:"   << myCompass.getPitch();
// Or just dump everything in one go
cout << myCompass;
// Alternatively the raw x,y,z magnetometer values can be shown
cout << Raw: << myCompass.getRawX() << ",";
cout << myCompass.getRawY() << ",";
cout << myCompass.getRawZ();
Function Summary

Valid XHTML 1.0 Transitional