Features
- Power 3-5V
- Chipset HMC5883L
- Communication via I2C protocol
- Dimensions 14.8 x 13.5 x 3.5mm
Reference Schematic

Arduino Example
When testing this unit, I found Arduino internal pull-ups are not that well working and I2C signal looked really bad and Wire library was hanging on endTransmission(), so I lowered I2C clock to 25kHz and that seems to help. The code below should be hot-pluggable, but Wire library may still hang. Upload the sketch and open Serial Monitor on 9600Bd.
/*
* HMC5883L Demo.
* dipmicro electronics, 2014
*
* Hardware Used:
*
* Arduino UNO or compatible
* GY271 module (dipmicro part DE4196
* Arduino GND -> GY271/HMC5883L GND
* Arduino 3.3V -> GY271/HMC5883L VCC
* Arduino A4 (SDA) -> GY271/HMC5883L SDA
* Arduino A5 (SCL) -> GY271/HMC5883L SCL
*/
#include <Wire.h> //I2C Arduino Library
#define HMC5883L_ADDR 0x1E //0011110b, I2C 7bit address of HMC5883
bool haveHMC5883L = false;
bool detectHMC5883L ()
{
// read identification registers
Wire.beginTransmission(HMC5883L_ADDR); //open communication with HMC5883
Wire.write(10); //select Identification register A
Wire.endTransmission();
Wire.requestFrom(HMC5883L_ADDR, 3);
if(3 == Wire.available()) {
char a = Wire.read();
char b = Wire.read();
char c = Wire.read();
if(a == 'H' && b == '4' && c == '3')
return true;
}
return false;
}
void setup()
{
//Initialize Serial and I2C communications
Serial.begin(9600);
Serial.println("GY271 TEST");
Wire.begin();
// lower I2C clock http://www.gammon.com.au/forum/?id=10896
TWBR = 78; // 25 kHz
TWSR |= _BV (TWPS0); // change prescaler
}
void loop()
{
bool detect = detectHMC5883L();
if(!haveHMC5883L)
{
if(detect)
{
haveHMC5883L = true;
Serial.println("We have HMC5883L, moving on");
// Put the HMC5883 IC into the correct operating mode
Wire.beginTransmission(HMC5883L_ADDR); //open communication with HMC5883
Wire.write(0x02); //select mode register
Wire.write(0x00); //continuous measurement mode
Wire.endTransmission();
}
else
{
Serial.println("No HMC5883L detected!");
delay(2000);
return;
}
}
else
{
if(!detect) {
haveHMC5883L = false;
Serial.println("Lost connection to HMC5883L!");
delay(2000);
return;
}
}
int x,y,z; //triple axis data
//Tell the HMC5883 where to begin reading data
Wire.beginTransmission(HMC5883L_ADDR);
Wire.write(0x03); //select register 3, X MSB register
Wire.endTransmission();
//Read data from each axis, 2 registers per axis
Wire.requestFrom(HMC5883L_ADDR, 6);
if(6<=Wire.available()){
x = Wire.read()<<8; //X msb
x |= Wire.read(); //X lsb
z = Wire.read()<<8; //Z msb
z |= Wire.read(); //Z lsb
y = Wire.read()<<8; //Y msb
y |= Wire.read(); //Y lsb
}
//Print out values of each axis
Serial.print("x: ");
Serial.print(x);
Serial.print(" y: ");
Serial.print(y);
Serial.print(" z: ");
Serial.println(z);
delay(250);
}


