Home Sensor Projects HMC5883L example

HMC5883L example

by shedboy71

The HMC5883L is a 3-axis magnetometer. The most common include usage of the chip is as a digital compass to sense direction or using them to detect ferrous metals.

The Honeywell HMC5883L is a surface-mount, multi-chip module designed for low-field magnetic sensing with a digital interface for applications such as lowcost compassing and magnetometry. The HMC5883L includes our state-of-theart, high-resolution HMC118X series magneto-resistive sensors plus an ASIC containing amplification, automatic degaussing strap drivers, offset cancellation, and a 12-bit ADC that enables 1° to 2° compass heading accuracy.

The I2Cserial bus allows for easy interface. The HMC5883L is a 3.0×3.0x0.9mm surface mount 16-pin leadless chip carrier (LCC). Applications for the HMC5883L include Mobile Phones, Netbooks, Consumer Electronics, Auto Navigation Systems, and Personal Navigation Devices.

Features

Three-Axis Magnetoresistive Sensors
Small Size for Highly Integrated Products. Just Add a MicroController Interface, Plus Two External SMT Capacitors
12-Bit ADC Coupled with Low Noise AMR Sensors Achieves 5 milli-gauss Resolution in ±8 Gauss Fields
Enables 1° to 2° Degree Compass Heading Accuracy
Built-In Self Test
Low Voltage Operations (2.16 to 3.6V) and Low Power Consumption (100 μA)
Compatible for Battery Powered Applications
I2C Digital Interface
Fast 160 Hz Maximum Output Rate

Now unless you have precision SMT assembly tools you will need to get this in a breakout module, here is the one I bought.

HMC5883L

HMC5883L

HMC5883L

/*
Analog input 4 I2C SDA
Analog input 5 I2C SCL
*/

Code

#include <Wire.h>
 
#define address 0x1E //I2C 7bit address of HMC5883
 
void setup(){
 
Serial.begin(9600);
Wire.begin();
 
//Put the HMC5883 IC into the correct operating mode
Wire.beginTransmission(address);
Wire.write(0x02);
Wire.write(0x00);
Wire.endTransmission();
}
 
void loop(){
 
int x,y,z
 
//begin reading data
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(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);
}

 

 

Links

HMC5883L at Amazon US

HMC5883L at Amazon UK

You may also like

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More