Let's take a look at the first part of the code:
#include <Wire.h> //I2C Arduino Library
#define address 0x1E //0011110b, I2C 7bit address of HMC5883
void setup(){
//Initialize Serial and I2C communications
Serial.begin(9600);
Wire.begin();
//Put the HMC5883 IC into the correct operating mode
Wire.beginTransmission(address); //open communication with HMC
5883
Wire.send(0x02); //select mode register
Wire.send(0x00); //continuous measurement mode
Wire.endTransmission();
The code that resides within the setup() function is ran once at the beginning. It
initializes the serial communication at 9600 baud. We use the serial communication to
send data for each axis back to the computer. The I2C is also initialized. We then do
a 'write' operation to the HMC5883L. The purpose of this 'write' operation is to adjust
the value in the configuration register of the HMC5883L to tell it to be in continuous
operation mode. This lets us make continuous reads of the axis data. By default the
chip is in single read mode meaning after reading from it once, it will go idle to save
power. Once idle, we have write to it to turn it on before we can read from it again.
The datasheet has all the information about the registers in addition to other useful
information. Of course, we always encourage you to RTFM... just look at the
datasheet...
Moving on, here's where we actually request and receive the data:
Page 5 of 7