3 Axis Compass

The 3-Axis Compass module measures magnetic fields in three directions – or axes, labeled X, Y, and Z. In its most simple form, the module can be used as a basic compass to find earth’s magnetic north.

Supported Products

  • Compass Module 3-Axis HMC5883L
  • SparkFun Triple Axis Magnetometer Breakout - HMC5883L

Includes

#include

Decleration Code

// 7-bit address of HMC5883 compass
#define Addr 0x1E

Setup Code

Wire.begin();
// Set operating mode to continuous
Wire.beginTransmission(Addr);
Wire.write(byte(0x02));
Wire.write(byte(0x00));
Wire.endTransmission();

EveryUpdate

int x, y, z;
// Initiate communications with compass
Wire.beginTransmission(Addr);
Wire.write(byte(0x03));             // Send request to X MSB register
Wire.endTransmission();
Wire.requestFrom(Addr, 6);        // Request 6 bytes; 2 bytes per axis
if(Wire.available() <=6) {        // If 6 bytes available
    x = Wire.read() << 8 | Wire.read();
    z = Wire.read() << 8 | Wire.read();
    y = Wire.read() << 8 | Wire.read();
}
// Transform values from 0 to 1023 to 0.0 to 1.0
Output_XActivation = transform(x, 0, 1023, 0.0, 1.0);
Output_YActivation = transform(y, 0, 1023, 0.0, 1.0);
Output_ZActivation = transform(z, 0, 1023, 0.0, 1.0);

No required source files.

To use this node in your Embrio project, either add it from the Library toolbar or copy the XML in the below box and paste it into a node screen.

View Comments (0)