0% found this document useful (0 votes)
429 views1 page

Include - Wire.h

This code configures an Arduino to communicate with an HMC5883 magnetometer over I2C to continuously read magnetic field data from its three axes. It sets the I2C address and register for continuous measurement mode, then in a loop reads the 6 bytes of x, y, z axis data from register 3, combining the 2 bytes for each value, and prints the results to the serial monitor every 500ms.

Uploaded by

Losa Fernandez
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
429 views1 page

Include - Wire.h

This code configures an Arduino to communicate with an HMC5883 magnetometer over I2C to continuously read magnetic field data from its three axes. It sets the I2C address and register for continuous measurement mode, then in a loop reads the 6 bytes of x, y, z axis data from register 3, combining the 2 bytes for each value, and prints the results to the serial monitor every 500ms.

Uploaded by

Losa Fernandez
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

#include <Wire.

h> //I2C Arduino Library

#define addr 0x1E //I2C Address for The HMC5883

void setup(){

[Link](9600);
[Link]();

[Link](addr); //start talking


[Link](0x02); // Set the Register
[Link](0x00); // Tell the HMC5883 to Continuously Measure
[Link]();
}

void loop(){

int x,y,z; //triple axis data

//Tell the HMC what regist to begin writing data into


[Link](addr);
[Link](0x03); //start with register 3.
[Link]();

//Read the data.. 2 bytes for each axis.. 6 total bytes


[Link](addr, 6);
if(6<=[Link]()){
x = [Link]()<<8; //MSB x
x |= [Link](); //LSB x
z = [Link]()<<8; //MSB z
z |= [Link](); //LSB z
y = [Link]()<<8; //MSB y
y |= [Link](); //LSB y
}

// Show Values
[Link]("X Value: ");
[Link](x);
[Link]("Y Value: ");
[Link](y);
[Link]("Z Value: ");
[Link](z);
[Link]();

delay(500);
}

You might also like