Adafruit MLX90393 User Manual

Page 1
MLX90393 Wide-Range 3-Axis
Magnetometer
Created by Kevin Townsend
https://learn.adafruit.com/mlx90393-wide-range-3-axis-magnetometer
Last updated on 2021-11-15 07:30:43 PM EST
©Adafruit Industries Page 1 of 22
Page 2

Table of Contents

Overview

Specifications

Pinout

Power Pins
Digital Pins

Assembly

Assembly
Prepare the header strip:
Add the MLX90393:
And Solder!

Arduino

Wiring
Installation
Load Example
Example Code
Setting the Gain

Arduino API

3
6
7
7
8
9
9
9
10
11
11
12
12
13
14
15

Python and CircuitPython

CircuitPython Wiring
Python Wiring
Library Installation
Python Installation of the MLX90393 Library
Example
Adjusting Gain

Python Docs

Downloads

Downloads and Design Files
Datasheet
Schematic for STEMMA QT Version
Fab Print for STEMMA QT Version
Schematic for Original Version
Board Layout for Original Version
15
15
16
17
18
18
19
20
20
20
20
21
21
21
22
©Adafruit Industries Page 2 of 22
Page 3

Overview

Measure the invisible magnetic fields that surround us, with this wide-range
magnetometer. The MLX90393 is a wide range magnetic field sensor, that can
measure 16-bits in ranges from +/-5mT up to +/-50mT in all 3 axes.
Compared to most magenetometers, this gives a huge range, which makes it
excellent for detecting magnets and magnetic orientation, rather than the Earths
magnetic field. (Magnets have a much stronger field that overwhelms most
magnetometers that would normally be used for orientation with respect to the North
Pole)
©Adafruit Industries Page 3 of 22
Page 4
To make it easy to use, we've placed this tiny little sensor onto a breakout board, with
a 3.3V power supply and level shifter. This makes it easy to use with any 3 or 5V
microcontroller. Our Arduino and CircuitPython code will get you started in a jiffy, with
I2C communication to the sensor. You will be readin' out those Gauss's in minutes!
With the address select pins you can have up to 4 sensors on one I2C bus.
Comes as a fully assembled and tested breakout board with a small piece of header
for use with a breadboard. As if that weren't enough, we've now also addedSparkFun
qwiic(https://adafru.it/Fpw)compatibleSTEMMA QT(https://adafru.it/Ft4)connectors
for the I2C bus so you don't even need to solder the I2C and power lines. Just wire
up to your favorite micro using aSTEMMA QT adapter cable.(https://adafru.it/JnB)Th
©Adafruit Industries Page 4 of 22
Page 5
e Stemma QT connectors also mean the MLX90393 can be used with ourvarious
associated accessories.(https://adafru.it/Ft6)QT Cable is not included, but we have a
variety in the shop(https://adafru.it/JnB).

Specifications

The MLX90393 has the following key technical specifications:
16-bit output on all three (XYZ) magnetic field sensors
An unusually large dynamic range of 5-50 mT (1 mT or millitesla = 10 G or Gauss).
By comparison, the LSM303DLHC saturates at +/-8.1 G (0.81 mT) at maximum
range setting.
Up to ~500 Hz sample rate [1]
User-adjustable I2C address to allow multiple sensors in your project (two I2C
ADDR pins for four possible I2C addresses).
[1] Based on OSR=0, DIG_FILT=2, HALLCONF=0xC for 1.84ms conversion time. See
15.1.5 HALLCONF [3:0} in the datasheet for details.
There are two versions of this board - the STEMMA QT version shown above, and
the original header-only version shown below. Code works the same on both!
©Adafruit Industries Page 5 of 22
Page 6

Pinout

The MLX90393 3-Axis magnetometer breakout has the following pins:
©Adafruit Industries Page 6 of 22
Page 7

Power Pins

This breakout board can be run on3.3V and5V systems, although only theSCLandS
DA lines are 5V safe (other pins like INT will need to be manually level-shifted by you).
VIN - This is the input to the 3.3V voltage regulator, which makes it possible to
use the 3.3V sensor on 5V systems. It also determines the logic level of the SCL
and SDA pins. Connect this to3.3V on the MCU for 3.3V boards (Adafruit
Feathers), or5.0V for 5V Arduinos (Arduino Uno, etc.).
3VO - This is theOUTPUT of the 3.3V regulator, and can be used to provide
3.3V power to other parts of your project if required (<100mA).
GND - Connect this to theGND pin on your development board to make sure
they are sharing a common GND connection, or the electrons won't have
anywhere to flow!
NOTE: Only SCL and SDA are 5V safe on this board. Using any other pins on a
5V system will require manual level shifting of the pins used (INT, etc.)

Digital Pins

SCL - The clock line on the I2C bus. This pin has an internal pullup resistor on
the PCB, which is required as part of the I2C spec, meaning you don't need to
add one externally yourself.
SDA- The data line on the I2C bus. This pin has an internal pullup resistor on
the PCB, which is required as part of the I2C spec, meaning you don't need to
add one externally yourself.
INT - This INT pin can be configured to 'fire' whenever a new data sample is
read in single measurement mode, which is what the device boots up to by
default in our driver.
On the top row, you have a few more rarely used specialty pins, though normally you
will simply leave these unsoldered and unconnected:
A0andA1: These two pins are LOW (0) by default, but if you need to connect
multiple MLX90393s to your MCU or resolve an address conflict, you can adjust
the logic on these two pins which will change the last two bits of the I2C
address that this breakout responds to. Most of the time, you will simplyleave
these disconnected. (On the original version, these pins were on the bottom
row.)
©Adafruit Industries Page 7 of 22
Page 8
CS (AKA SENB) - This is used to set the sensor in I2C or SPI mode, but this
breakout only supports I2C so the pin is provided purely for testing purposes
unless you want to dig into writing your own SPI driver and making the required
changes to the PCB.
TR(AKA INT/TRG)- This pin can optionally be setup to fire an interrupt in
addition to theINT pin, and is basically a mirror on INT when configured as such.
MIS (AKATRIG) - This is used as MISO on the SPI bus, but SPI isn't supported on
this breakout, so it is only provided to testing purposes.
G - This is simply an additional GND pin, and can be left unconnected if not
required.

Assembly

©Adafruit Industries Page 8 of 22
Page 9

Assembly

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.

Add the MLX90393:

Place the board over the pins so that the
short pins poke through the top of the
breakout pads.
©Adafruit Industries Page 9 of 22
Page 10

And Solder!

Be sure to solder all pins for reliable
electrical contact.
(For tips on soldering, be sure to check
out theGuide to Excellent
Soldering(https://adafru.it/aTk)).
OK, you're done!
You can now plug in your Magnetometer and start measuring some magnetic fields!
©Adafruit Industries Page 10 of 22
Page 11

Arduino

Wiring

Hooking up the MLX90393 to your Feather or Arduino is easy:
If you are running a Feather (3.3V),
connect the3V pin to 3.3V on the
MLX90393 (red wire on STEMMA
QT version)
If you are running a 5V Arduino
(Uno, etc.), connect5V to 5V on the
MLX90393 (red wire on STEMMA
QT version)
ConnectGND on the MCU toGND
on the MLX90393 (black wire on
STEMMA QT version)
Arduino SCL to MLX90393 SCL
(yellow wire on STEMMA QT
version)
Arduino SDA to MLX90393 SDA
(blue wire on STEMMA QT version)
The final results should resemble the illustration above, showing an Adafruit Metro
development board.
©Adafruit Industries Page 11 of 22
Page 12
Only the SCL and SDA pins on the MLX90393 are level shifted and safe to use
on 5V systems like the Arduino Uno. If you are using other pins on the breakout
(INT, etc.) on a 5V system, you will need to level shift these yourself. We have
some tutorials on how to do this in the learning system, simply search for 'level
shifting'!

Installation

You can install the Adafruit MLX90393 Library for Arduino using the Library Manager
in the Arduino IDE:
Click theManage Libraries ... menu item, search for Adafruit MLX90393,and select
theAdafruit MLX90393 library:

Load Example

Open upFile -> Examples -> Adafruit MLX90393->basicdemoand upload to your
Arduino wired up to the sensor
Upload the sketch to your board and open up the Serial Monitor (Tools->Serial
Monitor). You should see the temperature inmagnetic field values for X/Y/Z.
©Adafruit Industries Page 12 of 22
Page 13

Example Code

The following example code is part of the standard library, but illustrates how you can
retrieve sensor data from the MLX90393 for the X, Y and Z axis:
#include "Adafruit_MLX90393.h"
Adafruit_MLX90393 sensor = Adafruit_MLX90393();
#define MLX90393_CS 10
void setup(void)
{ Serial.begin(115200);
/* Wait for serial on USB platforms. */ while (!Serial) { delay(10); }
Serial.println("Starting Adafruit MLX90393 Demo");
if (! sensor.begin_I2C()) { // hardware I2C mode, can pass in address &
alt Wire
//if (! sensor.begin_SPI(MLX90393_CS)) { // hardware SPI mode Serial.println("No sensor found ... check your wiring?"); while (1) { delay(10); } } Serial.println("Found a MLX90393 sensor");
sensor.setGain(MLX90393_GAIN_2_5X); // You can check the gain too Serial.print("Gain set to: "); switch (sensor.getGain()) { case MLX90393_GAIN_1X: Serial.println("1 x"); break; case MLX90393_GAIN_1_33X: Serial.println("1.33 x"); break; case MLX90393_GAIN_1_67X: Serial.println("1.67 x"); break; case MLX90393_GAIN_2X: Serial.println("2 x"); break; case MLX90393_GAIN_2_5X: Serial.println("2.5 x"); break; case MLX90393_GAIN_3X: Serial.println("3 x"); break; case MLX90393_GAIN_4X: Serial.println("4 x"); break; case MLX90393_GAIN_5X: Serial.println("5 x"); break; }
// Set resolution, per axis sensor.setResolution(MLX90393_X, MLX90393_RES_19); sensor.setResolution(MLX90393_Y, MLX90393_RES_19); sensor.setResolution(MLX90393_Z, MLX90393_RES_16);
// Set oversampling sensor.setOversampling(MLX90393_OSR_2);
// Set digital filtering sensor.setFilter(MLX90393_FILTER_6); }
void loop(void) {
float x, y, z;
// get X Y and Z data at once if (sensor.readData(&x, &y, &z)) { Serial.print("X: "); Serial.print(x, 4); Serial.println(" uT"); Serial.print("Y: "); Serial.print(y, 4); Serial.println(" uT"); Serial.print("Z: "); Serial.print(z, 4); Serial.println(" uT");
©Adafruit Industries Page 13 of 22
Page 14
} else { Serial.println("Unable to read XYZ data from the sensor."); }
delay(500);
/* Or....get a new sensor event, normalized to uTesla */
sensors_event_t event; sensor.getEvent(&event); /* Display the results (magnetic field is measured in uTesla) */ Serial.print("X: "); Serial.print(event.magnetic.x); Serial.print(" \tY: "); Serial.print(event.magnetic.y); Serial.print(" \tZ: "); Serial.print(event.magnetic.z); Serial.println(" uTesla ");
delay(500); }
You should get something resembling the following output when you open the Serial
Monitor at 9600 baud:

Setting the Gain

The driver will default to 1x gain, but if you wish to adjust the gain you can do so using
the ` setGain(enum mlx90393_gain gain) ` function, passing in one of the following
values:
enum mlx90393_gain { MLX90393_GAIN_5X = (0x00), MLX90393_GAIN_4X, MLX90393_GAIN_3X, MLX90393_GAIN_2_5X, MLX90393_GAIN_2X, MLX90393_GAIN_1_67X, MLX90393_GAIN_1_33X, MLX90393_GAIN_1X };
For example, to set the gain to 2x you would call the function as follows:
sensor.setGain(MLX90393_GAIN_2X);
Resolution is managed internally in the driver itself, and defaults to the maximum
value of +/- 2^15 LSBs.
©Adafruit Industries Page 14 of 22
Page 15

Arduino API

Arduino API(https://adafru.it/DQK)

Python and CircuitPython

Using the MLX90393 with CircuitPython is easy. The Adafruit_CircuitPython_MLX903
93(https://adafru.it/DQL) repo on Github always contains the latest public code, and
allows you to quickly and easily get started with the Adafruit MLX90393 breakout
board.

CircuitPython Wiring

The diagram below shows how you can wire up your CircuitPython board (in Feather
form below) to the MLX90393:
©Adafruit Industries Page 15 of 22
Page 16
Simple connect:
3V on the dev board toVIN on the
MLX90393 (red wire on STEMMA
QT version)
GND on the dev board toGND on
the MLX90393 (black wire on
STEMMA QT version)
SCL on the dev board toSCL on the
MLX90393 (yellow wire on
STEMMA QT version)
SDA on the dev board to SDA on
the MLX90393 (blue wire on
STEMMA QT version)

Python Wiring

Since there'sdozensof Linux computers/boards you can use we will show wiring for
Raspberry Pi. For other platforms,please visit the guide for CircuitPython on Linux to
see whether your platform is supported(https://adafru.it/BSN).
Make the following connections between the Pi and the MLX90393:
©Adafruit Industries Page 16 of 22
Page 17
Simple connect:
3.3V on the RPi toVIN on the
MLX90393 (red wire on STEMMA
QT version)
GND on the RPi toGND on the
MLX90393 (black wire on STEMMA
QT version)
SCL on the RPi toSCL on the
MLX90393 (yellow wire on
STEMMA QT version)
SDA on the RPi to SDA on the
MLX90393 (blue wire on STEMMA
QT version)

Library Installation

You'll need to install the Adafruit CircuitPython MLX90393(https://adafru.it/DQL)
library on your CircuitPython board.
First make sure you are running thelatest version of Adafruit CircuitPython(https://
adafru.it/Amd)for your board.
©Adafruit Industries Page 17 of 22
Page 18
Next you'll need to install the necessary librariesto use the hardware--carefully follow
the steps to find and install these libraries fromAdafruit's CircuitPython library bundle
(https://adafru.it/uap). Our CircuitPython starter guide hasa great page on how to
install the library bundle(https://adafru.it/ABU).
For non-express boards like the Trinket M0 or Gemma M0, you'll need to manually
install the necessary libraries from the bundle:
adafruit_mlx90393.mpy
adafruit_bus_device
Before continuing make sure your board's lib folder or root filesystem has theadafrui
t_mlx90393.mpy,andadafruit_bus_devicefiles and folderscopied over.
Nextconnect to the board's serial REPL(https://adafru.it/pMf)so you are at the
CircuitPython>>>prompt.

Python Installation of the MLX90393 Library

You'll need to install the Adafruit_Blinka library that provides the CircuitPython
support in Python. This may also require enabling I2C on your platform and verifying
you are running Python 3.Since each platform is a little different, and Linux changes
often, please visit the CircuitPython on Linux guide to get your computer ready(https
://adafru.it/BSN)!
Once that's done, from your command line run the following command:
sudo pip3 install adafruit-circuitpython-mlx90393
If your default Python is version 3 you may need to run 'pip' instead. Just make sure
you aren't trying to use CircuitPython on Python 2.x, it isn't supported!

Example

The following example shows a minimal python script to make use of the MLX90393.
Running this example, a three axis magnetic field measurement will be read every
second, and displayed via the serial output. A timestamp will precede each sample,
©Adafruit Industries Page 18 of 22
Page 19
and if any error condition was encountered during the read attempt, the details of the
error code will be displayed:
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries # SPDX-License-Identifier: MIT
import time import board import adafruit_mlx90393
i2c = board.I2C() # uses board.SCL and board.SDA SENSOR = adafruit_mlx90393.MLX90393(i2c, gain=adafruit_mlx90393.GAIN_1X)
while True:
MX, MY, MZ = SENSOR.magnetic print("[{}]".format(time.monotonic())) print("X: {} uT".format(MX)) print("Y: {} uT".format(MY)) print("Z: {} uT".format(MZ)) # Display the status field if an error occured, etc. if SENSOR.last_status > adafruit_mlx90393.STATUS_OK: SENSOR.display_status() time.sleep(1.0)
If you open theSerial tab in mu-editor with the sample code running, you should get
output resembling the following if everything was setup correctly:

Adjusting Gain

The gain on the sensor can be adjusted via the .gain property, as shown below:
SENSOR.gain = adafruit_mlx90393.GAIN_2X
Alternatively, you can set the gain property via the constructor, as shown below:
SENSOR = adafruit_mlx90393.MLX90393(I2C_BUS, gain=adafruit_mlx90393.GAIN_1X)
The value assigned to the gain property can be one of the following entries:
GAIN_5X
©Adafruit Industries Page 19 of 22
Page 20
GAIN_4X
GAIN_3X
GAIN_2_5X
GAIN_2X
GAIN_1_67X
GAIN_1_33X
GAIN_1X
Resolution is managed internally in the driver itself, and defaults to the maximum
value of +/- 2^15 LSBs.

Python Docs

Python Docs(https://adafru.it/DQM)

Downloads

Downloads and Design Files

Board Files on Github(https://adafru.it/DQN)
Manufacturer's Product Page(https://adafru.it/DQO)
Arduino Library on Github(https://adafru.it/DQP) (for PRs and driver
development!)
CircuitPython Library on Github(https://adafru.it/DQL)
Fritzing Part(https://adafru.it/QC9)

Datasheet

You can download the datasheet for this chip using the following link:
MLX90393-Datasheet-Melexis.pdf
https://adafru.it/DQR
©Adafruit Industries Page 20 of 22
Page 21

Schematic for STEMMA QT Version

Fab Print for STEMMA QT Version

Schematic for Original Version

The schematic for this breakout is available below:
©Adafruit Industries Page 21 of 22
Page 22

Board Layout for Original Version

The breakout has the following physical dimensions:
©Adafruit Industries Page 22 of 22
Loading...