# Adafruit HMC5883L Breakout - Triple-Axis Magnetometer Compass Sensor

## Overview

![](https://cdn-learn.adafruit.com/assets/assets/000/014/635/medium800/adafruit_products_1746_LRG.jpg?1393609675)

He told you "Go West, young maker!" - but you don't know which way is West! Ah, if only you had this triple-axis magnetometer compass module. A magnetometer can sense where the strongest magnetic force is coming from, generally used to detect magnetic north.   
  
We based this breakout on a popular and well loved magnetometer, the HMC5883L. This compact sensor uses I2C to communicate and its very easy to use. Since it's a 3.3V max chip, we added circuitry to make it 5V-safe logic and power, for easy use with either 3 or 5V microcomtrollers. Simply connect VCC to +3-5V and ground to ground. Then read data from the I2C clock and data pins. There's also a Data Ready pin you can use to speed up reads (check the [datasheet](http://www.adafruit.com/datasheets/HMC5883L_3-Axis_Digital_Compass_IC.pdf) for details)

![](https://cdn-learn.adafruit.com/assets/assets/000/014/636/medium800/adafruit_products_1746quarter_LRG.jpg?1393609683)

This is the same magnetometer sensor that is _inside_ the LSM303. So i[f you want an accelerometer as well as a magnetometer, check out the LSM303](http://www.adafruit.com/products/1120 "Link: http://www.adafruit.com/products/1120") - it has basically this sensor + a nice digital 3-axis accelerometer!  
  
**Specs:**

- I2C interface
- 1-2 degree heading accuracy
- Integrated 12-bit ADC
- 160Hz max data rate
- Range of -8 to +8 Gauss

# Adafruit HMC5883L Breakout - Triple-Axis Magnetometer Compass Sensor

## Pinouts

![](https://cdn-learn.adafruit.com/assets/assets/000/014/639/medium800/adafruit_products_1746_LRG.jpg?1393610465)

![](https://cdn-learn.adafruit.com/assets/assets/000/014/652/medium800/adafruit_products_1746quarter_LRG.jpg?1393614516)

This is a fairly simple breakout. The sensor itself is super tiny and in the middle of the PCB. We have all the useful pins broken out in to a 0.1" spaced header at the bottom

- **VIN** - this is the power in pin, give it 3-5VDC. It is reverse-polarity protected. Use the same voltage as you do for the controlling microcontroller's logic. For an Arduino, 5V is best.  
- **GND** - this is the common power and data ground pin
- **3vo** - This is the 3.3V output from the onboard regulator. It will be at 3.3V and you can grab up to 100mA from this to power other items
- **SDA** and **SCL** - these are the I2C data and clock pins used to send and receive data from the module to your microcontroller. There are 10K pullups on these pins to the **VIN** pin. You can connect these pins to 5V I2C lines, there are level shifters on board to safely bring the pins down to 3V
- **RDY** - this it the 'data ready' pin output. If you want to stream data at high speed (higher than 100 times a second) you may want to listen to this pin for when data is ready to be read. Check the datasheet for more details about using the RDY pin, we don't use it because we don't read that fast!  

# Adafruit HMC5883L Breakout - Triple-Axis Magnetometer Compass Sensor

## Assembly

![](https://cdn-learn.adafruit.com/assets/assets/000/014/647/medium800/adafruit_products_1746_LRG.jpg?1393611879)

Assembly is really easy, you can use straight or 'right-angle' style headers to attach to the PCB. We'll be using the plain straight headers included  
  
The board comes with all surface-mount components pre-soldered. The included header strip can be soldered on for convenient use on a breadboard or with 0.1" connectors. You can also skip this step and solder on wires.

## Prepare the header strip:
Cut the strip to length if necessary. It will be easier to solder if you insert it into a breadboard - **long pins down** ![adafruit_products_header.jpg](https://cdn-learn.adafruit.com/assets/assets/000/014/641/medium640/adafruit_products_header.jpg?1393611600)

## Add the breakout board:
Place the breakout board over the pins so that the short pins poke through the breakout pads![adafruit_products_place.jpg](https://cdn-learn.adafruit.com/assets/assets/000/014/642/medium640/adafruit_products_place.jpg?1393611799)

## And Solder!
Be sure to solder all 5 pins for reliable electrical contact.  
  
_(For tips on soldering, be sure to check out our_ [_Guide to Excellent Soldering_](http://learn.adafruit.com/adafruit-guide-excellent-soldering)_)._  
![adafruit_products_solder.jpg](https://cdn-learn.adafruit.com/assets/assets/000/014/643/medium640/adafruit_products_solder.jpg?1393611813)

![adafruit_products_solder2.jpg](https://cdn-learn.adafruit.com/assets/assets/000/014/644/medium640/adafruit_products_solder2.jpg?1393611823)

You're done! Check your solder joints visually and continue onto the next steps

![adafruit_products_done.jpg](https://cdn-learn.adafruit.com/assets/assets/000/014/645/medium640/adafruit_products_done.jpg?1393611848)

# Adafruit HMC5883L Breakout - Triple-Axis Magnetometer Compass Sensor

## Wiring and Test

![](https://cdn-learn.adafruit.com/assets/assets/000/014/648/medium800/adafruit_products_wiring.jpg?1393611968)

This 3-axis magnetometer breakout is thankfully very easy to use. It only requires 2 power pins and 2 data pins.  
We'll be demoing it with an Arduino but any other microcontroller can be used if you are willing to port the I2C code. We suggest using an Arduino to start since its guaranteed to work

- Connect **VIN** to 5V pin on the Arduino. If you have a 3V logic Arduino, connect **VIN** to 3V instead
- Connect **GND** to the ground pin on the Arduino
- Connect the **SDA** pin on the breakout to the I2C data **SDA** pin on your Arduino. On an UNO & '328 based Arduino, this is also known as **A4** , on a Mega it is also known as **digital 20** and on a Leonardo/Micro, **digital 2**
- Connect the **SCL** pin on the breakout to the I2C clock **SCL** pin on your Arduino. On an UNO & '328 based Arduino, this is also known as **A5** , on a Mega it is also known as **digital 21** and on a Leonardo/Micro, **digital 3**  

The HMC5883L has a fixed I2C address, you can only connect _one_ sensor per microcontroller!  
# Download & Install Libraries

Lucky for you we have written a very nice sensor library for the HMC5883L, which is based off of our Adafruit\_Sensor framework library.

Open up the Arduino library manager:

![](https://cdn-learn.adafruit.com/assets/assets/000/084/189/medium800/adafruit_products_library_manager_menu.png?1573770754)

Search for the&nbsp; **Adafruit Sensor&nbsp;** library and install it

![](https://cdn-learn.adafruit.com/assets/assets/000/084/190/medium800/adafruit_products_sensors_adafruitsensor.png?1573770844)

Search for the **Adafruit**  **HMC5883&nbsp;** library and install it

![](https://cdn-learn.adafruit.com/assets/assets/000/084/191/medium800/adafruit_products_hmc5883.png?1573770908)

We also have a great tutorial on Arduino library installation at:  
[http://learn.adafruit.com/adafruit-all-about-arduino-libraries-install-use](http://learn.adafruit.com/adafruit-all-about-arduino-libraries-install-use "Link: http://learn.adafruit.com/adafruit-all-about-arduino-libraries-install-use")

# Demo

Now, open the demo with `file -> examples -> Adafruit_HMC5883_U -> magsensor` and upload it to your Arduino.

![](https://cdn-learn.adafruit.com/assets/assets/000/014/649/medium800/adafruit_products_loaddemo.gif?1448049682)

And open up the serial console. You will see some quick spec's about the HMC5883 and then a stream of data with X Y Z magnetometer data in uTeslas and then a calculated heading. If you hold the module so that it is parallel with the ground then it will point you in the direction of North!

![](https://cdn-learn.adafruit.com/assets/assets/000/014/895/medium800/adafruit_products_hmc5883test.png?1394064091)

If you have to mount the module so that some other axis is parallel with the ground (X or Y) you will have to modify the heading code to take the arcTan of X&Z or Y&Z instead of X&Y

# Adafruit HMC5883L Breakout - Triple-Axis Magnetometer Compass Sensor

## Downloads

# Datasheets & Files

- Grab the [HMC5883L datasheet here](http://www.adafruit.com/datasheets/HMC5883L_3-Axis_Digital_Compass_IC.pdf "Link: http://www.adafruit.com/datasheets/HMC5883L\_3-Axis\_Digital\_Compass\_IC.pdf"), it has a lot more specifications available!
- [This handy app note from Honeywell talks about how to calculate heading from a magnetometer's raw data](http://www.adafruit.com/datasheets/AN203_Compass_Heading_Using_Magnetometers.pdf "Link: http://www.adafruit.com/datasheets/AN203\_Compass\_Heading\_Using\_Magnetometers.pdf")
- [Fritzing object in the Adafruit Fritzing library](https://github.com/adafruit/Fritzing-Library)
- [EagleCAD PCB files on GitHub](https://github.com/adafruit/Adafruit-HMC5883-Mag-Compass-Sensor-PCB)

# Schematic

Click to enlarge

![](https://cdn-learn.adafruit.com/assets/assets/000/014/638/medium800/adafruit_products_hmcschem.png?1393610443)

# Breakout board design

Layout diagram in inches:

![](https://cdn-learn.adafruit.com/assets/assets/000/014/637/medium800/adafruit_products_diagram.png?1393610403)


## Featured Products

### Triple-axis Magnetometer (Compass) Board - HMC5883L

[Triple-axis Magnetometer (Compass) Board - HMC5883L](https://www.adafruit.com/product/1746)
He told you "Go West, young maker!" - but you don't know which way is West! Ah, if only you had this triple-axis magnetometer compass module. A magnetometer can sense where the strongest magnetic force is coming from, generally used to detect magnetic north.  
  
We based...

No Longer Stocked
[Buy Now](https://www.adafruit.com/product/1746)
[Related Guides to the Product](https://learn.adafruit.com/products/1746/guides)

## Related Guides

- [Tri-Layer Mini Round Robot Chassis Kit](https://learn.adafruit.com/tri-layer-mini-round-robot-chassis-kit.md)
- [Adjustable Breadboard Power Supply Kit](https://learn.adafruit.com/adjustable-breadboard-power-supply-kit.md)
- [MCP3008 - 8-Channel 10-Bit ADC With SPI Interface](https://learn.adafruit.com/mcp3008-spi-adc.md)
- [Adafruit Feather HUZZAH ESP8266](https://learn.adafruit.com/adafruit-feather-huzzah-esp8266.md)
- [Introducing the Adafruit Bluefruit LE UART Friend](https://learn.adafruit.com/introducing-the-adafruit-bluefruit-le-uart-friend.md)
- [UTi165 Thermal Fever Scanner Camera](https://learn.adafruit.com/uti165-thermal-fever-scanner-camera.md)
- [Introducing Gemma](https://learn.adafruit.com/introducing-gemma.md)
- [reef-pi Guide 5: Light Controller](https://learn.adafruit.com/reef-pi-lighting-controller.md)
- [Adafruit Terminal PiCowbell for Pico](https://learn.adafruit.com/adafruit-terminal-picowbell-for-pico.md)
- [Adafruit CCS811 Air Quality Sensor](https://learn.adafruit.com/adafruit-ccs811-air-quality-sensor.md)
- [Adafruit STSPIN220 Stepper Motor Driver Breakout Board](https://learn.adafruit.com/adafruit-stspin220-stepper-motor-driver-breakout-board.md)
- [Adafruit AD5693R 16-Bit DAC Breakout Board](https://learn.adafruit.com/adafruit-ad5693r-16-bit-dac-breakout-board.md)
- [Adafruit TPA2012 2.1W Stereo Audio Amplifier](https://learn.adafruit.com/adafruit-ts2012-2-8w-stereo-audio-amplifier.md)
- [Adafruit 1.47" Round Rectangle TFT Display](https://learn.adafruit.com/adafruit-1-47-round-rectangle-tft-display.md)
- [Adafruit STEMMA Reflective Photo Interrupt Sensor](https://learn.adafruit.com/adafruit-stemma-reflective-photo-interrupt-sensor.md)
