Qwiic Magnetometer (MLX90393) Hookup Guide

Pages
Contributors: Englandsaurus
Favorited Favorite 0

Introduction

The MLX90393 is a tri-axial magnetic sensor capable of sensing very small fields (like the Earth’s magnetic field) while behaving as one would want and expect during saturation in larger fields (like a nearby magnet). It turns out the favorite HMC5883L and other such sensors that are intended for compass applications have a low dynamic range but also strange and undefined behavior in large fields. Ted Yapo did an incredibly extensive characterization of the sensor over on Hackaday. He published his controlled experiments testing a few sensors and found the MLX90393 to be superior.

The MLX90393 can be used as a compass sensor but also works well as a non-contact controller (joystick), flow meter (with magnetic impeller), or a linear actuator position sensor. The breakout board is also a part of SparkFun’s Qwiic system, so you won’t have to do any soldering to figure out what the magnetic fields look like.

SparkFun Triple Axis Magnetometer Breakout - MLX90393 (Qwiic)

SEN-14571
$14.95

In this hookup guide, we’ll get going by getting some basic readings from the sensor, then look at how to configure the sensor on different I2C addresses.

Required Materials

To get started, you’ll need a microcontroller to control everything in your project.

SparkFun RedBoard - Programmed with Arduino

DEV-13975
$19.95
27
SparkFun ESP32 Thing

DEV-13907
$19.95
52
Raspberry Pi 3

DEV-13825
$39.95
90
Particle Photon (Headers)

WRL-13774
$19.00
28

Now, to get your microcontroller into the Qwiic ecosystem, the key will be one of the following Qwiic shields to match your preference of microcontroller:

SparkFun Qwiic Shield for Arduino

DEV-14352
$5.95
SparkFun Qwiic HAT for Raspberry Pi

DEV-14459
$4.95
1
SparkFun Qwiic Shield for Photon

DEV-14477
$4.95

You will also need a Qwiic cable to connect the shield to your magnetometer, choose a length that suits your needs.

Qwiic Cable - 200mm

PRT-14428
$1.50
Qwiic Cable - 500mm

PRT-14429
$1.95
Qwiic Cable - 100mm

PRT-14427
$1.50
Qwiic Cable - 50mm

PRT-14426
$0.95

Or, here’s the wishlist to follow along exactly with this guide:

Suggested Reading

If you aren’t familiar with our new Qwiic system, we recommend checking out our overview:

WHAT IS QWIIC?

We would also recommend taking a look at the hookup guide for the Qwiic Shield if you haven’t already. Brushing up on your skills in I2C is also recommended, as all Qwiic sensors are I2C.

I2C

An introduction to I2C, one of the main embedded communications protocols in use today.

Serial Terminal Basics

This tutorial will show you how to communicate with your serial devices using a variety of terminal emulator applications.

Qwiic Shield for Arduino & Photon Hookup Guide

Get started with our Qwiic ecosystem with the Qwiic shield for Arduino or Photon.

Hardware Overview

Let’s look over a few characteristics of the MLX90393 so we know a bit more about how it behaves.

CharacteristicRange
Operating Voltage2.2V - 3.6V
Operating Temperature-20°C - 85°C
Resolution0.161 µT
Current Consumption100 µA (Typ.)
I2C Address0xC0

Pins

The characteristics of the available pins on the magnetometer are outlined in the table below.

Pin LabelPin FunctionInput/OutputNotes
3.3VPower SupplyInputShould be between 2.2V - 3.6V
GNDGroundInput0V/common voltage.
SDAI2C Data SignalBi-directionalBi-directional data line. Voltage should not exceed power supply (e.g. 3.3V).
SCL/SCLKI2C Clock SignalInputMaster-controlled clock signal. Voltage should not exceed power supply (e.g. 3.3V).
I2C Data SignalBi-directionalBi-directional data line. Voltage should not exceed power supply (e.g. 3.3V).
CSChip SelectInputChip Select pin, digital input. Tied high for I2C operation, cut trace jumper and tie low for SPI.
INTInterruptOutputInterrupt pin, active high, digital output. Also configurable as a data ready pin
TRIGResetInput/OutputTrigger pin, active high, digital output. Also configurable as interrupt pin.

Optional Features

The Qwiic MLX90393 has onboard I2C pull up resistors, which can be removed by removing the solder from the jumper highlighted below. Only remove this solder if you are using your own pullups on the I2C lines.

I2C Pullup Jumper

The I2C of the Qwiic Magnetometer can be changed using the A0 and A1 jumpers on the back of the board. Simply cut the traces connecting each pad to ground (0) and solder the other side to connect it to 3.3V (1).

Address Jumper

To operate the Qwiic Magnetometer in SPI mode, cut the chip select trace jumper (labeled below) and ensure that the CS pin is then connected to ground.

Chip Select Jumper

Hardware Assembly

If you haven’t yet assembled your Qwiic Shield, now would be the time to head on over to that tutorial.

Qwiic Shield for Arduino Photon Hookup Guide

With the shield assembled, SparkFun’s new Qwiic environment means that connecting the sensor could not be easier. Just plug one end of the Qwiic cable into the Magnetometer breakout, the other into the Qwiic Shield of your choice and you’ll be ready to upload a sketch and figure out what the magnetic fields look like. It seems like it’s too easy to use, but that’s why we made it that way!

Connected Magnetometer

Example Code

Before we get into programming our Magnetometer, we’ll need to download and install the magnetometer library. Ted Yapo has written a library to control the Qwiic Magnetometer. You can obtain the .zip for this library using the below button. Never installed a library before? That’s ok! Checkout our tutorial on installing Arduino Libraries.

Download the MLX90393 Library

Note: This example assumes you are using the latest version of the Arduino IDE on your desktop. If this is your first time using Arduino, please review our tutorial on installing the Arduino IDE. If you have not previously installed an Arduino library, please check out our installation guide.

Since this isn’t a SparkFun library, the example sketches are not included, so you’ll have to download those from the Github page by clicking the below button. The example sketches are located in software

Download the MLX90393 Github Repo

Go ahead and unzip these examples to a location of your choosing and open Example 1 - Basic Readings.

Example 1 - Basic Readings

Once we have our first example opened up, let’s look at how things are structured as we set up our sensor. First we create our sensor, and then an array of floats to contain our data.

language:c
MLX90393 mlx;
MLX90393::txyz data; //Create a structure, called data, of four floats (t, x, y, and z)

Then in our setup loop, we must initialize our sensor, notice the delay afterwards, this gives the sensor time to initialize before we start attempting to talk to it.

language:c
mlx.begin(); //Assumes I2C jumpers are GND. No DRDY pin used.
delay(1000); //Allow the sensor to initialize.

Once the sensor is initialized, our void loop() will read the data from our sensor into data, the array of floats we created earlier, and print it over serial to our serial monitor. Opening up your serial monitor to a baud rate of 9600 should display output similar to that shown below. The numbers are in units of µT. For scale, the magnetic field of the Earth ranges from 25 - 65 µT.

Basic Sensor Readings

Example 2 - Configure Sensor

Go ahead and open Example 2 from the location you saved it in. The only differences between this example and the previous one are in the setup() function. Notice how we now call the begin() function with 3 arguments, which are the value of the A0 jumper, the value of the A1 jumper, and the pin that we’ve connected to INT. The below code assumes that the A0 jumper has been cut from ground and soldered to 3.3V and that A3 is connected to the INT pin (sometimes referred to in the datasheet as the DRDY or data ready pin).

language:c
byte status = mlx.begin(1, 0, A3);

If we must change the address of our magnetometer due to overlapping addresses, ensure that the values we pass into begin() match up with the values of our address jumpers. The MLX90393 also has a ton of different ways to change the sensor behavior. You can set the gain, resolution of the x, y and z channels, oversampling rates, and even offsets of your x, y, and z channels. In this example however, we set the gain to 1 and the resolution of our x, y, and z channels to their finest setting, 0.

language:c
mlx.setGainSel(1);
mlx.setResolution(0, 0, 0);

A table showing the different possible resolutions of the sensor for the X and Y axes is shown below. These can be selected using the setGainSel(uint8_t gain) and setResolution(uint8_t x, uint8_t y, uint8_t z) functions. All resolutions are in units of µT/LSB.

XY Axis:

GainRes = 0Res = 1Res = 2Res = 3
00.8051.6103.2206.440
10.6441.2882.5765.152
20.4830.9661.9323.864
30.4030.8051.6103.220
40.3220.6441.2882.576
50.2680.5371.0732.147
60.2150.4290.8591.717
70.1610.3220.6441.288

Shown below is the table containing the possible combinations for resolutions on the Z axis. Once again, units are in µT/LSB

Z Axis:

GainRes = 0Res = 1Res = 2Res = 3
01.4682.9365.87211.744
11.1742.3494.6989.395
20.8811.7623.5237.046
30.7341.4682.9365.872
40.5871.1742.3494.698
50.4890.9791.9573.915
60.3910.7831.5663.132
70.2940.5871.1742.349

Since we are simply connecting our magnetometer on a different address, our output should be similar to the one in the first example.

Resources and Going Further

For more information, check out the resources below:

Need some inspiration for your next project? Check out some of these related tutorials:

Qwiic Shield for Arduino & Photon Hookup Guide

Get started with our Qwiic ecosystem with the Qwiic shield for Arduino or Photon.

AS726X NIR/VIS Spectral Sensor Hookup Guide

It's now easier than ever to measure and characterize how different materials absorb and reflect different wavelengths of light. The AS726X spectral sensors allow you to detect wavelengths in the visible range (VIS) and near infrared range (NIR)!

CCS811/BME280 (Qwiic) Environmental Combo Breakout Hookup Guide

Sense various environmental conditions such as temperature, humidity, barometric pressure, eCO2 and tVOCs with the CCS811 and BME280 combo breakout board.
New!

Qwiic VR IMU (BNO080) Hookup Guide

Figure out how things are oriented with the robust 9 degrees of freedom (DOF) BNO080 IMU. Maybe even make your own virtual reality (VR) applications if you're feeling savvy.

Or check out this blog post about using a magnetometer as a compass: