Home Code Raspberry Pi MAG3110 magnetometer example

Raspberry Pi MAG3110 magnetometer example

by shedboy71

Freescale’s MAG3110 is a small, low-power, digital 3-axis magnetometer. The device can be used in conjunction with a 3-axis accelerometer to realize an orientation independent electronic compass that can provide accurate heading information. It features a standard I2C serial interface output and smart embedded functions.

The MAG3110 is capable of measuring magnetic fields with an output data rate (ODR) up to 80 Hz; these output data rates correspond to sample intervals from 12.5 ms to several seconds. The MAG3110 is available in a plastic DFN package and it is guaranteed to operate over the extended temperature range of -40°C to +85°C.

Again for ease to use its easier to buy a module with the device fitted to it, here is one that I purchased. You can see the MAG3110 in the middle of the picture


I also found this schematic online of a similar module to the one pictured above. Looking at the schematic it looks very similar to the module




Raspberry Pi Pin MAG3110 Pin
3v3 VCC


An I2C device so easy to wire up as per the connections above





Save the following code as mag3110.py

[codesyntax lang=”python”]

import smbus
import time

bus = smbus.SMBus(1)

# MAG3110 I2C address 0x0E
# Select Control register, 0x10(16)
bus.write_byte_data(0x0E, 0x10, 0x01)


# MAG3110 I2C address 0x0E
# Read data back from 0x01(1), 6 bytes
data = bus.read_i2c_block_data(0x0E, 0x01, 6)

# Convert the data
xMag = data[0] * 256 + data[1]
if xMag > 32767 :
	xMag -= 65536

yMag = data[2] * 256 + data[3]
if yMag > 32767 :
	yMag -= 65536

zMag = data[4] * 256 + data[5]
if zMag > 32767 :
	zMag -= 65536

# Output data
print "X-Axis : %d" %xMag
print "Y-Axis : %d" %yMag
print "Z-Axis : %d" %zMag




Run the code above – sudo python mag3110.py



MAG3110 compass module three-axis electronic compass module magnetoresistive sensor magnetometer module

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