Home Code Raspberry Pi and MLX90393 magnetic field sensor example

Raspberry Pi and MLX90393 magnetic field sensor example

by shedboy71

The MLX90393 magnetic field sensor can be reprogrammed to different modes and with different settings at run-time. The sensor offers a 16-bit output proportional to the magnetic flux density sensed along the XYZ axes using the Melexis proprietary Triaxis® technology and also offers a temperature output signal. These digital values are available via I2C and SPI, where the MLX90393 is a slave on the bus.

By selecting which axes are to be measured, the raw data can be used as input for further post-processing, such as for joystick applications, rotary knobs, and more complex 3D position sensing applications. Unparallelled performance is achieved with this sensor, which is primarily targeting industrial and consumer applications.

Connection

Module Raspberry Pi
VDD 3v3
Gnd Gnd
SDA Pin 3 – GPIO2
SCL Pin 5 – GPIO3

Code

 
# 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.
# MLX90393
# This code is designed to work with the MLX90393_I2CS I2C Mini Module available from ControlEverything.com.
# https://www.controleverything.com/products
import smbus
import time
# Get I2C bus
bus = smbus.SMBus(1)
# MLX90393 address, 0x0C(12)
# Select write register command, 0x60(96)
# AH = 0x00, AL = 0x5C, GAIN_SEL = 5, Address register (0x00 << 2)
config = [0x00, 0x5C, 0x00]
bus.write_i2c_block_data(0x0C, 0x60, config)
# Read data back, 1 byte
# Status byte
data = bus.read_byte(0x0C)
# MLX90393 address, 0x0C(12)
# Select write register command, 0x60(96)
# AH = 0x02, AL = 0xB4, RES for magnetic measurement = 0, Address register (0x02 << 2)
config = [0x02, 0xB4, 0x08]
bus.write_i2c_block_data(0x0C, 0x60, config)
# Read data back, 1 byte
# Status byte
data = bus.read_byte(0x0C)
# MLX90393 address, 0x0C(12)
# Start single meaurement mode, X, Y, Z-Axis enabled
bus.write_byte(0x0C, 0x3E)
# Read data back, 1 byte
# Status byte
data = bus.read_byte(0x0C)
time.sleep(0.5)
# MLX90393 address, 0x0C(12)
# Read data back from 0x4E(78), 7 bytes
# Status, xMag msb, xMag lsb, yMag msb, yMag lsb, zMag msb, zMag lsb
data = bus.read_i2c_block_data(0x0C, 0x4E, 7)
# Convert the data
xMag = data[1] * 256 + data[2]
if xMag > 32767 :
xMag -= 65536
yMag = data[3] * 256 + data[4]
if yMag > 32767 :
yMag -= 65536
zMag = data[5] * 256 + data[6]
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

 

Output

Run the file above in the command line – sudo python MLX90393.py

 

Links

https://www.melexis.com/-/media/files/documents/datasheets/mlx90393-datasheet-melexis.pdf

CJMCU-90393, MLX90393 digital 3D Holzer sensor, displacement, angle, rotation, 3D position

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