Raspberry Pi MAG3110 Sensor Python Tutorial
by Dcube Tech Ventures in Circuits > Electronics
82 Views, 0 Favorites, 0 Comments
Raspberry Pi MAG3110 Sensor Python Tutorial
MAG3110 is a small, low power, digital 3-axis magnetometer. Its sensitivity is very high as well as it is embodied with an efficient noise reduction mechanism. It requires very low power for its efficient operation and works in single-shot measurement mode. Here is the demonstration with a python code using Raspberry Pi.
What You Need..!!
Connection:
Take a I2C shield for raspberry pi and gently push it over the gpio pins of raspberry pi.
Then connect the one end of I2C cable to MAG3110 sensor and the other end to the I2C shield.
Also connect the Ethernet cable to the pi or you can use a WiFi module.
Connections are shown in the picture above.
Code:
The python code for MAG3110 can be downloaded from our github repository-Dcube Store
Here is the link for the same :
https://github.com/DcubeTechVentures/MAG3110/blob/master/Python/MAG3110.py
We have used SMBus library for python code, the steps to install SMBus on raspberry pi is described here:
https://pypi.python.org/pypi/smbus-cffi/0.5.1
You can also copy the code from here, it is given as follows:
# Distributed with a free-will license.
# Use it any way you want, profit or free, provided it fits in the licenses of its associated works.
# MAG3110
# This code is designed to work with the MAG3110_I2CS I2C Mini Module
import smbus
import time
# Get I2C bus
bus = smbus.SMBus(1)
# MAG3110 address, 0x0E(14)
# Select Control register, 0x10(16)
# 0x01(01) Normal mode operation, Active mode
bus.write_byte_data(0x0E, 0x10, 0x01)
time.sleep(0.5)
# MAG3110 address, 0x0E(14)
# Read data back from 0x01(1), 6 bytes
# X-Axis MSB, X-Axis LSB, Y-Axis MSB, Y-Axis LSB, Z-Axis MSB, Z-Axis LSB
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 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
Applications:
MAG3110 is a small, low power, digital 3-axis magnetometer. It can be employed in electronic compass and location based services which includes Smartphones, personal navigation devices, robotics, UAVs, speed sensing, current sensing and wrist watches with embedded electronic compasses (e-compass) function.