Home Code Raspberry Pi and HMC5883 example

Raspberry Pi and HMC5883 example

by shedboy71

The Honeywell HMC5883L is a surface-mount, multi-chip module designed for low-field magnetic sensing with a digital interface for applications such as low-cost compassing and magnetometry. The HMC5883L includes our state-of-the-art, 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 I2C serial 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.

The HMC5883L utilizes Honeywell’s Anisotropic Magnetoresistive (AMR) technology that provides advantages over other magnetic sensor technologies. These anisotropic, directional sensors feature precision in-axis sensitivity and linearity. These sensors’ solid-state construction with very low cross-axis sensitivity is designed to measure both the direction and the magnitude of Earth’s magnetic fields, from milli-gauss to 8 gauss. Honeywell’s Magnetic Sensors are among the most sensitive and reliable low-field sensors in the industry.


12-Bit ADC Coupled with Low Noise AMR Sensors Achieves 2 milli-gauss Field Resolution in ±8 Gauss Fields
Low Voltage Operations (2.16 to 3.6V) and Low Power Consumption (100 μA)
I2C Digital Interface
Wide Magnetic Field Range(+/-8Oe)
Fast 160Hz Maximum Output Rate





Save the following as HMC5883.py

import smbus
import time
# Get I2C bus
bus = smbus.SMBus(1)
# HMC5883 address, 0x1E
bus.write_byte_data(0x1E, 0x00, 0x60)
bus.write_byte_data(0x1E, 0x02, 0x00)
# HMC5883 address, 0x1E and Read data
data = bus.read_i2c_block_data(0x1E, 0x03, 6)
# Convert the data
xMag = data[0] * 256 + data[1]
if xMag > 32767 :
	xMag -= 65536
zMag = data[2] * 256 + data[3]
if zMag > 32767 :
	zMag -= 65536
yMag = data[4] * 256 + data[5]
if yMag > 32767 :
	yMag -= 65536
# Output data to screen
print "Magnetic field in X-Axis : %d" %xMag
print "Magnetic field in Y-Axis : %d" %yMag
print "Magnetic field in Z-Axis : %d" %zMag


run the example above – sudo python HMC5883.py




GY-271 HMC5883 three-axis magnetic field sensor

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