#include #define ADDRESS 0x1E void setup(){ Serial.begin(9600); // initialise Serial communication Wire.begin(); Wire.beginTransmission(ADDRESS); Wire.write(0x02); //select mode register Wire.write(0x00); //continuous measurement mode Wire.endTransmission(); } void loop() { long x,y,z; Wire.beginTransmission(ADDRESS); Wire.write(0x03); //select register 3, X MSB register Wire.endTransmission(); //Read data from each axis, 2 registers per axis Wire.requestFrom(ADDRESS, 6); if(Wire.available()) { x = Wire.read() * 256; x += Wire.read(); z = Wire.read() * 256; z += Wire.read(); y = Wire.read() * 256; y += Wire.read(); } float B = sqrt(x*x + y*y + z*z); // print the three components of the field on the Serial monitor // as well as its intensity in arbitrary units // Read the data sheet of the digital compass to convert the // reading in Gauss or Tesla Serial.print(x); Serial.print(" "); Serial.print(y); Serial.print(" "); Serial.print(z); Serial.print(" "); Serial.println(B); delay(250); }