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 VIN on the MLX90393
  • If you are running a 5V Arduino (Uno, etc.), connect 5V to VIN on the MLX90393
  • Connect GND on the MCU to GND on the MLX90393
  • Connect the SCL pins together ...
  • ... and finally connect the SDA pins together

The final results should resemble the illustration above, showing an Adafruit Metro development board.

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.

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 <Wire.h>

#include "Adafruit_MLX90393.h"

Adafruit_MLX90393 sensor = Adafruit_MLX90393();

void setup(void)
{
  Serial.begin(9600);

  /* Wait for serial on USB platforms. */
  while(!Serial) {
      delay(10);
  }

  Serial.println("Starting Adafruit MLX90393 Demo");

  if (sensor.begin())
  {
    Serial.println("Found a MLX90393 sensor");
  }
  else
  {
    Serial.println("No sensor found ... check your wiring?");
    while (1);
  }
}

void loop(void)
{
    float x, y, z;

    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");
    } else {
        Serial.println("Unable to read XYZ data from the sensor.");
    }

    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:

Download: file
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:

Download: file
sensor.setGain(MLX90393_GAIN_2X);

Resolution is managed internally in the driver itself, and defaults to the maximum value of +/- 2^15 LSBs.

This guide was first published on Feb 13, 2019. It was last updated on Feb 13, 2019. This page (Arduino) was last updated on Sep 22, 2019.